from Crypto.Cipher import AES import base64 class EncryptAEC: def __init__(self, key='www.talebase.com'): self.key = key.encode('utf-8') def pkcs7padding(self, text): """ 明文使用PKCS7填充 """ bs = 16 padding_size = bs - len(text) % bs padding_text = chr(padding_size) * padding_size return text + padding_text def aes_encrypt(self, content): """ AES加密 """ cipher = AES.new(self.key, AES.MODE_ECB) # 处理明文,确保长度是16的倍数 content_padding = self.pkcs7padding(content) # 加密 encrypt_bytes = cipher.encrypt(content_padding.encode('utf-8')) # 重新编码 result = str(base64.b64encode(encrypt_bytes), encoding='utf-8') return result def aes_decrypt(self, content): """ AES解密 "
AEC加密解密
于 2022-10-11 15:38:54 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)