TypeError:在使用Python的AES算法中,类型'int'的参数不可迭代

gstyhher  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(176)

我想用AES算法加密一个文本。代码如下所示。但是当我运行这段代码时,我得到了以下错误:

Traceback (most recent call last):
  File "/home/a/AES/aes.py", line 159, in <module>
cipher = AES.new(key.encode('utf8'), AES.MODE_ECB)
  File "/home/a/anaconda3/envs/AES/lib/python3.9/site-packages/Crypto/Cipher/AES.py", line 232, in new
return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
  File "/home/a/anaconda3/envs/AES/lib/python3.9/site-packages/Crypto/Cipher/__init__.py", line 79, in _create_cipher
return modes[mode](factory, **kwargs)
  File "/home/a/anaconda3/envs/AES/lib/python3.9/site-packages/Crypto/Cipher/_mode_ecb.py", line 216, in _create_ecb_cipher
cipher_state = factory._create_base_cipher(kwargs)
  File "/home/a/anaconda3/envs/AES/lib/python3.9/site-packages/Crypto/Cipher/AES.py", line 92, in _create_base_cipher
if len(key) not in key_size:
TypeError: argument of type 'int' is not iterable

密码:

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

key = 'QwroeApp90652321'
AES.key_size=128
AES.block_size=128
plaintext = "wdrdooloo"
cipher = AES.new(key.encode('utf8'), AES.MODE_ECB)
msg =cipher.encrypt(pad(plaintext.encode('utf8'), 16))
print(msg.hex())

我想用128位密钥大小、128位块大小、PKCS7填充和ECB模式加密文本。
有谁能提出一个解决这个问题的方案吗?

yjghlzjz

yjghlzjz1#

代码中的问题是以下几行:

AES.key_size=128
AES.block_size=128

因为它们覆盖了pyCryptodome库的AES模块的参数(请查看源代码,第246 - 250行)。
如果您移除它们,程式码会如预期般运作,产生下列输出:

4742782fa8cb2be804b91d25e815765d

相关问题