Skip to content
Advertisement

Crypto module and Encryption method not working

Basically, my question is divided into two problems, first of all, I am having this problem when I try running this code [code below] and get an error, like this:

JavaScript

The second problem is when I enter text, i.e: “My data is here” I got the encrypted text as : “GIdd+zxj8m0nMeh7wmZJ+Q==” but reversing the process in decryption it outputs a different text, to validate the process I am using and checking through https://aesencryption.net/ as reference in my work.
My Code:

JavaScript

Advertisement

Answer

In the __pad() method ** is to be replaced by *:

JavaScript

In the __unpad() method the padding size is to be determined from the last byte:

JavaScript

With these changes the code can be executed successfully with PyCryptodome, see also here for the installation.


Padding does not need to be implemented explicitly. PyCryptodome supports various paddings (including PKCS7) in a dedicated module (Crypto.Util.Padding).


Please note that deriving the key from a password with a digest is generally insecure. It is more secure to use a reliable key derivation function such as Argon2 or PBKDF2.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement