If you've ever needed basic command line encryption that is likely to work on most Linux shells, then you may have been looking for the old trusty openssl
command.
Sure there are many other tools out there, and many of them may be better suited, but openssl
is likely to be installed on pretty much every Linux host you'll meet, and often most *NIX hosts too.
OpenSSL isn't just for x509 certificates.
Here is a skull
text file that I want to encrypt with a simple passphrase:
nicolaw@linux:~$ cat skull
uuuuuuu
uu$$$$$$$$$$$uu
uu$$$$$$$$$$$$$$$$$uu
u$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$" "$$$" "$$$$$$u
"$$$$" u$u $$$$"
$$$u u$u u$$$
$$$u u$$$u u$$$
"$$$$uu$$$ $$$uu$$$$"
"$$$$$$$" "$$$$$$$"
u$$$$$$$u$$$$$$$u
u$"$"$"$"$"$"$u
uuu $$u$ $ $ $ $u$$ uuu
u$$$$ $$$$$u$u$u$$$ u$$$$
$$$$$uu "$$$$$$$$$" uu$$$$$$
u$$$$$$$$$$$uu """"" uuuu$$$$$$$$$$
$$$$"""$$$$$$$$$$uuu uu$$$$$$$$$"""$$$"
""" ""$$$$$$$$$$$uu ""$"""
uuuu ""$$$$$$$$$$uuu
u$$$uuu$$$$$$$$$uu ""$$$$$$$$$$$uuu$$$
$$$$$$$$$$"""" ""$$$$$$$$$$$"
"$$$$$" ""$$$$""
$$$" $$$$"
I can encrypt and compress it like so:
nicolaw@linux:~$ openssl enc -aes-256-cbc -e -a -in skull -o skull.enc.b64
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
nicolaw@linux:~$ cat skull.enc.b64
/Td6WFoAAATm1rRGAgAhARwAAAAQz1jMAQOvU2FsdGVkX19aXCabwKNmzxVnjuPFkMRMcJ9MPvWl
hzAsIYmFcpzN8+7/EZADZszqTK5gyWoB4WHdKkTtGQ4fPw0OIQHRdYnIzBQJm37PHK5veWfWQU3x
sSUODnw+o3g68pmA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G7aENMb+NU2pycmydknyqvNa2l9qzbEZ9IgEGUVDFZ8XU5Eu066GoV1ihAvTH
FT0A5Aj+GJMhPFUAAcgHsAcAAAvM85SxxGf7AgAAAAAEWVo=
And decrupt it thus:
nicolaw@linux:~$ openssl enc -aes-256-cbc -d -in skull.enc.b64
enter aes-256-cbc decryption password:
uuuuuuu
uu$$$$$$$$$$$uu
uu$$$$$$$$$$$$$$$$$uu
u$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$$$$$$$$$$$$$$$$$$$$u
u$$$$$$" "$$$" "$$$$$$u
"$$$$" u$u $$$$"
$$$u u$u u$$$
$$$u u$$$u u$$$
"$$$$uu$$$ $$$uu$$$$"
"$$$$$$$" "$$$$$$$"
u$$$$$$$u$$$$$$$u
u$"$"$"$"$"$"$u
uuu $$u$ $ $ $ $u$$ uuu
u$$$$ $$$$$u$u$u$$$ u$$$$
$$$$$uu "$$$$$$$$$" uu$$$$$$
u$$$$$$$$$$$uu """"" uuuu$$$$$$$$$$
$$$$"""$$$$$$$$$$uuu uu$$$$$$$$$"""$$$"
""" ""$$$$$$$$$$$uu ""$"""
uuuu ""$$$$$$$$$$uuu
u$$$uuu$$$$$$$$$uu ""$$$$$$$$$$$uuu$$$
$$$$$$$$$$"""" ""$$$$$$$$$$$"
"$$$$$" ""$$$$""
$$$" $$$$"
nicolaw@linux:~$
The -a
argument (which performs an automatic base64 encode or decode) is of course optional, but is almost certianly what you want if you're going to consider embedding any encrypted data in shell scripts.
nicolaw@linux:~$ openssl enc -aes-256-cbc -e -in skull -a > skull.enc.b64
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
nicolaw@linux:~$ openssl enc -aes-256-cbc -e -in skull > skull.enc
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
nicolaw@linux:~$ ls -al *skull*
-rw-rw-r-- 1 nicolaw nicolaw 917 Dec 18 18:28 skull
-rw-rw-r-- 1 nicolaw nicolaw 944 Dec 18 18:41 skull.enc
-rw-rw-r-- 1 nicolaw nicolaw 1280 Dec 18 18:39 skull.enc.b64
nicolaw@linux:~$
Other related tools that you might wish to Google for are: gnupg
, bcrypt
, ccrypt
, zip
and 7zip
.