import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
BLOCK_SIZE = 16
KEY = '' # 密钥
IV = '' # 偏移量
def aes_encrypt(text, timestamp=None, in_key=None):
"""
AES加密
:param text:
:return:
"""
print(f"传入的参数text= {text}, timestamp= {timestamp}, in_key= {in_key}")
text = bytes(str(text), 'utf-8')
print(f"密钥key的类型= {type(key)}")
if isinstance(key, str):
key = key.encode('utf-8')
print(f"转换后的密钥key的类型= {type(key)}")
cipher = AES.new(key, AES.MODE_CBC, IV)
# 加密
ciphertext = cipher.encrypt(pad(text, BLOCK_SIZE))
# 返回 base64 编码的密文字符串
b64_ciphertext = base64.b64encode(ciphertext).decode()
print(f"加密后参数= {b64_ciphertext}")
return b64_ciphertext
python-AES-CBC加密
于 2023-10-08 13:54:35 首次发布