When working with offsite backups and cloud storage, it's highly recommended to encrypt your data.
While encryption can be done with GPG and OpenSSL, I'll be using OpenSSL in this example.
First, let's create a unique random 64-character password which will be used to encrypt the files.
$ < /dev/urandom tr -dc A-Z-a-z-0-9 | head -c ${1:-64} > key.txt
I'd recommend to also save this key to your password manager (assuming you use one) just in case.
Files can be encrypted using this syntax.
$ openssl enc -e -aes256 -iter 8 -pass file:key.txt -in workingdir.tar.xz -out workingdir.tar.xz.enc
-e will encrypt
-d will decrypt
-aes256 is the encryption algorithm
-iter 8 means 8 iterations will be done
-pass file:key.txt will read the password from the file 'key.txt'
-in is the file currently residing on the system that should be encrypted
-out is the encrypted -in-file
When working with archives, the content that is supposed to be archived can be piped to OpenSSL and an encrypted archive can be created that way
$ tar -cJ workingdir | openssl enc -e -aes256 -iter 8 -pass file:key.txt -out workingdir.tar.xz.enc
Now that some files are encrypted them, you can decrypt them using OpenSSL again with almost the same command used to encrypt them
$ openssl enc -d -aes256 -iter 8 -pass file:key.txt -in workingdir.tar.xz.enc -out workingdir.tar.xz
or in the case of an archive
$ openssl enc -d -aes256 -iter 8 -pass file:key.txt -in workingdir.tar.xz.enc | tar -xJ -C Downloads/
Here's an example using text files
$ echo "test" | openssl enc -e -aes256 -iter 8 -pass file:key.txt -out file.txt.enc
When decrypting, the output can be written to a file
$ openssl enc -d -aes256 -iter 8 -pass file:key.txt -in file.txt.enc -out file.txt
or stdout
$ openssl enc -d -aes256 -iter 8 -pass file:key.txt -in file.txt.enc
test
As you can see it's fairly simple to encrypt files and archives in Linux using OpenSSL.
Feel free to comment and / or suggest a topic
Comments
Post a Comment