If I were to AES-encrypt a file, and then ZLIB-compress it, would the compression be less efficient than if I first compressed and then encrypted?
In other words, should I compress first or encrypt first, or does it matter?
You should compress before encrypting. Encryption turns your data into high-entropy data, usually indistinguishable from a random stream. Compression relies on patterns in order to gain any size reduction.
First compress and then encrypt is best. However there is more to good encryption than just the order of encrypt and compress. Ideally you should use non-deterministic authenticated encryption. - encryption should take some random as input (next to a sectret key) so that it produces a different ciphertext every time.
Once encrypted no other than the intended receiver can decrypt it. Hence compression and encryption is done simultaneously.
Compress and then encrypt is better. Data compression removes redundant character strings in a file. So the compressed file has a more uniform distribution of characters. This also provides shorter plaintext and ciphertext, which reduces the time spent encrypting, decrypting and transmiting the file.
Compress first. Once you encrypt the file you will generate a stream of random data, which will be not be compressible. The compression process depends on finding compressible patterns in the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With