ValueError:AES键在python中必须是16、24或32字节长的错误

2024-09-27 23:21:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试加密字符串。但是得到这个错误:-你知道吗

ValueError: AES key must be either 16, 24, or 32 bytes long

from Crypto.Cipher import AES
import hashlib

key = 'E61EFD66173C8A58132B84E43566AB4826533F4483968BA58C9C4739FFD25FAD'
firstByte = key[0:32]
lastByte = key[32:64]

hexfirstByte = firstByte.encode()
hexlastByte = lastByte.encode()
# xorNumber = int(firstByte,2) ^ int(lastByte,2)
xorNumber = hex(int(firstByte,16) ^ int(lastByte,16))
hexNumber = bytes(xorNumber, 'utf-8')    
IV = 16 * '\x00'
mode = AES.MODE_CBC
encryptor = AES.new(hexNumber, mode, IV=IV)

text = 'j' * 64 + 'i' * 128
ciphertext = encryptor.encrypt(text)

decryptor = AES.new(hexNumber, mode, IV=IV)
plain = decryptor.decrypt(ciphertext)
print("decript" + str(plain))

Tags: keytextimportnewbytesmodeencodeint

热门问题