TypeError: Incorrect padding

2

I am trying to decrypt AES, but I am encountering the following error:

  

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

Here's 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
    
asked by anonymous 14.03.2016 / 00:18

2 answers

2

You're having an error saying that the formatting of your value in base64 is incorrect - now, I think your password can not be decrypted in the "AES" library in base64 (it's in this return value that you try to apply base64 decoding ) -

Why the normal process for creating a symmetric password that will "travel" over the network is: (1) encrypt the password, (2) co decode this encrypted base64 value so that everyone the characters are ASCII and digits or letters.

Note that you are doing things in reverse order in your function - trying to "decode" from base64 a value returned by the non-base64 encryption library to begin with - just try changing base64.base64decode to base64.base64encode -

    
15.03.2016 / 04:54
1

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

Second, what exactly are you trying to decipher? I do not know what library you are using, but from what I understand of your code you are using a password field as a key (PS make sure the data type passed to the function - string or binary - is the same type expected by it ) and then you're trying to decrypt ... password itself ?! Should your function not receive a key and a cipher [and an IV] and use the key to decrypt the cipher?

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

    
16.03.2016 / 02:01