DEV Community

Should you encrypt or compress first?

Max Veytsman on January 17, 2017

Imagine this: You work for a big company. Your job is pretty boring. Frankly, your talents are wasted writing boilerplate code for an application ...
Collapse
 
daniel15 profile image
Daniel Lo Nigro

So is the solution that you should never compress when you're encrypting? Compressing before encrypting results in the vulnerabilities discussed in this post, while compressing after encrypting doesn't result in significant savings due to the entropy of the encrypted data.

I guess you could compress data, then pad it out to a fixed length (or multiple of a fixed length), then encrypt it?

Collapse
 
neilmadden profile image
Neil Madden

I asked basically the same question on crypto.stackexchange a little while ago: crypto.stackexchange.com/questions... The replies contain some useful details.

Collapse
 
daniel15 profile image
Daniel Lo Nigro

Thanks, this is useful information!

Collapse
 
mveytsman profile image
Max Veytsman Appcanary

Honestly, I don't have a universal answer here.

I wrote this article because I kept hearing compress then encrypt as a universal answer, and wanted to point out that it can lead to data leakage.

To muddle the point even further, since writing this article I've found that there's research about encrypting first and still actually compressing (see pdfs.semanticscholar.org/372d/0dae... )

I think the answer is that you have to be aware of this when designing systems that encrypt and/or compress data...

Collapse
 
tbroyer profile image
Thomas Broyer

Not sure you can conclude that: http/2 dropped gzip (or whatever it was) compression after CRIME, but replaced it with another compression algorithm (HPACK). I don't know (or have forgotten) the details of what makes it more secure though.

Collapse
 
bgadrian profile image
Adrian B.G.

I don't care if this is real,write a book of short stories. You'll win.

Collapse
 
courier10pt profile image
Bob van Hoove

My first guess was to encrypt before you compress because your want to keep signal / noise as low as possible. But then I didn't think about the effect on compression :)