hyb
2025-12-23 7e5db3a16b423ec4a43459805e277979bcac7db5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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')