Moshiuzzaman4135
Moshiuzzaman4135

Reputation: 11

Getting Exception:TypeError: new() missing 1 required positional argument: 'mode'

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

Upvotes: 0

Views: 7106

Answers (3)

andrewdotn
andrewdotn

Reputation: 34873

The old pycrypto library had a default mode of MODE_ECB. But that library is no longer maintained.

The newer pycryptodome fork of that library is almost completely compatible, but this is one edge case where it is not 100% compatible.

You need to change old code that created a new AES object without specifying a mode:

Crypto.Cipher.AES.new(key).decrypt(encrypted)

to now explictly specify that mode that used to be the default:

Crypto.Cipher.AES.new(kek, Crypto.Cipher.AES.MODE_ECB).decrypt(encrypted)

Upvotes: 0

augustomen
augustomen

Reputation: 9759

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.

Upvotes: 1

tausif
tausif

Reputation: 712

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.

Upvotes: 1

Related Questions