Part 3 · Networking and hardening · Lesson 06 · 45 minutes
Sign and encrypt files with GnuPG
- Create a key pair, export a public key and import the key of another person
- Encrypt a file for one person and decrypt it
- Sign a file, verify the signature, and sign a download with a detached signature
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
You publish a maintenance notice and a tool archive to your colleague Bob. Your team lead says: "Bob must know that the files come from you and were not changed, and the plan for the backup must be readable by Bob only." You use GnuPG, a free program that implements OpenPGP, to sign the files and to encrypt one of them. To play both people on one machine, you make two separate keyrings, one for Alice (you) and one for Bob.
Recall
Which scp option sets the port, and why does it differ from ssh?
scp -P PORT with a capital P. In scp the small -p means "preserve times and modes".
Which command shows that two copies of a file are the same?
sha256sum on both, then compare the hashes, or cmp FILE1 FILE2, which prints nothing when the files are the same.
The idea
GnuPG uses public-key cryptography. A key pair has a public key, which you give to everyone, and a private key (the program calls it the secret key), which only you hold. The two jobs use the pair in opposite ways:
| Job | Made with | Opened with |
|---|---|---|
| Encrypt a file for Bob | Bob's public key | Bob's private key |
| Sign a file | your private key | your public key |
To sign, GnuPG makes a hash of the file and signs the hash with your private key. Anyone with your public key can check that the hash fits. If one byte of the file changes, the check fails.
- Alice: hash the file
- Alice: sign the hash with her private key
- Alice → Bob: send the file and the signature
- Bob: hash the file again
- Bob: check it with Alice's public key
A key is only useful when you know whose it is. Every key has a fingerprint, a long hash of the key. Before you trust a key that you imported, compare its fingerprint with the one that its owner tells you over another channel, such as a phone call.
Worked example
Nothing here needs administrator rights. The variable GNUPGHOME tells GnuPG where its keyring is. You use a folder in your lab, so that your real keyring in ~/.gnupg stays untouched.
1. Make two keyrings
mkdir -m 700 makes a folder that only you can open. GnuPG complains about a keyring that others can read.
mkdir -p ~/krlab/linux-advanced/gpg && cd ~/krlab/linux-advanced/gpg
mkdir -m 700 alice bob2. Create a key pair
Point GNUPGHOME at Alice's folder. gpg --quick-generate-key "NAME <MAIL>" default default 1y makes a key pair with the default algorithms that expires after one year. --batch --passphrase '' skips the passphrase question: use it in this lab only. A real key always has a passphrase.
export GNUPGHOME=$PWD/alice
gpg --batch --passphrase '' --quick-generate-key "Alice Lab <alice@krbox.lab>" default default 1y
gpg --list-keys
gpg --list-secret-keyspub ed25519 2026-11-21 [SC] [expires: 2027-11-21]
8CBD5681A6D3C39BD3123208B02B518149DCDC6D
uid [ultimate] Alice Lab <alice@krbox.lab>
sub cv25519 2026-11-21 [E]
sec ed25519 2026-11-21 [SC] [expires: 2027-11-21]
8CBD5681A6D3C39BD3123208B02B518149DCDC6D
uid [ultimate] Alice Lab <alice@krbox.lab>
ssb cv25519 2026-11-21 [E]--list-keys shows the public keys of the keyring, and --list-secret-keys the private ones. Your fingerprint, the 40-character line, is different. Read the pub line like this:
sub is a subkey for encryption (E). [ultimate] means that you trust your own key.
3. Export your public key and make Bob's key
--armor writes the output as text, and --export writes a public key. Then switch to Bob's keyring and make his key the same way.
gpg --armor --export alice@krbox.lab > alice.pub.asc
head -1 alice.pub.asc
export GNUPGHOME=$PWD/bob
gpg --batch --passphrase '' --quick-generate-key "Bob Lab <bob@krbox.lab>" default default 1y
gpg --armor --export bob@krbox.lab > bob.pub.asc-----BEGIN PGP PUBLIC KEY BLOCK-----The file alice.pub.asc is safe to send by mail or to publish. Never export the private key (--export-secret-keys) except for a backup that you keep offline.
4. Import the key of the other person
Bob imports Alice's key. Alice imports Bob's. --import reads a key file into the keyring.
gpg --import alice.pub.asc
export GNUPGHOME=$PWD/alice
gpg --import bob.pub.ascgpg: key B02B518149DCDC6D: public key "Alice Lab <alice@krbox.lab>" imported
gpg: Total number processed: 1
gpg: imported: 1
gpg: key B5EFEED8C4B0A261: public key "Bob Lab <bob@krbox.lab>" imported
gpg: Total number processed: 1
gpg: imported: 15. Check the fingerprint and certify the key
The imported key of Bob has no trust yet, and GnuPG refuses to encrypt for it in a script. Read the fingerprint of Bob's key in Alice's keyring, and compare it with the one that Bob would read to you. Then certify the key, which means that you vouch that it belongs to Bob. awk picks the fingerprint out of the machine-readable output, and --quick-lsign-key signs the key for your own keyring only.
gpg --fingerprint bob@krbox.lab
FPR=$(gpg --with-colons --fingerprint bob@krbox.lab | awk -F: '$1=="fpr"{print $10; exit}')
gpg --quick-lsign-key "$FPR"pub ed25519 2026-11-21 [SC] [expires: 2027-11-21]
1232 62F3 B5D5 38DB B1B1 1E62 B5EF EED8 C4B0 A261
uid [ unknown] Bob Lab <bob@krbox.lab>
sub cv25519 2026-11-21 [E]The command prints a summary of the signature, which is shortened here. Your numbers differ.
6. Encrypt a file and decrypt it
--encrypt --recipient MAIL encrypts for one person. --armor makes text. Alice's keyring is active. The result is plan.txt.asc.
echo "plan: move the backup to Friday" > plan.txt
gpg --armor --encrypt --recipient bob@krbox.lab plan.txt
head -1 plan.txt.asc
gpg --decrypt plan.txt.asc-----BEGIN PGP MESSAGE-----
gpg: encrypted with cv25519 key, ID 9B06E0B7DA1CEC61, created 2026-11-21
"Bob Lab <bob@krbox.lab>"
gpg: public key decryption failed: No secret key
gpg: decryption failed: No secret keyAlice encrypted the file for Bob, so Alice cannot read it, because only Bob holds the private key. Switch to Bob's keyring. --output FILE writes the result into a file.
export GNUPGHOME=$PWD/bob
gpg --output plan-bob.txt --decrypt plan.txt.asc
cat plan-bob.txtgpg: encrypted with cv25519 key, ID 9B06E0B7DA1CEC61, created 2026-11-21
"Bob Lab <bob@krbox.lab>"
plan: move the backup to Friday7. Sign a text and verify it
--clearsign keeps the text readable and puts the signature below it. Alice signs, and Bob verifies. Delete the original first, because the signed file holds the text. gpg --verify FILE checks a signature and sets the exit code: 0 for good and 1 for bad.
export GNUPGHOME=$PWD/alice
printf 'notice: maintenance on Friday\n' > notice.txt
gpg --clearsign notice.txt
rm notice.txt
export GNUPGHOME=$PWD/bob
gpg --verify notice.txt.asc; echo "exit: $?"gpg: Good signature from "Alice Lab <alice@krbox.lab>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg: There is no indication that the signature belongs to the owner.
exit: 0The signature is good:
The warning says that Bob has not certified Alice's key. Bob would compare her fingerprint first, as in step 5. Now change one word and verify again. sed 's/OLD/NEW/' replaces the first match of OLD on each line with NEW.
sed 's/Friday/Monday/' notice.txt.asc > notice-bad.asc
gpg --verify notice-bad.asc; echo "exit: $?"gpg: BAD signature from "Alice Lab <alice@krbox.lab>" [unknown]
exit: 18. Sign a download with a detached signature
A detached signature is a separate file. The download stays untouched, and the signature travels next to it. This is how projects sign a release. --detach-sign makes it, and --verify SIGNATURE FILE checks the pair.
export GNUPGHOME=$PWD/alice
mkdir -p dl && printf 'tool v1.0\n' > dl/tool.txt && tar -czf tool-1.0.tar.gz dl
gpg --armor --detach-sign tool-1.0.tar.gz
export GNUPGHOME=$PWD/bob
gpg --verify tool-1.0.tar.gz.asc tool-1.0.tar.gz; echo "exit: $?"
echo "x" >> tool-1.0.tar.gz
gpg --verify tool-1.0.tar.gz.asc tool-1.0.tar.gz; echo "exit: $?"gpg: Good signature from "Alice Lab <alice@krbox.lab>" [unknown]
exit: 0
gpg: BAD signature from "Alice Lab <alice@krbox.lab>" [unknown]
exit: 1One added byte is enough to break the signature. Without the pair, the check fails.
Your turn
Now Bob answers Alice. There are no steps here: use the commands of the worked example. Keep GNUPGHOME in mind: each command runs in the keyring of the person who does it.
- Bob certifies Alice's key in his keyring (read the fingerprint first).
- Bob writes
reply.txtwith the lineack: backup moved to Friday, encrypts it for Alice, and Alice decrypts it intoreply-alice.txt. - Bob writes
release.txtwith the linerelease 2.0, makes a detached signature, and Alice verifies it. Then add one line torelease.txtand verify again.
The last answer comes from gpg --list-keys --with-colons | grep -c '^pub', run with Alice's keyring.
Then clean up. Each keyring has a gpg-agent process, and GNUPGHOME still points to one of them. Stop both agents and clear the variable, so that later lessons use your real keyring.
for k in alice bob; do GNUPGHOME=$PWD/$k gpgconf --kill gpg-agent; done
unset GNUPGHOMECheck yourself
Alice encrypts a file for Bob. Who can read it?
Only Bob, who holds the private key that fits the public key used. Alice cannot read it, unless she also adds her own key as a recipient (--recipient alice@krbox.lab as well).
A signature is "Good" but GnuPG warns that the key is not certified. What does that mean?
The file matches the key that signed it. GnuPG does not know that the key belongs to the person you think. Check the fingerprint over another channel, and then certify it.
Why is a detached signature used for a download?
The file stays exactly as the publisher built it, so it works with any tool, and the signature is a small separate file. You verify the pair.
Why can you not decrypt a file after you lost your keyring, although you still have the public key?
Decryption needs the private key. The public key only encrypts. Keep a backup of the private key offline, and make a revocation certificate (GnuPG stores one in openpgp-revocs.d when it makes the key).
Field notes
- A key that you use for real work needs a passphrase, an expiry date and a backup of the private key offline. Keep the revocation certificate in a safe place: it lets you declare a lost key as invalid.
- Check the signature of a download that you install, and get the fingerprint of the publisher from a source that you trust apart from the download. A signature from an unknown key proves nothing.
- Signed software packages use the same idea:
aptchecks the signatures of the repositories before it installs. gpg --symmetric FILEencrypts with a passphrase and no key pair. It suits a file that you keep for yourself.- GnuPG also signs
gitcommits and tags. Newer tools such asage(encryption) andminisign(signatures) do less and are easier to use, but GnuPG is on almost every Linux system.