Take the 2-minute tour ×

I have the assumption there is no added protection at all.

share|improve this question

6 Answers 6

up vote 9 down vote accepted

There is no difference in the security provided, but because of the way compression algorithms work, you are probably going to get better compression if you compress first then encrypt.

Compression algorithms exploit statistical redundancies (such as those that exist in natural language or in many file formats) in the data which should be eliminated when you encrypt it, therefore an encrypted message shouldn't be able to be compressed all that well.

From the wikipedia article:

However, lossless data compression algorithms will always fail to compress some files; indeed, any compression algorithm will necessarily fail to compress any data containing no discernible patterns. Attempts to compress data that has been compressed already will therefore usually result in an expansion, as will attempts to compress all but the most trivially encrypted data.

share|improve this answer
Hmm, so if it's a lossless compression then it's not worth compressing first? –  john Dec 9 '10 at 15:49
@john: no, if it's lossless compression then it is not worth compressing after. –  Reese Moore Dec 9 '10 at 15:52
Ahh, the patterns inserted by lossless compression should be hidden by the encryption-- so best to do compression first. –  john Dec 9 '10 at 16:01
Incidentally, compression will also reduce the size of the plaintext - this may be an infinitesimal increase in security. –  Piskvor Dec 9 '10 at 16:10
Compression before encryption can help against some attacks. In particular a known plaintext attack where the plaintext that is known is within other unknown plaintext such that the compressed version of the known text is affected by the unknown plaintext. –  Slartibartfast Dec 10 '10 at 20:05

Encryption works better on short messages, with a uniform distribution of symbols. Compression replaces a message with a non-uniform distribution of symbols by another, shorter sequence of symbols that are more uniformly distributed.

Therefore, it's mathemathically safer to compress before encryption. Compression after encryption doesn't affect the encryption, which remains relatively weak due to the non-uniform distribution of plaintext.

Of course, if you use anything like AES256, and the NSA isn't after you, this is all theory.

share|improve this answer
Why is it mathematically safer to compress before encryption? Could you site your source, please? My understanding has always been that for a good encryption algorithm, such as AES, you cannot distinguish ciphertext from random data. –  Steve Jan 22 '13 at 14:41
@Steve: Modern attacks against cyphers assume that you have at least some idea of the possible plaintexts. Since well-compressed data is also indistinguishable from random data, it makes it hard to impossible to validate whether you made any progress on breaking the key. And if you don't know whether you made any progress, you have to revert to brute-forcing the decryption. In the extreme, when you're encrypting truly random data with a perfect encryption algorithm, there is no way to determine whether any key is correct. This is for instance trivially true with a One Time Pad. –  MSalters Jan 22 '13 at 15:22
But compressed data is not indistinguishable from random. If you assume the attacker knows the plaintext, or can submit the plaintext for compression & encryption, the attacker also work out the compressed version of the plaintext. There is no additional randomness (or security) added by compressing the data first. In fact, compressing truly random data would give an attacker some idea of the data because they can start looking for compression headers. –  Steve Jan 22 '13 at 15:37

There is no added security (as compression is not a security mechanism), but a properly encrypted message shouldn't be easily compressible (i.e. rule of thumb: if you can significantly compress an encrypted message, something is wrong).

Therefore, compress then encrypt.

share|improve this answer

There is no difference in security provided.

share|improve this answer
Thanks, I can think of a few reasons why compressing first would be a wiser choice. I just wanted to make sure I wasn't affecting the security. Thanks again!! –  john Dec 9 '10 at 15:27

Look here: Super User thread about compression && encryption or the other way around

They have a complete and detailed answer to your question (witch is compress then encrypt, by the way).

share|improve this answer

Yep, there should be no difference in the security provided.

share|improve this answer

Your Answer


