Typeerror: Incorrect padding

Asked

Viewed 928 times

3

Guys I’m trying to decrypt AES, only I’m facing the following mistake:

Traceback (Most recent call last):
File ". /teste2.py", line 190, in main()
File ". /teste2.py", line 186, in main Decrypt(password)
File ". /teste2.py", line 172, in Decrypt Dec = Base64.b64decode(crypt.Decrypt(password))
File "/usr/lib/python2.7/Base64.py", line 76, in b64decode raise Typeerror(msg)
Typeerror: Incorrect padding

This is my code:

def decrypt(password):
    iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
    crypt = AES.new(password, AES.MODE_CBC, iv)
    dec = base64.b64decode(crypt.decrypt(password))
    print "[+] Result: %s" % dec

2 answers

2


You are having an error saying that formatting your value in Base64 is incorrect - now, I find that hardly your decrypted password in the "AES" library would be in Base64 (it is at this return value that you try to apply Base64 decryption) -

Why the normal process for creating a symmetric password that will "travel" over the network is: (1) encrypt the password, (2) codifficult to encrypt this value in Base64 so that all characters are ASCII and digits or letters.

Note that you are doing things in reverse order in your role - trying to "decode" Base64 a value returned by the encryption library that was not Base64 to begin with - just try changing the base64.base64decode for base64.base64encode -

  • I have already solved this problem, but it returns the password to me like this: Ṃ. "-} R Y # Υ Ӵ A

  • Note that it is not using the "crypt" library (whatever: Unix, C, or PHP), simply the variable is called crypt. But your remark about Base64 is correct.

  • 1

    Thank you - corrected in the text.

  • @exploitsec: the password in this way that you demonstrate that the string contains non-printable characters. I don’t know if it was your idea to have it in "clear text" at that point (because of the call to Crypt) - if so, there is something wrong there. If you only want a printable version that can be serialized as text, use Base64.base64encode at that value

1

First, you are trying to use a random IV at the time of decrypt. IV must be random when encrypting, but to decrypt you must use the same IV used in the encryption (and therefore you must save IV next to ciphertext, otherwise you cannot decipher it).

Second, what exactly are you trying to decipher? I don’t know what library you’re using, but from what I understand from your code you’re using a field password as key (P.S. make sure that the type of data passed to the function - string or binary - is the same type expected by it) and soon after is trying to decrypt... itself password?! Your function should not receive a key and a cipher [and an IV] and use the key to decrypt the cipher?

Finally, there is the problem pointed out by jsbueno: if your encryption function works with binary data (as expected from a Python library) and your/IV cipher are in Base64, then you need to use the b64decode on the IV before passing it to AES.new and on the cipher before passing it to decrypt, and maybe take the final (binary) result and put it in a convenient format to be handled by python code (in what format was this data when it was encrypted? was another python code that encrypted, or was it another language/platform? and finally/IV cipher were actually encoded in Base64?).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.