GPG ENCRYPTION — INTERACTIVE LAB

GNU Privacy Guard (GPG)

Generate key pairs, encrypt and decrypt files, import trusted keys, and digitally sign documents — all through a simulated terminal environment.

Lab Exercises
1
Generate a GPG Key Pair
RSA 4096-bit key generation with uid and passphrase
BEGINNER ✓ Done
Scenario: You are setting up a secure communications channel with your professor. Before you can encrypt anything, you need a personal GPG key pair — a public key you share freely and a private key you guard with your passphrase.

Public Key

Share with anyone who needs to send you encrypted messages or verify your signatures. Safe to publish on keyservers.

Private Key

Never shared. Protected by a passphrase. Used to decrypt messages encrypted with your public key and to sign documents.

Run the key generation command. Use gpg --full-generate-key for the interactive wizard, or the shorthand gpg --gen-key. The simulator will walk you through the full process.

  • 1.Initiate key generation
  • 2.Confirm the key details are generated
gpg key generation
# GPG uses asymmetric cryptography (RSA, EdDSA, etc.) # Your key pair is stored in ~/.gnupg/ # Passphrase protects your private key at rest student@hexworth:~$ _
student@hexworth:~$
Hint: type gpg --full-generate-key or gpg --gen-key to start the key generation wizard. Either form is accepted.
KEY PARAMETERS EXPLAINED

Key type RSA: The most widely supported algorithm — safe choice for interoperability.
Key size 4096: Double the minimum. Computationally expensive to brute-force — the NSA considers 2048 adequate through 2030; 4096 gives you a generous margin.
Expiration: Setting an expiry date forces you to rotate keys periodically. A key without an expiry that leaks is permanently compromised.

PASSPHRASE SECURITY

Your passphrase is the last line of defence if your private key file is stolen. Use a minimum of 20 characters — a short random sentence works well. GPG uses a key-derivation function (KDF) with the passphrase, so brute-force attacks are slow — but a weak passphrase still falls quickly.

2
List & Export Your Keys
Inspect the keyring and produce an ASCII-armored public key file
BEGINNER ✓ Done
Scenario: Your key pair has been generated. Now you need to inspect what is stored in your keyring and export your public key so you can share it with the professor.

Try both commands. List first, then export:

  • 1.Run gpg --list-keys to see all keys on your public keyring
  • 2.Run gpg --export -a "student@hexworth" to produce the ASCII block
gpg keyring inspection
# Your keyring lives at ~/.gnupg/pubring.kbx (GPG2) # --list-keys shows public keys --list-secret-keys shows private student@hexworth:~$ _
student@hexworth:~$
Step 1: gpg --list-keys
Step 2: gpg --export -a "student@hexworth" > public.key
(The -a flag produces ASCII armor — readable text instead of binary.)
READING THE KEY LISTING

pub = master public key. Flags: [SC] means Sign + Certify.
uid = user identity (name + email).
sub = subkey. Flag: [E] means Encrypt.
GPG uses subkeys so your master key (which never travels the network) stays safe while the encryption subkey does the day-to-day work.

THE WEB OF TRUST

GPG does not rely on certificate authorities. Instead, you manually verify someone's fingerprint (over a phone call, in person, or via a signed email) and then sign their key. The more respected people have signed a key, the more trustworthy it is considered. This is the web of trust model — decentralised and resilient, but requires active participation.

3
Import & Verify a Public Key
Add the professor's key to your keyring and check the fingerprint
INTERMEDIATE ✓ Done
Scenario: The professor has sent you their public key file (professor.key). Before you can encrypt messages for them, you must import the key and verify the fingerprint out-of-band to confirm it has not been tampered with.
  • 1.Import the key with gpg --import professor.key
  • 2.Verify the fingerprint with gpg --fingerprint professor@hexworth
gpg import & verify
# professor.key has been placed in your working directory # It contains the professor's RSA-4096 public key student@hexworth:~$ _
student@hexworth:~$
Step 1: gpg --import professor.key
Step 2: gpg --fingerprint professor@hexworth
Verify the fingerprint over a trusted channel (phone, in person) before trusting this key for sensitive communications.
TRUST LEVELS

After importing, GPG assigns unknown trust by default. You can set trust with gpg --edit-key professor@hexworth then the trust command. Levels: unknown → undefined → marginal → full → ultimate. You only assign ultimate to your own keys.

KEY SUBSTITUTION ATTACKS

A man-in-the-middle could send you a fake public key with the professor's name on it. Without fingerprint verification, you would encrypt to the attacker's key thinking it belongs to the professor. Always verify fingerprints out-of-band — via a voice call, an in-person meeting, or a separate signed channel.

