Today we’re going to encrypt a file or directory using Gpg tool which can be installed in any Linux version. If you are really concerned about your privacy and you don’t want your friends to sneak into your laptop or files you can use strong passwords, hide files somewhere in safe locations, or in some cases, you can encrypt files. You can do pretty much everything from encrypting a file to an entire hard drive.
Gpg is a free tool that is used to encrypt a single file or folder with few commands, the only way to decrypt those files is with a password.
Let’s get started! For this tutorial, I’m using Kali Linux and it has Gpg pre-installed not just Kali it comes pre-installed in every Linux version.
How to Encrypt/Decrypt a File in Linux using gpg (Kali Linux)
Let’s say you have file name important.txt and it contains some classified information or some secret stuff that you wanna hide. This ‘important.txt’ file contains the text “Top_Secret” or something totally depends on your work, let’s say it’s a password.
Now, Before everything we have to generate a key first. You will be prompted to enter some security information. Use the defaults when available, otherwise enter your name and email address. You will also be prompted for a passphrase. Remember this passphrase.
After generating the key. We have to encrypt our file.
gpg -e -r fsociety important.txt
If you remember fsociety is our USER-ID. After typing that command your file will be encrypted and another file will be generated with a .gpg extension to delete your original non-encrypted file.
Now you’ll see two files “important.txt” and “important.txt.gpg” let’s cat to see the difference.
As you can see gpg has encoded our string or password inside “important.txt” file and now you can delete your previous text file.
Now, It’s time to decrypt our “important.txt.gpg” back to “important.txt” and readable text.
gpg -d -o decrypted.txt important.txt.gpg
You will be prompted to enter a password for the key and boom!