All articles

What is GPG / PGP and how do I use it?

In this article, we explain the basics of GnuPG (GPG), including what it's used for, the difference between a public and a private key, how to generate a key pair, how to import/export a public key, and how to encrypt/decrypt messages

Oma Team

What is GPG / PGP

GnuPG (more commonly known as GPG) is an implementation of a standard known as PGP (Pretty Good Privacy). It uses a system of "public" and "private" keys for the encryption and signing of messages or data.

Understanding public vs. private keys

Private keys are the first half of a GPG key which is used to decrypt messages that are encrypted using the public key, as well as signing messages - a technique used to prove that you own the key. As the name implies, this part of the key should never be shared.

Public keys are the second half of a key which is used to encrypt messages for the owner of the private key. As the name implies, this part of the key is safe to give out to the public, as it can only be used to encrypt messages or data for the private key owner.

You can download GnuPG (including graphical versions for those uncomfortable with command line) for various platforms, including Windows and macOS / OSX from the GnuPG Website.

On most Linux distributions, GnuPG is included by default as the command line utility gpg, or is available in your package manager.

For our usage examples, we only cover the command line version of GPG. If you're using a GUI version, you should be able to find guides online on how to use them.

Generating a key pair and exporting the public key

To use GPG to send and receive messages securely, first you must generate a key pair.

gpg --generate-key

NOTE: If you want to be able to add a comment to the key, change the bit size (e.g. RSA 2048 or 4096) or algo, or other extra key options you might wish to adjust, you should use gpg --full-generate-key instead:

gpg --full-generate-key

After creating a key pair, you'll want to export the public key to share with others.

# List the keys you own (have the private key for)
$ gpg -K --fingerprint
sec   rsa4096 2019-03-02 [SC] [expires: 2021-03-01]
    9ECF 1199 8AD7 A743 7353  BC57 0E66 E4DE A98A 4921
uid           [ultimate] John Doe <johnd@example.com>
ssb   rsa4096 2019-03-02 [E] [expires: 2021-03-01]

# Take the last 8 characters of that string of random letters and numbers and remove the space
$ gpg -a --export A98A4921 > john_doe.asc

In the above example, the public key was exported to john_doe.asc, you should open the file in a text editor such as Notepad, and make sure the first line shows:

-----BEGIN PGP PUBLIC KEY BLOCK-----

This is to make sure you haven't accidentally exported your private key.

Importing a public key from a key server

Next, import the public key of the person or organisation you want to send an encrypted message to. In the examples below, that's Jane Doe, whose key ID is 4C6B 9D2E 81F3 0A57. Always check the key ID or fingerprint with the owner through a channel you trust, such as their website.

gpg --recv-keys 4C6B9D2E81F30A57

Change the default key server in your config to prevent issues with --recv-keys

A lot of systems are pre-configured to use unreliable or retired key servers, which can cause problems when using --recv-keys. You can resolve most problems with --recv-keys / --search-keys by adding a reliable key server to your GPG config:

Commands to fix keyserver config

# Ensure your user's .gnupg folder exists, so we can create / append to the config files
mkdir -p ~/.gnupg
# Set keys.openpgp.org as your default keyserver
echo "keyserver hkps://keys.openpgp.org" | tee -a ~/.gnupg/dirmngr.conf | tee -a ~/.gnupg/gpg.conf

Once you've added the keyserver setting to your config, you should be able to use --recv-keys and --search-keys without issues.

Retrieve a key from a specific key server for just one command

You can use the --keyserver flag to specify a key server for a single command. This is useful if you can't find a key on your default keyserver and want to try another one.

Retrieve 4C6B 9D2E 81F3 0A57 from keyserver.ubuntu.com

gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys 4C6B9D2E81F30A57

Searching for keys on a key server (e.g. by name or email)

keys.openpgp.org does not allow searching by name, domain or comment, so a search for example.org comes back empty:

$ gpg --keyserver hkps://keys.openpgp.org --search-keys example.org

gpg: error searching keyserver: No data
gpg: keyserver search failed: No data

However, if you search for an exact email address, it returns the key if its owner has published and verified it:

$ gpg --keyserver hkps://keys.openpgp.org --search-keys jane@example.org

gpg: data source: https://keys.openpgp.org:443
(1) Jane Doe <jane@example.org>
      4096 bit RSA key 4C6B9D2E81F30A57, created: 2024-01-15
Keys 1-1 of 1 for "jane@example.org".  Enter number(s), N)ext, or Q)uit >

Enter the number 1 and press enter to import that key. You can enter several numbers separated by spaces (e.g. 1 2 3) to import several keys at once.

Other keyservers, such as hkps://keyserver.ubuntu.com, do support searching by name or domain:

gpg --keyserver hkps://keyserver.ubuntu.com --search-keys example.org