4
Encrypt a File for a Recipient
Asymmetric encryption using the recipient's public key
INTERMEDIATE ✓ Done
Scenario: You need to send a sensitive file (secret.txt) to the professor. Encrypt it with their public key so that only the professor's private key can decrypt it.
[Your file] + [Professor's public key] | v GPG asymmetric encryption | [secret.txt.gpg] ←— only professor's private key can open this Note: GPG internally generates a random session key, encrypts the data with AES-256, then encrypts the session key with RSA-4096. This hybrid approach gives you speed (AES) + asymmetric security (RSA).
  • 1.Encrypt secret.txt for the professor using --encrypt --recipient
  • 2.Observe the encrypted output file
gpg encryption
# File to encrypt: secret.txt # Recipient: professor@hexworth (key already imported in Ex 3) student@hexworth:~$ _
student@hexworth:~$
Command: gpg --encrypt --recipient professor@hexworth secret.txt
Short form: gpg -e -r professor@hexworth secret.txt
Add --armor (-a) to produce a readable ASCII .asc file instead of binary .gpg.
HYBRID ENCRYPTION INTERNALS

GPG does not directly encrypt your file with RSA — RSA is too slow for large data. Instead GPG generates a random 256-bit session key, encrypts your file with AES-256 using that session key, then encrypts the session key itself with the recipient's RSA public key. The .gpg file contains both the encrypted session key and the AES ciphertext. Only the private key holder can recover the session key and therefore decrypt the file.

5
Decrypt a Received File
Unlock an encrypted message using your private key and passphrase
INTERMEDIATE ✓ Done
Scenario: The professor has sent back an encrypted reply: message.gpg. It was encrypted with your public key. Only your private key — protected by your passphrase — can open it.
  • 1.Decrypt with gpg --decrypt message.gpg
  • 2.Enter the passphrase when prompted
  • 3.Read the decrypted content
gpg decryption
# message.gpg is in your working directory # It was encrypted with your public key (student@hexworth) student@hexworth:~$ _
student@hexworth:~$
Command: gpg --decrypt message.gpg
Short form: gpg -d message.gpg
To save output to a file: gpg --output plain.txt --decrypt message.gpg

Why Only You Can Decrypt

The file's session key was locked with your public key. Your private key — which never left your machine — is the only mathematical inverse that can unlock it. Even the professor who encrypted it cannot decrypt it without your private key.

The Passphrase's Role

Your private key is stored encrypted on disk. The passphrase decrypts it into memory at the moment of use, then clears it. An attacker with your key file but not your passphrase still cannot decrypt anything.

GPG AGENT & CACHING

gpg-agent runs in the background and caches your passphrase for a configurable TTL (default 10 minutes). This is why you are not prompted on every single GPG operation in a session. You can configure the TTL in ~/.gnupg/gpg-agent.conf with default-cache-ttl 600 (seconds).

6
Digital Signatures — Sign & Verify
Prove authorship with your private key; verify with the signer's public key
ADVANCED ✓ Done
Scenario: You want to publish a security advisory (advisory.txt) and prove it genuinely came from you. You will sign it with your private key. Anyone with your public key can verify the signature — and detect if the document was tampered with after signing.
[advisory.txt] → SHA-512 hash → RSA sign with [your private key] | [advisory.txt.gpg] (signed document) Verification (by anyone with your public key): [advisory.txt.gpg] → extract signature → decrypt hash with [your public key] | compare to hash of document content | Match = "Good signature" / No match = "BAD signature"
  • 1.Sign advisory.txt with gpg --sign advisory.txt
  • 2.Verify the signature with gpg --verify advisory.txt.gpg
  • 3.Simulate a tampered document and observe the "BAD signature" warning
gpg sign & verify
# advisory.txt is in your working directory # Use --sign to create a binary signed file (.gpg) # Use --clearsign for a human-readable signed text block (.asc) # Use --detach-sign for a separate signature file (.sig) student@hexworth:~$ _
student@hexworth:~$
Step 1 — sign: gpg --sign advisory.txt
Step 2 — verify: gpg --verify advisory.txt.gpg
Step 3 — simulate tamper: simulate tamper (special command)
Human-readable alternative: gpg --clearsign advisory.txt
NON-REPUDIATION

A digital signature provides non-repudiation: once you sign a document with your private key, you cannot credibly deny having signed it. This is the legal and technical basis for code-signing, software release announcements, and email authentication (OpenPGP). If your private key is compromised and you did not revoke it, a forged signature becomes possible — which is why key revocation certificates matter.

SIGN vs ENCRYPT vs BOTH

Sign only: Anyone can read the document, but they can verify it came from you and was not changed.
Encrypt only: Only the recipient can read it, but they cannot prove who sent it.
Sign then encrypt: Best of both — private and authenticated. Use gpg --sign --encrypt -r recipient@host file.

REVOCATION CERTIFICATES

Generate a revocation certificate immediately after creating a key pair: gpg --gen-revoke student@hexworth > student-revoke.asc. Store it offline. If your private key is ever lost or compromised, publish the revocation certificate to notify the world that your key should no longer be trusted.

Lab Complete

0 / 6 exercises

Complete all six exercises to unlock lab completion credit.

Lab marked complete. Your GPG fundamentals are solid.