This question already has an answer here:

This may be a basic question but bear with me.

I started encrypting a text with aes 256 cbc mode generating a random iv of 16 bytes and a key.

text = 'hi I am learning encryption.';

Then I created another random iv and tried to decrypt the encrypted text. ( I know we are supposed to use the same iv we used for encryption to decrypt the ciphered text).

The result came as

(some rubbish text) encryption.

Then I padded the main text with 16 random characters, encrypted it and decrypted it I get my actual text padded with some junk text at the start.

I am unable to figure out why it messes with the first 16 bytes of text and then successfully extracts the remaining.

Can you explain?


marked as duplicate by otus, Ella Rose, Community Sep 26 '18 at 18:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Here is the picture for CBC decryption ($\oplus$ is the XOR operation of each bit/byte at the same position of the input blocks):

enter image description here

As you can see only the first block is altered by the IV.

During encryption every block is changed because the ciphertext will propagate the uniqueness of the IV over all subsequent blocks. However, during decryption only the first block is XOR'ed with the IV to get back the first block of plaintext.

If that block is random then the result will also be random. The rest of the blocks is unaffected.

If you change just a single bit of the original IV, then only that particular bit in the plaintext block will be affected.

  • $\begingroup$ Thank you. It actually makes good sense now. I guess I hadn't understood the encryption step better. I missed the fact that during encryption only first block is XOR-ed with IV. $\endgroup$ – Suman Lama Sep 25 '18 at 22:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.