Anyone can upload a key with any name and email to these servers, so a search result alone proves nothing: check the fingerprint before you trust a key.

Encrypting and sending your message

Now, write your message in a plain text editor such as Notepad (Windows) or TextEdit (Mac). You may want to put your public key at the end of the message if you want it to be sent encrypted, otherwise you'll have to send your public key as an attachment or unencrypted text in the message.

Let's assume you saved your message as message.txt. Now encrypt it for Jane:

gpg -a -r 4C6B9D2E81F30A57 -e message.txt

This encrypts message.txt for the public key with the ID 4C6B9D2E81F30A57 (Jane Doe), and writes the encrypted version to message.txt.asc.

Now just open up the .asc file, you should see it starts with

-----BEGIN PGP MESSAGE-----

Copy and paste the contents of this file (including the BEGIN and END lines) into an email or other message, and make sure you've included your public key in some form - either in the encrypted message, or in plain text with the message (e.g. pasted at the end, or attached to an email). Finally, send the message.

Decrypting an encrypted message sent to you

To decrypt a message sent to you, assuming it's saved as reply.txt.asc, run the following command. GPG automatically selects the correct private key to decrypt the message and prints it to the terminal.

Decrypt an encrypted message

$ gpg -d reply.txt.asc

gpg: encrypted with 4096-bit RSA key, ID 0E66E4DEA98A4921, created 2019-03-02
      "John Doe <johnd@example.com>"
hello world

You can also pipe the encrypted message into GPG. This is very useful for interacting with the clipboard - e.g. the pbcopy and pbpaste commands on macOS (on Linux, xclip -o or wl-paste do the same job).

Piping pbpaste into gpg -d (decrypt)

$ pbpaste | gpg -d
gpg: encrypted with 4096-bit RSA key, ID 0E66E4DEA98A4921, created 2019-03-02
      "John Doe <johnd@example.com>"
hello world

If you want to save the message / data instead of outputting it to the terminal, use shell redirection.

The below example outputs the message to reply.txt:

$ gpg -d reply.txt.asc > reply.txt

gpg: encrypted with 4096-bit RSA key, ID 0E66E4DEA98A4921, created 2019-03-02
      "John Doe <johnd@example.com>"

$ cat reply.txt

hello world

Trust a GPG key to stop getting asked whether you're sure you want to use a certain key

By default, GPG treats imported public keys as unverified, and asks you to confirm that you really want to encrypt for a key. For example, if you try to encrypt a message for Jane, you'll likely see something like this:

user@host $ gpg -r jane@example.org -ae hello.txt
gpg: 7D21A0C35B9E4F18: There is no assurance this key belongs to the named user
sub  rsa4096/7D21A0C35B9E4F18 2024-01-15 Jane Doe <jane@example.org>
 Primary key fingerprint: 2B1F 6A90 C4D7 3E85 9A12  F06E 4C6B 9D2E 81F3 0A57
      Subkey fingerprint: 9E40 1B7C 2D58 A6F3 0C91  44B2 7D21 A0C3 5B9E 4F18

It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N)

To fix this, sign the public key GPG is complaining about. This tells GPG: "I, John Doe, am certain that key 4C6B9D2E81F30A57 belongs to the person I believe it does." Only do this after you have checked the fingerprint.

Sign Jane's key locally to tell GPG you trust it

gpg --lsign-key 4C6B9D2E81F30A57

GPG shows you the key you're signing, and the key it will sign with:

pub  rsa4096/4C6B9D2E81F30A57
     created: 2024-01-15  expires: never       usage: SC
     trust: unknown       validity: unknown
 Primary key fingerprint: 2B1F 6A90 C4D7 3E85 9A12  F06E 4C6B 9D2E 81F3 0A57

     Jane Doe <jane@example.org>

Are you sure that you want to sign this key with your
key "John Doe <john@example.com>" (9FD64F451078892A)

The signature will be marked as non-exportable.

Really sign? (y/N)

Now, if you encrypt a message for Jane, GPG no longer asks whether you're sure, and encrypts the message immediately:

user@host $ gpg -r jane@example.org -ae hello.txt

Signing and verifying a file / message to show that you approve a message, or verify the authenticity of a file

Clearsign (GPG signature as part of the file/message)

For signing simple text messages, clearsign is usually used, as they're often intended for humans to read, thus the visible signature won't cause much of an issue

First let's create a file message.txt

hello world
lorem ipsum dolor

this message is from john doe!

Now, we'll sign it with our John Doe key

Sign the file with the john@example.org private key

gpg -u john@example.com -a --clear-sign message.txt

If we now read the file message.txt.asc - we can see both the original message, and the signature:

user@host $ cat message.txt.asc
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

hello world
lorem ipsum dolor

this message is from john doe!
-----BEGIN PGP SIGNATURE-----

