#!/usr/bin/env python # -*- coding: utf-8 -*- import hashlib from Crypto.Cipher import AES import base64 from binascii import b2a_hex def create_md5_pwd(password): m = hashlib.md5() b = password.encode(encoding='utf-8') m.update(b) md5_pwd = m.hexdigest() return md5_pwd class PrpCrypt(object): def __init__(self, API_SECRET): self.key = API_SECRET[:16].encode('gbk') # 密匙 self.iv = API_SECRET[16:].encode('gbk') # 密匙向量 def encrypt(self,text): # 加密 mycipher = AES.new(self.key, AES.MODE_CBC, self.iv) # 加密的明文长度必须为16的倍数,如果长度不为16的倍数,则需要补足为16的倍数 # 将iv(密钥向量)加到加密的密文开头,一起传输 ciphertext = self.iv + mycipher.encrypt(text.encode()) return ciphertext # 加密 def decrypt(self,text): # 解密 mydecrypt = AES.new(self.key, AES.MODE_CBC, text[:16]) decrypttext = mydecrypt.decrypt(text[16:]) decrypt_pwd = decrypttext.decode() # 解密后数据 return decrypt_pwd if __name__ == '__main__': password = "Ikongjian2017@360" API_SECRET = "c205b9ed6750bbabd38ba183cef79a79" # 点睛提供 text = create_md5_pwd(password) pc = PrpCrypt(API_SECRET) # 初始化密匙 ciphertext = pc.encrypt(text) e = b2a_hex(ciphertext)[32:].decode() d = pc.decrypt(ciphertext) print('加密后:' + e) print('解密后:' + d)
如何实现?
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。