Encrypt/decrypt files in GNU/Linux terminal using GPG
Sometimes you have files containing sensitive information that you don't want others to see without your permission. Also, you want to be sure that the encryption in place cannot be broken (at least not during your lifetime). That's where GPG comes into play. If you're on Windows, you can use an application called GPG4win. If you're on GNU/Linux, you need a package called gnupg2. You can install it with the following commands...
1 2 3 | sudo yum -y install gnupg2 # for CentOS/RHEL sudo dnf -y install gnupg2 # for Fedora sudo apt install gnupg2 # for Ubuntu |
To encrypt a group of files, it's best to first create an archive of those files to "merge" all files into one so you can apply the encryption easily.
1 | tar czf your-files.tar.gz file1.txt file2.txt file3.txt |
After that, you can encrypt the archive using the AES 256-bit encryption algorithm by executing...
1 | gpg2 --cipher-algo aes256 -o encrypted-file.tar.gz.gpg -c your-files.tar.gz |
To decrypt a gpg2-encrypted file, use the following command (you will be prompted to enter a decryption password)...
1 | gpg2 -o decrypted-file.tar.gz -d encrypted-file.tar.gz.gpg |