iQFFBAEBCAAvFiEEp6Qc+4bcuOcca8Lgn9ZPRRB4iSoFAmAKXEgRHGpvaG5AZXhh
bXBsZS5jb20ACgkQn9ZPRRB4iSoo/Af+JyG6r7FhZKPpaQ+nVu0zv3X+EbEqHGy2
SkBlSu3knbTrDsNmGT3offk7PDdyhtScvCauLcfVmRum9pLTRjoOq1qAewMSZhjk
TmOFgC4BUAVE5wsSG4gvgfRU9W4zijDCSXo6L9zSejIsIifj++w8oCR2KqtizVGa
3dzTOudIqUjV8yJr+6uAHfyPo0DRyy3C97s4ZLCJkl8Dr4xNRUPpvk9SSjHtVPOm
eSrO5GEGSkw/h74Cb/AwYJMVgGdOqDTe0pvJUHetw7d9zBjYMf5iUeaUCn7CwrSl
R35cTc9XNejA2vXimFwPiADkhoT5mRPX4Z5j3fm08HjO5tyJN0DDUQ==
=VDen
-----END PGP SIGNATURE-----

Verify a clear-signed message

Verify the message.txt.asc file's signature

gpg --verify message.txt.asc

NOTE A user needs the GPG public key for john@example.com to be able to verify the signature, while you require the private key to be able to make a signature.

Output from the command:

gpg: Signature made Fri Jan 22 05:02:00 2021 UTC
gpg:                using RSA key A7A41CFB86DCB8E71C6BC2E09FD64F451078892A
gpg:                issuer "john@example.com"
gpg: Good signature from "John Doe " [ultimate]

Detached Signature (separate signature file)

Detached signatures are very common for signing files where the signature would prevent the file from being able to be read, or where you don't have the ability to / don't want to distribute the original file with your signature.

They're most common for verifying software, to ensure that nobody has tampered with the application other than the actual developer(s) who've signed it.

First we'll remove the previous clearsign signature

rm message.txt.asc

Now we'll use the -b flag to create a detached signature (alternatively you can use --detach-sign for clarity)

gpg -u john@example.com -a -b message.txt

If we cat message.txt.asc - we can see this time that it's JUST the signature, not the original contents. This means the person verifying the signature, needs both the original file, and the signature file (.asc)

-----BEGIN PGP SIGNATURE-----

iQFFBAABCAAvFiEEp6Qc+4bcuOcca8Lgn9ZPRRB4iSoFAmAKXdMRHGpvaG5AZXhh
bXBsZS5jb20ACgkQn9ZPRRB4iSqkRgf/W2/XkhI0tyi4eRQRAS82sfnf4snUU8WS
U7S+PnbMDHdtNfDHQQRp3ygm0YcfNDeIa1KUT+7IsgKnmEDNQLBldTX4C776ibT9
cnUJCBj+qFfDcw2DtpMkbKnOHyvhmkkHaN09dXbcwAYP7B5ytuR066DeYz3EOIlK
12WiK8v/TxDpXiVLsgz9huOj6YkuhC/JISK6kVf56j4sMI5O2h0yV87CqVx6gmEK
NeQHAEM2mdpiVUwObzj+XDYw7/LyCYsxrDUEM6hqNAXUH9S+VhAl4ywZ2Q8fQCrO
hnqFgoSsunIZTm0iNVNzJGkkUhIFqJ7yTNS1nG+5t9TEBo3VQUvtdw==
=Ugoy
-----END PGP SIGNATURE-----

Verifying a detached signature

There are two ways to verify a detached signature.

1) Ensure the signature file has the same filename as the file being verified, but ends in either .asc (ASCII Armoured) or .gpg (raw binary)

If the original file has the same name, minus the .asc / .gpg extension, then you can simply pass the signature's name to gpg, and it will auto-detect the original filename

gpg --verify message.txt.asc

This would output:

gpg: assuming signed data in 'message.txt'
gpg: Signature made Fri Jan 22 05:08:35 2021 UTC
gpg:                using RSA key A7A41CFB86DCB8E71C6BC2E09FD64F451078892A
gpg:                issuer "john@example.com"
gpg: Good signature from "John Doe <john@example.com>" [ultimate]

2) Manually specify both the signature filename and the original filename

If the original file has a different name to the signature, OR the signature and file are in different locations, then you should specify both filenames to GPG, placing the signature's file name after --verify, and then the original filename after that.

gpg --verify message.txt.asc message.txt

This would output:

gpg: Signature made Fri Jan 22 05:08:35 2021 UTC
gpg:                using RSA key A7A41CFB86DCB8E71C6BC2E09FD64F451078892A
gpg:                issuer "john@example.com"
gpg: Good signature from "John Doe <john@example.com>" [ultimate]