When compressing and encrypting, should I compress first, or encrypt first?

seisatsu picture seisatsu · Jan 13, 2011 · Viewed 36.9k times · Source

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?

Answer

Ferruccio picture Ferruccio · Jan 13, 2011

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.