Loading... 安装包`pip install pycryptodome` ```python from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad from base64 import b64encode, b64decode import binascii # 密钥(str 格式) key_str = "116c3559614f2271" # 转换密钥为 Hex 格式 key_hex = bytes.fromhex("31313663333535393631346632323731") # 模式与填充:AES/ECB/PKCS5Padding def aes_encrypt_ecb(data, key): cipher = AES.new(key, AES.MODE_ECB) padded_data = pad(data.encode('utf-8'), AES.block_size) # PKCS5Padding encrypted = cipher.encrypt(padded_data) return b64encode(encrypted).decode('utf-8') # 转 base64 输出 def aes_decrypt_ecb(data, key): cipher = AES.new(key, AES.MODE_ECB) decoded_data = b64decode(data) decrypted = unpad(cipher.decrypt(decoded_data), AES.block_size) # 去填充 return decrypted.decode('utf-8') # 输入参数 plaintext = "4f22710ebd88081f28759b426fe731541a8618caf81f23116c355961" # 使用字符串密钥进行加密 encrypted_str_key = aes_encrypt_ecb(plaintext, key_str.encode('utf-8')) print(f"Encrypted with str key: {encrypted_str_key}") # 使用 Hex 密钥进行加密 encrypted_hex_key = aes_encrypt_ecb(plaintext, key_hex) print(f"Encrypted with hex key: {encrypted_hex_key}") # 示例解密 decrypted_hex_key = aes_decrypt_ecb(encrypted_hex_key, key_hex) print(f"Decrypted with hex key: {decrypted_hex_key}") ``` 最后修改:2025 年 01 月 04 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