-1

I want to use AES encryption to encrypt my data I have done it before but in my laptop the method is not working and giving me same error is there any way to fix it ? Here is the code :

from Crypto.Cipher import AES
def paddedKey(key):
    while len(key)%8!=0:
        key +=' '
    return key
def paddingText(text):
    while len(text)%16!=0:
        text +=' '
    return text
data = paddingText(input('Enter text to encrypt - '))
key = paddedKey(input('Enter key between 16-32 charachters - '))
if(len(key)<=16 & len(key)>=32):
    print('Key must me between 16 and 32 charachters')
cipher = AES.new(key)
ciphertext = cipher.encrypt(data)
# print(ciphertext)
# print(ciphertext.decode('cp855'))
print('Encrypted text = ',ciphertext)

But this code is giving me error : Traceback (most recent call last): File "C:\Users\shati\Desktop\Python\Research\AES_Extended.py", line 18, in cipher = AES.new(key) TypeError: new() missing 1 required positional argument: 'mode'

I am using pythone version : Python 3.6.8
Pip version: pip 20.1.1
PycryptoDome version : pycryptodome 3.9.8

2 Answers2

1

pycrypto and pycryptodome collide in using the same module name (Crypto), but pip allows you to install both of them at the same time.

I had installed pycrypto, which does not require the argument mode, but some other library I installed required pycrptodome, and it got installed without my knowledge, and then I had the same error described in the question.

augustomen
  • 8,977
  • 3
  • 43
  • 63
  • 1
    Found out our development server was using pycrypto and our production server was running pycrptodome, and I had a hard time troubleshooting the error until I found your post. Thanks! – Malachi Bazar Jan 15 '22 at 19:41
0

It's obvious from error, the new method needs two parameters.

Check this Encrypt & Decrypt using PyCrypto AES 256

You have to pass the mode parameter to the AES.new function.

tausif
  • 672
  • 1
  • 6
  • 15
  • I used this video as reference and it worked before. After installing new windows in laptop it's not working. https://www.youtube.com/watch?v=i0fSS6MoNV8&feature=youtu.be&fbclid=IwAR037kNRB2zmr1pSS9NsZ5wcJVXAZFQXtjpho8Myo-0eHcLEgPO15excSbs – Moshiuzzaman4135 Jul 14 '20 at 07:10