Manipulate encrypted file: AES / ECB / PKCS5Padding As part of a competition I want to manipulate (or decrypt) a file. I know, that the file contains gzip compressed text and is encrypted with AES / ECB / PKCS5.
I also have the plain text and the cipher text of that file. I don't know the secret key. What would be a strategy to manipulate this file, so that afterwards it could still be decrypted and decompressed with the same secret key and the plain text would be different from the original one. Is that possible at all?
A block cipher is a permutation of all possible plaintext blocks and ciphertext blocks. That is: each plaintext block has a corresponding ciphertext block and vice versa. Which one depends on the key and direction of operation (encryption or decryption).

Now ECB just results in a succession of the plaintext blocks converted to ciphertext blocks. There are two things to notice: if a ciphertext block is changed then decryption of that block will always result in a different plaintext block. However, because you don't know the key you don't know which one. So unless you can choose a ciphertext block for which you know (part of) the plaintext, the result will be indistinguishable from random.

If you do know (part of) the plaintext for a ciphertext block then you can use this knowledge to put this plaintext block anywhere in the ciphertext. So you can reshuffle, delete blocks, duplicate blocks and put in blocks for random plaintext as you wish. However, you should always keep to blocks, not individual bytes.

There is only one rule when doing this: the last block should be left alone otherwise the unpadding is likely to fail. You should only replace it with a ciphertext block with correct padding, if at all (you may have received a previous message encrypted using the same key with a known plaintext/padding block that you can reuse).



Note that you require authenticated encryption or encrypt-then-MAC (with MAC'ed or known IV) to avoid the problem of replacing plaintext. Just using CBC or CTR mode - for instance - doesn't protect the plaintext message in any meaningful way.