TypeError: argument of type 'int' is not iterable in AES Algorithm using Python

172 views Asked by At

I want to encrypt a text using AES algorithm. The code is given below.But when I run this code, I got the following error:

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

code :

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())

I want to encrypt the text with 128 bit key size , 128 bit block size , PKCS7 padding and in ECB mode.

Can anyone suggest a solution to solve this issue.

1

There are 1 answers

2
PieCot On

The problem in your code is the following lines:

AES.key_size=128
AES.block_size=128

because they overwrite the parameters of the AES module of the pyCryptodome library (look at the source code, lines 246 - 250).

If you remove them, the code works as expected, producing the following output:

4742782fa8cb2be804b91d25e815765d