Encrypting and decrypting a file with GPG in Linux

Friday, August 9, 2024 at 11:46 AM | 5 min read

Last modified on Saturday, July 18, 2026 at 12:32 PM

#encryption, #decryption, #gpg, #linux, #command line, #cybersecurity

Man seated at a desk holding a magnifying glass in one hand and deciphering symbols on individual pieces of paper

Photo by cottonbro studio on pexels.com

Table of Contents

You may wonder why I am fascinated with encryption and decryption. I have even written a number of posts about it. I have always loved to solve puzzles and mysteries. But I especially started to take an interest in encryption and decryption of text when people had increased privacy concerns during the Arab Spring of 2011. I had been following the Electronic Frontier Foundation and believed in their mission. On their about page, EFF states,

The Electronic Frontier Foundation is the leading nonprofit organization defending civil liberties in the digital world. Founded in 1990, EFF champions user privacy, free expression, and innovation through impact litigation, policy analysis, grassroots activism, and technology development. EFF's mission is to ensure that technology supports freedom, justice, and innovation for all people of the world.

In addition, in 2010, EFF released an open source tool called HTTPS Everywhere. It made the (free) implementation of HTTPS on websites available to people like myself who otherwise would not have had access to such a tool.

But what put my privacy concerns over the top was when Edward Snowden leaked data about the National Security Agency's (NSA) mass surveillance programs in 2013. I was not the only one who was worried about data privacy and how their data was collected both by government entities and corporations. I went as far as looking into using open source email services and setting up my own email encryption. I never got around to fully implementing it because not only did I need to set it up, but the people I communicated with had to as well.

I found that learning how to encrypt and decrypt documents and email attachments was a more attainable goal. I began writing posts like this one in human-readable language to make the concepts more accessible to a broader audience who shares my interest in the topic.

What is GPG?

According to man gpg in Linux,

GPG is an OpenPGP encryption and signing tool native to Linux. The gpg command is the OpenPGP part of the GNU Privacy Guard (GnuPG). It is a tool that provides digital encryption and signing services using the OpenPGP standard. gpg features complete key management and all the bells and whistles you would expect from a full OpenPGP implementation.

There are two main versions of GnuPG: GnuPG 1.x and GnuPG 2.x. GnuPG 2.x supports modern encryption algorithms and thus should be preferred over GnuPG 1.x. You only need to use GnuPG 1.x if your platform doesn't support GnuPG 2.x, or you need support for some features that GnuPG 2.x has deprecated, e.g., decrypting data created with PGP-2 keys.

If you are looking for version 1 of GnuPG, you may find that version installed under the name gpg1...

GPG options you need to know

There are a lot of GPG options. However, in order to be able to encrypt and decrypt data, I only need to know the following three options:

  • Create or encrypt (-c)
  • Decrypt (-d)
  • Extract and decrypt (no option)

Creating a file for encryption

First, I create a new file for encryption called original_file.txt and add some text to it at the same time using the stdout redirect operator:

echo This is an encryption test > original_file.txt

And when I open the new file with the command vim original_file.txt, it contains the following contents:

This is an encryption test

Encrypting the original_file.txt file

Next, I run the following command in Terminal:

gpg -c original_file.txt

As mentioned previously, the -c flag encrypts a file.

Running the command returns the following:

# in my Linux Mint OS, a window prompt appears in which I have to enter a password, and after I enter it twice and it matches, I am taken back to the Terminal prompt. maria@maria-VirtualBox:~/Desktop$ # next, when I run `ls` against ~/Desktop, the following is returned: cron-job-scripts linux-shell-scripts original_file.txt.gpg history.txt original_file.txt test-file.txt

According to the article entitled File encryption and decryption made easy with GPG on the Red Hat blog,

The .gpg extension isn't required, but it does let the user know which decryption tool to use to read the file. You can rename the file to anything you want.

Decrypting the original_file.txt file with the -d flag

Next, I remove original_file.txt so that the encrypted file is the sole source of the information contained in it. I do this with the rm original_file.txt command.

Then, to decrypt original_file.txt.gpg, I run gpg original_file.txt.gpg which yields:

# first I am provided with a prompt to type in my password and then am returned to the Terminal prompt and the following is stdout there: gpg: AES256.CFB encrypted data gpg: encrypted with 1 passphrase This is an encryption test # which is the original content of the file

Decrypting and extracting the original_file.txt file without any option

If I want to decrypt original_file.txt.gpg and extract the original_file.txt file at the same time, I simply run the gpg original_file.txt.gpg command, without any options. Running the command returns the following:

gpg: WARNING: no command supplied. Trying to guess what you mean ... gpg: AES256.CFB encrypted data gpg: encrypted with 1 passphrase

This time the contents of the file is not output to Terminal. The original_file.txt, however, reappears:

# I run the `ls` command to see if original_file.txt has been successfully extracted: ls # which returns: cron-job-scripts linux-shell-scripts original_file.txt.gpg history.txt original_file.txt test-file.txt

Decrypting a file using the -d flag vs without any option

When I decrypt a file using the -d flag, I need access to the passphrase created when the file was encrypted. When I decrypt a file without any options, I still need to provide the passphrase.

Conclusion

In this post, I talk about why I developed an interest in encryption and decryption of text, and why I have been following organizations such as the Electronic Frontier Foundation pretty much since its inception. Most important, I demonstrate how to use GPG to encrypt and decrypt files. My aim has been to make sure that I explain the concepts in a way that is understandable to the maximum number of people possible.

Further Reading

I also wrote an article entitled Encrypting and Decrypting Files and Strings, in which I compare using Python to encrypt and decrypt strings vs using GnuPG to encrypt and decrypt strings and files via the Command Line on macOS. The process(es) differ somewhat from what has been described here, and GnuPG is not native to macOS.