December 5, 2026

GPG Explained: Encrypting, Signing and Verifying Files, With Every Error Message Tested

A wax-sealed envelope beside a brass seal and a laptop on a desk

GnuPG has a reputation for being hard, and most of that reputation comes from its messages. “Unusable public key.” “There is no assurance this key belongs to the named user.” “Good signature,” followed immediately by “WARNING: This key is not certified with a trusted signature!” Each one means something specific, and most of them are the tool doing its job. To see them all in context, two test identities, Alice and Bob, were created with GnuPG 2.4.4 (the version in Ubuntu 24.04), and every common task was run between them, including what happens when a single byte of a signed file changes.

Video: DFIRScience on YouTube

Keys first

Making a key pair

The quickest modern way to create a key:

gpg --quick-gen-key "Alice Test <alice@example.com>" default default 1y

The default choices produced an Ed25519 primary key for signing and certifying, [SC], plus a Curve25519 subkey for encryption, [E], both expiring in a year. Two keys under one identity is normal: one signs, one receives encrypted mail.

Sharing the public half

gpg --armor --export alice@example.com > alice.asc
gpg --import bob.asc

Alice’s whole exported public key, in the text-safe “armored” format, came to 648 bytes. Elliptic-curve keys are small enough to paste into an email or a web page. The private half never leaves the machine.

Old sealed letter

Encrypting a file for someone else

The first error

Alice imported Bob’s key and tried to encrypt a file for him in batch mode:

$ gpg --batch -e -r bob@example.com -o text.gpg text.txt
gpg: F4E12B3942F772C6: There is no assurance this key belongs to the named user
gpg: text.txt: encryption failed: Unusable public key

Nothing was wrong with the key. GnuPG simply had no evidence that the key really belonged to Bob, and in batch mode it refused rather than guess. Interactively it asks. The proper fix is to check Bob’s fingerprint with Bob, through some channel other than the one the key arrived by, and then sign the key locally (covered below). For a one-off test, --trust-model always skips the check, which is exactly as dangerous as it sounds.

What came out

Once encrypted, the 1,000,000-byte text file shrank to 2,648 bytes. A 1,000,000-byte file of random data grew to 1,000,976. gpg --list-packets showed why:

:pubkey enc packet: version 3, algo 18, keyid F4E12B3942F772C6
:aead encrypted packet: cipher=9 aead=2 cb=16
:compressed packet: algo=2
:literal data packet:

GnuPG compresses before encrypting. The test text was one line repeated tens of thousands of times, so it collapsed to almost nothing; random data does not compress, so it only gained a little overhead. In the packet list, cipher 9 is AES-256, algorithm 18 is the elliptic-curve encryption Bob’s subkey uses, and the AEAD packet means the encrypted data carries its own integrity check.

The surprise: the sender cannot read it

$ gpg -d text.gpg            # run as Alice
gpg: decryption failed: No secret key

Alice encrypted the file only to Bob’s key, so only Bob’s private key can open it. Senders who want to read their own copies later add themselves as a second recipient, -r alice@example.com, or set encrypt-to in their configuration. Bob decrypted it with gpg -d -o out.txt text.gpg, and cmp confirmed the output matched the original byte for byte.

Signing and verifying

A detached signature

Signing a release file produced a separate 228-byte .asc file:

gpg --armor --detach-sign -o release.tar.asc release.tar

The GnuPG manual’s operational commands page recommends always naming both files when verifying a detached signature, the signature first and the data second, rather than relying on GnuPG to guess the data file from the signature’s name:

$ gpg --verify release.tar.asc release.tar
gpg: Good signature from "Alice Test <alice@example.com>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
Primary key fingerprint: 6EE5 F81B 0640 18A0 C131  9556 D8D3 D0D9 CD53 A334

Reading the warning correctly

“Good signature” means the file is exactly what that key signed. The warning means GnuPG has no proof the key belongs to the real Alice. Both halves matter. Anyone can make a key named “Alice Test.” The fingerprint is the thing to compare against one published somewhere trustworthy.

One changed byte

A single byte in the middle of the 1 MB file was overwritten, then the same command was run again:

gpg: BAD signature from "Alice Test <alice@example.com>" [unknown]

The exit codes, which are what scripts should check, were 0 for a good signature, 1 for a bad one, and 2 when the verifier did not have the signer’s public key at all (“Can’t check signature: No public key”).

