GnuPG Encryption Cheat Sheet
GnuPG (gpg) is a great utility that is usually available both on Linux and Mac. It can be used for generating encryption and signing keys and using them to encrypt and decrypt your sensitive data. Here’s a quick cheat sheet for basic usage scenarios.
Generating a new key
Generate a new gpg key:
gpg --full-generate-key
The utility will ask some questions, for most of them the default values are fine:
- What kind of key you want? (RSA and RSA)
- What keysize do you want? (3072)
- How long the key should be valid? (the key does not expire)
- Real name? (Input a name to identify the key)
- Email address? (Input the email address that you want to be associated with the key)
- Passphrase to secure the key.
Make sure you memorize the passphrase and store it in a secure place, otherwise you’ll lose access to the key, and, consequently, to the encrypted files. An example of the generation output is provided below.
$ gpg --full-generate-key
gpg (GnuPG) 2.2.19; Copyright (C) 2019 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Please select what kind of key you want:
(1) RSA and RSA (default)
(2) DSA and Elgamal
(3) DSA (sign only)
(4) RSA (sign only)
(14) Existing key from card
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072) 4096
Requested keysize is 4096 bits
Please specify how long the key should be valid.
0 = key does not expire
<n> = key expires in n days
<n>w = key expires in n weeks
<n>m = key expires in n months
<n>y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y
GnuPG needs to construct a user ID to identify your key.
Real name: forketyfork
Email address: forketyfork@example.com
Comment:
You selected this USER-ID:
"forketyfork <forketyfork@example.com>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key 2338E8E84DC0810C marked as ultimately trusted
gpg: revocation certificate stored as '/home/user/.gnupg/openpgp-revocs.d/ABCDEF0123456789ABCDEF0123456789ABCDEF01.rev'
public and secret key created and signed.
pub rsa4096 2021-04-08 [SC]
ABCDEF0123456789ABCDEF0123456789ABCDEF01
uid forketyfork <forketyfork@example.com>
sub rsa4096 2021-04-08 [E]
The gpg files, including the keys, are located in ~/.gnupg directory.
Here’s how to list the existing keys:
$ gpg --list-secret-keys
/home/user/.gnupg/pubring.kbx
-------------------------------------------------------
sec rsa2048 2021-04-06 [SCEA]
ABCDEF0123456789ABCDEF0123456789ABCDEF01
uid [ultimate] forketyfork <forketyfork@example.com>
ssb rsa2048 2021-04-06 [SEA]
Note the line [ultimate] forketyfork <forketyfork@example.com> . Here, forketyfork is the key name. It’s preceded by the [ultimate] trust level and followed by the email associated with the key.
Backing up and importing the keys
You may want to transfer the key files to another machine. Here’s how to create a secure backup of the key:
gpg --output backupkeys.pgp --armor --export-secret-keys --export-options export-backup "forketyfork"
The file backupkeys.pgp will be created. You should store it in a safe location. The option --armor allows to create a text file instead of a binary file, so such file should be readable by any text editor.
You can use this backup file to import the key on another machine. Here’s how to import the key from the backup:
gpg --import backupkeys.pgp
When you list the keys, you’ll see that the trust level for the newly imported key is [unknown]:
$ gpg --list-keys
/home/user/.gnupg/pubring.kbx
-------------------------------------------------------
pub rsa4096 2021-04-08 [SC]
ABCDEF0123456789ABCDEF0123456789ABCDEF01
uid [unknown] forketyfork <forketyfork@example.com>
sub rsa4096 2021-04-08 [E]
So you’ll also need to set the trust level for the imported key —for your own key, you can set it to ultimate by selecting “I trust ultimately” when asked:
gpg --edit-key "forketyfork"
gpg> trust
gpg> save
Now the trust level should be [ultimate] :
$ gpg --list-keys
/home/user/.gnupg/pubring.kbx
-------------------------------------------------------
pub rsa4096 2021-04-08 [SC]
ABCDEF0123456789ABCDEF0123456789ABCDEF01
uid [ultimate] forketyfork <forketyfork@example.com>
sub rsa4096 2021-04-08 [E]
Encrypting and decrypting files
Encrypting and signing a file filename and store the result in filename.gpg is as easy as:
gpg --encrypt --sign --recipient "forketyfork" --output filename.gpg filename
Or a shorter version:
gpg -esr "forketyfork" -o filename.gpg filename
Decrypting the file filename.gpg to filename:
gpg --decrypt --output filename filename.gpg
Or a shorter version:
gpg -do filename filename.gpg
Archiving a file or a whole directory, signing and encrypting, as a one-liner:
tar cz directory | gpg -esr "forketyfork" directory.tar.gz.gpg