I would like to compress data before encrypting and storing it in a MySQL DB to reduce especially bandwith requirements (compression reduces bandwith by 70-80%). At present, all is handled by a server-based script. In the future, the DB may also be accessible via a web interface.

Various comments on the insecurity of compression plaintext prior to encryption, related to security breaches such as CRIME or BREACH, make me unsure of how to do this. Also, some argue that compression prior to encryption is actually good security practice. I realize that attacks such as CRIME or BREACH are specific scenarios, but I am concerned that my application might be too close (or eventually come too close) to a similar scenario where compression may pose a security risk (DB contents can be stolen, server compromised etc). I am also not enough of an expert to fully understand all the aspects, details and nuances of CRIME or BREACH.

After reading this discussion on the topic, with the recommendation to pad the plaintext with a random nonce before compression, I wonder if the following implementation (in PHP) will safely do the job:

    $plaintext = gzcompress ($plaintext . openssl_random_pseudo_bytes (64));
    $cipher_text = openssl_encrypt ($plaintext, 'aes-256-cbc', $key, 0, $iv);

Data integrity is ensured through an HMAC signing / verification procedure on the cipher text and key.

Am I understanding the issue correctly?

Is this a safe implementation?

Would there be any benefit in the nonce being of random length, and then adding the length information to the end of the padding in order to remove the padding after decryption? Or should the length of the nonce be in relation to the length of the plaintext (which would typically be at least 512 bytes)?

  • 1
    $\begingroup$ Have you done some analysis to see just how much disk space this saves you? Disk space is so cheap that unless the savings were extremely large (say like an 80% reduction), I have to wonder if the added complexity is even worth it. $\endgroup$
    – mikeazo
    Oct 26, 2015 at 15:57
  • 1
    $\begingroup$ Well, it is not so much an issue of space as it is of bandwith, I edited the question accordingly. $\endgroup$
    – azren
    Oct 26, 2015 at 16:09
  • $\begingroup$ So the bandwidth you are concerned with is between an application server and a database server? Or will the encrypted data be sent to clients for decryption? $\endgroup$
    – otus
    Oct 27, 2015 at 11:34
  • $\begingroup$ @otus Yes between application and DB servers. $\endgroup$
    – azren
    Oct 27, 2015 at 19:13
  • $\begingroup$ I checked, compression reduces bandwith by 70-80%. An answer to my question was posted here: security.stackexchange.com/questions/103782/… $\endgroup$
    – azren
    Nov 3, 2015 at 11:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.