Here's a few ways to do this. One thing to note is that if you're going to use separate compression and encryption tools you should always compress before encryption, since encrypted data is essentially non-compressible.
These examples compress and encrypt a file called clear_text
.
Using gpg
$ gpg -c clear_text #Compress & Encrypt
$ gpg -d clear_text.gpg #Decrypt & Decompress
gpg will compress the input file before encryption by default, -c
means to use symmetric encryption with a password. The output file will be clear_text.gpg
. One benefit of using gpg
is that is uses standard OpenPGP formats, so any encryption software that supports OpenPGP will be able to decrypt it.
Using mcrypt
$ mcrypt -z clear_text #Compress & Encrypt
$ mdecrypt -z clear_text.gz.nc #Decrypt & Decompress
The -z
option compresses. By default this outputs a file called clear_text.gz.nc
.
Using bcrypt
$ bcrypt -r clear_text #Compress & Encrypt
$ bcrypt -r clear_text.bfe #Decrypt & Decompress
bcrypt compresses before encrypting by default, the -r
option is so that the input file isn't deleted in the process. The output file is called clear_text.bfe
by default.
Using gzip
and aespipe
$ cat clear_text | gzip | aespipe > clear_text.gz.aes #Compress & Encrypt
$ cat clear_text.gz.aes | aespipe -d | gunzip > clear_text #Decrypt & Decompress
aespipe is what it sounds like, a program that takes input on stdin and outputs aes encrypted data on stdout. It doesn't support compression, so you can pipe the input through gzip first. Since the output goes to stdout you'll have to redirect it to a file with a name of your own choosing. Probably not the most effective way to do what you're asking but aespipe is a versatile tool so I thought it was worth mentioning.
I can't seem to run this on a mac. Is this different in anyway? – eleijonmarck – 2016-12-20T20:46:03.407
3@eleijonmarck provide the part "does not work because <insert-error-message-here>"… – akira – 2016-12-21T08:34:11.220
Doesn't work for me between Ubuntu 16.04 and Ubuntu 18.04 machine. Error: "bad decrypt" (and warnings about deprecated key derivation and hints to user -iter or -pbkdf2). Decrypting on the same (16.04) machine works. – Enno Gröper – 2019-08-08T06:59:34.023
@EnnoGröper: i provided 2 ways to encrypt / decrypt. also: provide the used software versions if you expect anyone to solve that issue of yours. – akira – 2019-08-13T19:03:52.810
26For anyone wondering how to decrypt the file with openssl:
openssl aes-256-cbc -d -in out.tar.gz.enc -out decrypted.tar.gz
– ndbroadbent – 2013-01-28T22:03:22.2831@nathan.f77 that command also shows how to do things without piping them into openssl.
openssl enc -aes-256-cbc -e -in foo.tar.gz -out bar.tar.gz.enc
– Keith Smiley – 2014-03-06T23:48:57.0773@KeithSmiley if you have large archives and not a lot of space (like it could be on a VPS) it's more space-efficient to pipe. – Andrew Savinykh – 2014-06-02T23:15:52.343