Search code examples
aespycrypto

AES decryption using pycrypto


As a self study exercise, I'm trying to learn how to use some of the pycrypto library. I need to decrypt a ciphertext string in CBC_MODE using AES. I the ciphertext, key, and IV are all given. Here is the code that I have written:

from Crypto.Cipher import AES

mode = AES.MODE_CBC
key = "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1"
ciphertext = "a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1";

iv = ciphertext[:32] 
ciphertext = ciphertext[32:] 
decryptor = AES.new(key, mode, iv)
plaintext = decryptor.decrypt(ciphertext)
print plaintext

When I run this, I get the following error:

ValueError: IV must be 16 bytes long

I know that the IV string is 32 hex characters, and therefore 16 bytes. I think that this might be a typing problem, but I don't know how to correct it. Can anyone help?

Thank you!


Solution

  • Your strings contain only hex characters, but they are still plain strings, so every character counts.

    So your IV string is 32 byte long as you sliced it out from ciphertext.