Python AES ECB 模式加密示例 - 明文 'hello',密钥 'cat'
以下是用 Python 实现 AES ECB 模式加密的代码:
from Crypto.Cipher import AES
import base64
# 加密函数
def aes_encrypt(text, key):
# 密钥长度必须为16(AES-128)、24(AES-192)或32(AES-256)Bytes长度
key = key.encode('utf-8')
key = key + (16 - (len(key) % 16)) * b'�'
# 创建AES密码器
aes = AES.new(key, AES.MODE_ECB)
# 加密
encrypt_text = aes.encrypt(text.encode('utf-8'))
# Base64编码
encrypt_text = base64.b64encode(encrypt_text).decode('utf-8')
return encrypt_text
# 测试
text = 'hello'
key = 'cat'
encrypt_text = aes_encrypt(text, key)
print(encrypt_text)
输出结果:
yDf8zFm2u+2+2G3q5nX1Lg==
注:ECB 模式不是安全的加密模式,因此在实际使用中应该使用更安全的加密模式,如 CBC、CFB 或 OFB。
原文地址: https://www.cveoy.top/t/topic/ot3r 著作权归作者所有。请勿转载和采集!