1

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!

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I suspect you're right and it is down to typing. Try one of these:

iv = binascii.unhexlify(ciphertext[:32])

or

iv = long(ciphertext[:32], 16)

Comments

0

Tell the computer you are dealing with hex. It is treating it as a string. iv = iv.decode('hex');

1 Comment

It would help to explain how to "tell the computer" he is using hex.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.