There is often a need to encrypt and/or password protect archive files. Whether you are using them to backup data or share it across the internet, you should take the necessary steps to protect your data. In this quick tip we will examine three ways to create an encrypted and password protected archive in Linux. We will also briefly discuss some pros and cons of each method.

Using tar and gpg to create a secured tar archive.This is the most secure way of creating an encrypted / password protected compressed archive, it is also one of the more complicated. We will use the to create an archive and pipe it to the gpg command for encryption and password protection. This example specifies the AES-256 encryption algorithm. Tar czvpf - file1.txt file2.pdf file3.jpg gpg -symmetric -cipher-algo aes256 -o myarchive.tar.gz.gpgAfter entering the above command you will be prompted for a passphrase.After entering the passphrase you will be asked to repeat it.
Then the archive will be created as an encrypted archive, using a secure algorithm and protected by your custom passphrase. Gpg -d myarchive.tar.gz.gpg tar xzvf -You will be prompted for the passphrase before the archive is extracted.I like to always name these types of archives.tar.gz.gpg so I know how they were created. For this example we used tar, gzip and gpg. Also, it is important that you DO NOT forget the passphrase.
If you do, there is no way to recover the data. Use 7zip to create zip format archives with secure algorithmsThis is just as secure as the first option since it supports the same AES-256 encryption algorithm, although it does require you put the passphrase or “secret” on the command line, which I am not a fan of. It is also not as convenient because most systems do not come with the P7zip package installed.

To install P7zip on Red Hat, or RH variants like CentOS or Fedora: sudo yum -y install p7zipor sudo dnf -y install p7zipOn Debian based systems such as Ubuntu: sudo apt-get install p7zip-fullTo create the archive, use the command below, replace “PASSPHRASE” with your own secret passphrase. Simply add the –password option to the zip command like so: zip -password PASSPHRASE myarchive.zip file1.txt file2.pdf file3.jpgRemember to replace PASSPHRASE with your password.Example output: $ zip -password PASSPHRASE myarchive.zip file1.txt file2.pdf file3.jpgadding: file1.txt (deflated 75%)adding: file2.pdf (deflated 7%)adding: file3.jpg (deflated 4%)To extract the archive, use the normal unzip utility. The only difference is you will be asked for a password. $ unzip myarchive.zipArchive: myarchive.zipmyarchive.zip password:inflating: file1.txtinflating: file2.pdfinflating: file3.jpg ConclusionSo there you have my three favorite ways to created encrypted archives. There are plenty more ways to accomplish this (openssl, gpg-zip, bcrypt) and some are better than others. If you data is really important, I suggest you read up on the different algorithms and signing methods that are out there and decide for yourself which is right.Whatever method you use it is important to NOT forget your passphrase.