Can I compress an encrypted file?
You can compress it, but it is unlikely to save much disk space. By its nature, encryption rarely leaves a file compressible by much.
Try it for yourself to see if there is any file size savings.
One data point:
-rw-r----- 1 gene gene 2428671 2009-06-02 12:39 test.log
-rw-r----- 1 gene gene 134524 2009-06-02 12:39 test.log.bz2
-rw-r----- 1 gene gene 217162 2009-06-02 12:38 test.log.gz
-rw-r--r-- 1 gene gene 263229 2009-06-02 12:47 test-AES.gpg
-rw-r--r-- 1 gene gene 264833 2009-06-02 12:42 test-AES.gpg.bz2
-rw-r--r-- 1 gene gene 263302 2009-06-02 12:41 test-AES.gpg.gz
-rw-r--r-- 1 gene gene 134609 2009-06-02 12:43 test-bz2-AES.gpg
-rw-r--r-- 1 gene gene 217246 2009-06-02 12:43 test-gz-AES.gpg
test.log
is the original, and test.log.bz2
and test.log.gz
are simply compressed with bzip2 and gzip, respectively.
If I encrypt it (gpg --symmetric --cipher-algo AES --output test-AES.gpg test.log
) the encrypted file (test-AES.gpg
) is slightly larger than compressed versions. Compressing the encrypted file actually adds a little size (test-AES.gpg.bz2
and test-AES.gpg.gz
).
Compressing first then encrypting does show some savings (test-bz2-AES.gpg
and test-gz-AES.gpg
), especially with bzip2.
Of course, your experience may differ given different encryption software and/or different compression software.
You should consider whether the file size savings you get simply via encryption is enough, or if compressing then encrypting is worth the extra step in the process.
Not if the encryption is any good. Compression deals with recognizing patterns in data and creating a "shorthand" that refers to those patterns for later extraction.
If your encryption is good, the file looks like random noise, and that's not going to compress very much due to the absence of patterns. Of course you can put it in to an archive file (.zip, .gz, etc.), but you aren't likely to make it get much smaller.
Compression programs don't modify the actual data in any way - if they did, they would be useless. (Sound and image compression is an exception, as the human's eye doesn't see such small changes, while a computer can choke on a single flipped bit.) So yes, you can compress encrypted files.
But since encrypted data is very similar to random data, it doesn't compress very well - so if you can, compress before encrypting. Otherwise the "compression" will be fairly useless.
For a compression program, Unix world prefers tar
along with gzip
/bzip2
(usually used from within tar, as in tar czf foo.tar.gz foo
), while Windows users prefer ZIP, RAR or 7z.
Using any compression program (7z, zip, gzip, bzip2) is lossless and does not affect your ability to decrypt the data.
However, due to the nature of the encrypted data, you will probably not gain much from it.
The proper thing to do is compress it before the encryption step. Existing utilities such as gpg do this. The behaviour to compress before encryption is the default:
michael:~> dd if=/dev/zero of=testfile bs=1048576 count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00300552 s, 349 MB/s
michael:~> gpg --symmetric --cipher-algo aes --batch --passphrase cheesestring testfile
michael:~> ls -al testfile testfile.gpg
-rw-r--r-- 1 michael users 1048576 2009-06-02 12:42 testfile
-rw-r--r-- 1 michael users 1123 2009-06-02 12:43 testfile.gpg
An encrypted file will lose the statistical properties that make compression work, so compressing the encrypted file will save little if any space. You should compress the file first (while it still behaves in a way that compresses well) before encrypting the compressed file. Aside from that, the compression will not affect the original content of the file when you come to uncompress it.