# Alter plaintext through IV in block cipher AES in CBC mode

IV: 69bb375aa919c72aa561dd7b7ffacf66

Ciphertext: "c28ea025d10186510855d083747d6435c808d9857075abd9bfc54b094e60eaf3"

Plaintext: "SEND ENCRYPTED DATA"

I want to be able to change the resulting Plaintext to "SEND THE DATA", which if I understand correctly would involve altering the IV in this case.

below the encryption (at the server side- we were given the code), includes the padding used,

  def encrypt(plaintext):
log.info("[%d] encrypt data command invoked" % (client_id))

        # add padding
if (len(plaintext) % 16 != 0):
plaintext += b"_" * (16 - len(plaintext) % 16)

cipher = AES.new(shared_key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(plaintext)

return hexlify(iv) + b"," + hexlify(ciphertext) + b"\n"

def no_tranform(plaintext):
log.info("[%d] send data command invoked" % (client_id))
return hexlify(plaintext) + b"\n"

commands = {"SEND ENCRYPTED DATA": encrypt,
"SEND THE DATA": no_tranform}


Also you can see that the only allowed commands are "SEND ENCRYPTED DATA" and "SEND THE DATA" and I would like the latter.

• Some more details would be welcome. Exactly what have you tried, and what kind of "troubles" have you had? – Ilmari Karonen Feb 10 '19 at 2:58
• Also, does the modified plaintext have to be exactly "SEND THE DATA"? Because it would be easier to do this if you didn't have to change the length of the plaintext. (You can probably still do it in this case anyway, but the details will depend on the padding scheme used.) – Ilmari Karonen Feb 10 '19 at 3:02
• @llmari Karonen please check my new edit, thank you – nachofest Feb 10 '19 at 12:29
• This is off-topic here. programming Questions belongs to SO. – kelalaka Feb 10 '19 at 12:33
• @kelalaka: I would say that this question can be interpreted and answered in a way that is on-topic here (i.e. the answer does not necessarily need to include any code). Whether such an answer would actually help the OP is still somewhat unclear to me. – Ilmari Karonen Feb 10 '19 at 15:49

Each bit you flipped in the IV changes the same bit in the decrypted plaintext. So if you want to change the first E character of ENCRYPTED into a T of THE then you need to XOR the binary representation of E and T (probably ASCII) and XOR that with the byte in the IV. This will of course only work for the first block of 16 bytes. Fortunately "SEND THE DATA" is small enough for that (13 bytes if I count correctly).
That leaves the problem of the padding. The original block contains the padding in the second block because of the size (19 bytes). However, you just have to adjust the padding of the first block by performing the XOR as displayed above. Instead of XOR'ing the final 16 - 3 = 3 plaintext bytes with a letter you XOR them with the value 03 in hex and XOR that with the IV. Now you've also generated the required padding.