Passport stamp

Trust without a web of trust

Local signatures

After confirming Alice’s fingerprint, Bob marked her key as verified on his own machine:

gpg --quick-lsign-key 6EE5F81B064018A0C1319556D8D3D0D9CD53A334

The next verification read Good signature from "Alice Test <alice@example.com>" [full], and the warning disappeared. A local signature, lsign, stays on Bob’s keyring and is never exported, so it records Bob’s own check without announcing anything to the world. Bob’s encryption to Alice would also stop needing --trust-model always.

gpgv for scripts

For automated checks, such as verifying a download in a build script, the manual points to gpgv, a stripped-down verifier that only accepts signatures from keys in a keyring it is given. Tested with a keyring holding only Alice’s key, it printed “Good signature” and exited 0. Pointed at a keyring holding only Bob’s key, the same valid file failed with exit code 2. That is the behavior a script wants: trust exactly one key, decided in advance, and nothing else.

Two other modes worth knowing

Cleartext signatures

gpg --clearsign -o note.txt.asc note.txt

The result is readable text wrapped in -----BEGIN PGP SIGNED MESSAGE----- with a Hash: SHA512 header. Changing “noon” to “three” in the signed text produced a BAD signature, as it should. The GnuPG manual still advises preferring detached signatures, because a cleartext signature covers only the text between its markers, not anything placed around it, and it notes other pitfalls with the format. For files, detached is the safer habit.

Symmetric encryption with a passphrase

gpg -c -o sym.gpg text.txt

No keys at all: the file is encrypted with AES-256 (cipher 9 in the packet list) using a key derived from a passphrase, and anyone who knows the passphrase can open it. It is the simplest option for a backup sent to oneself, and it lives or dies by the strength of the passphrase.

Locksmith

Backups, expiry and the kill switch

Back up the private key before anything else

A key that encrypts files is also the only way to decrypt them. Lose it and every file encrypted to it is gone. Exporting it takes one command:

gpg --armor --export-secret-keys alice@example.com > alice-secret.asc

For the test key, the secret export was 752 bytes, small enough to print on paper. It belongs offline, on an encrypted USB stick or in a safe, and never next to the files it protects.

Expiry is a safety catch, not a deadline

The test key was created to expire in a year. An expiry date does not destroy anything; the owner can push it out at any time with gpg --quick-set-expire <fingerprint> 2y and re-share the public key. In the test, that one command moved the trust database’s next check a year further out. The point of an expiry date is what happens if the owner loses the private key and does nothing: the key eventually stops being accepted on its own.

The revocation certificate

GnuPG 2.4 created one automatically when the key was made, in a folder called openpgp-revocs.d inside the GnuPG home directory. Its own header calls it a kind of “kill switch” for declaring that a key should no longer be used. It is stored with a colon in front of the first armor line so it cannot be imported by accident; removing that colon arms it.

The test imported Alice’s revocation into Bob’s keyring and verified a file Alice had signed earlier:

gpg: Good signature from "Alice Test <alice@example.com>" [unknown]
gpg: WARNING: This key has been revoked by its owner!

The exit code was still 0. A script that only checks whether gpg --verify succeeded would accept a signature from a revoked key without blinking. Scripts that matter should either use gpgv with a curated keyring, which is updated when a key is retired, or parse GnuPG’s machine-readable status output instead of trusting the exit code alone.

The everyday commands

A short list

Task Command
Encrypt for someone gpg -e -r bob@example.com file
Encrypt for someone and keep a readable copy gpg -e -r bob@example.com -r alice@example.com file
Decrypt gpg -d -o file file.gpg
Sign a file gpg --armor --detach-sign file
Verify a file gpg --verify file.asc file
Encrypt with a passphrase only gpg -c file
Mark a checked key as trusted locally gpg --quick-lsign-key <fingerprint>

What the tool cannot do for anyone

Every command above worked perfectly with two keys whose owners were invented minutes earlier. The math only proves that a file matches a key. Whether the key belongs to the person on the label is a question GnuPG asks out loud every time, and the warning is the right answer until someone checks a fingerprint.

Sources

  • Operational GPG Commands, Using the GNU Privacy Guard (GnuPG manual): https://www.gnupg.org/documentation/manuals/gnupg/Operational-GPG-Commands.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *