Introduction
Encryption is the process of encoding files in a manner that only authorized individuals can access them. Encryption does not, in itself, prevent interception, but it denies the file content to the interceptor. In an encryption scheme, the intended files, referred to as plaintext, are encrypted using an encryption algorithm, generating ciphertext that can only be read if decrypted.
Linux distributions provide a few standard encryption/decryption tools that can prove handy at times. In this article, we have covered three such tools with proper standard examples, which will help you encrypt, decrypt, and password-protect your files.
This guide is tested on the Ubuntu 22.04 OS template; However, it should work with our other Ubuntu and Debian templates.
1. GnuPG
The GnuPG (GNU Privacy Guard, often abbreviated as GPG) package is included by default in most of today’s Linux distributions. If it’s not installed, you can install it using apt or yum from the repository.
To install GnuPG:
sudo apt install gnupg
Now you can encrypt a file using GPG. As soon as you run the gpg command with option -c (encryption only with symmetric cipher) it will create a file testfile.txt.gpg.
gpg -c /path_to_the_file/testfile.txt
Note: Enter Paraphrase twice to encrypt the given file. You can just specify a different algorithm, optionally. To see all the encryption algorithms present, you may execute:
gpg --version
Output example:
Now, if you want to decrypt the above encrypted file, you may use the following command:
gpg /path_to_the_file/testfile.txt.gpg
Note: You must provide the same password you provided during encryption to decrypt when prompted.
More information about GNU Privacy Guard on the official site:
2. Zip
It is one of the most famous archive formats, and it is so well-known that we generally refer to archive files as zip files in day-to-day communication. It uses the PKZIP stream cipher algorithm. By default, it is installed on our OS templates. However, to make sure it is installed, use:
sudo apt install zip
Create an encrypted zip file using zip:
zip --password mypassword testarchive.zip testfile.txt
If you want to add more files to the zip archive:
zip --password mypassword testarchive.zip testfile.txt testfile1.txt testfile2.txt
Note. In the above example mypassword
is our password used for encryption?
To decrypt the file:
sudo apt install unzip
You need to provide the same password you provided at encryption.
2. OpenSSL
By default, it is installed on our OS templates. However, to make sure it is installed, use:
sudo apt install openssl
To encrypt:
openssl enc -aes-256-cbc -in /path_to_the_file/testfile.txt -out /path_to_the_file/testfile.dat
Explanation of each option used in the above command:
enc encryption
-aes-256-cbc the algorithm to be used;
-in full path of a file to be encrypted;
-out the full path where it will be decrypted.
To decrypt, use:
openssl enc -aes-256-cbc -d -in /path_to_the_file/testfile.dat > /path_to_the_file/testfile2.txt