import base64
|
from Crypto.Cipher import AES
|
|
def aes_encrypt(word: str, key_str: str, iv_str: str) -> str:
|
# 将密钥和IV转换为UTF-8编码的字节
|
key = key_str.encode('utf-8')
|
iv = iv_str.encode('utf-8')
|
|
# 将明文转换为UTF-8编码的字节
|
data = word.encode('utf-8')
|
|
# 计算ZeroPadding填充
|
block_size = AES.block_size
|
padded_len = (len(data) + block_size - 1) // block_size * block_size
|
padding = padded_len - len(data)
|
padded_data = data + bytes([0] * padding)
|
|
# 创建AES-CBC加密器
|
cipher = AES.new(key, AES.MODE_CBC, iv)
|
|
# 执行加密
|
encrypted_data = cipher.encrypt(padded_data)
|
|
# 返回Base64编码的密文
|
return base64.b64encode(encrypted_data).decode('utf-8')
|