Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 80 ยท Cryptography Basics

PGP and S/MIME: Encrypting Email

Learn how PGP and S/MIME sign and encrypt email using hybrid cryptography, how the web of trust compares to certificate authorities, and where email encryption falls short.

Ordinary email is like a postcard. It crosses many servers, sits on disks, and anyone with access along the way can read or change it. Transport encryption between mail servers (TLS) helps, but it protects only each hop, not the message itself, and the mail provider still sees everything. For truly private or verifiable email, the message must be protected end to end: encrypted so only the intended reader can open it, and signed so the recipient knows who wrote it and that nobody changed it.

Two standards do this: PGP (Pretty Good Privacy, standardized as OpenPGP) and S/MIME (Secure/Multipurpose Internet Mail Extensions). They use the same cryptography but differ in how they decide whether a public key really belongs to a person. This lesson explains how each one works, builds a small toy version of the hybrid scheme in Python, and compares the trust models. It builds on Public-Key Cryptography, Symmetric Encryption and Modes and TLS, Certificates and PKI.

What email encryption must provide

PGP and S/MIME offer four services:

Service Goal Mechanism
Confidentiality Only the recipient reads it Message encrypted with a random session key; that key encrypted with the recipient's public key
Authentication The sender is who they claim Hash of the message signed with the sender's private key
Integrity The message was not altered The signature fails if one bit changes
Non-repudiation The sender cannot easily deny it Only the private-key holder can produce the signature

Public-key operations are slow and limited in size, so both standards use hybrid encryption: a fast symmetric cipher (such as AES) encrypts the actual message with a one-time session key, and the slow public-key algorithm only encrypts that small key.

flowchart TB
    subgraph Sender
    M["Message"] --> H["Hash"] --> SIG["Sign with sender's private key"]
    M --> Z["Compress"]
    SIG --> Z
    Z --> E["Encrypt with random session key"]
    K["Session key"] --> E
    K --> WK["Encrypt session key with recipient's public key"]
    end
    E --> OUT["Send: encrypted message + encrypted session key"]
    WK --> OUT

Sign first, then compress and encrypt. The recipient reverses the steps: decrypt the session key, decrypt the message, verify the signature.

PGP in more detail

PGP was created by Phil Zimmermann in 1991, and OpenPGP is its open standard (implemented by GnuPG and many email plugins). A PGP user has a key pair and shares the public key, often on a keyserver or in a profile. To send a message you:

  1. Sign the message: compute a hash and encrypt it with your private key.
  2. Compress it (which also hides patterns and saves space).
  3. Encrypt the result with a fresh session key, and encrypt the session key for each recipient's public key. Anyone in the recipient list can read it.
  4. Encode the binary result as text (radix-64, also called ASCII armor) so email can carry it.

The receiver decrypts the session key with their private key, decrypts and decompresses the message, and verifies the signature with your public key. You can also use PGP only to sign (a "clear-signed" message) or only to encrypt.

A toy hybrid scheme

The code below follows the structure above using only the Python standard library. The "RSA" uses small Mersenne primes without padding, and the "cipher" is a SHA-256 keystream. It is an illustration, not secure; use GnuPG or a vetted library in practice.

Python Toy PGP: sign, encrypt, decrypt, verify

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The ciphertext looks random, Bob recovers the message and the signature verifies, and a flipped bit breaks verification (in the toy, it can even break parsing). Real PGP also uses authenticated encryption, padding schemes and algorithm negotiation.

S/MIME in more detail

S/MIME adds signing and encryption to the MIME email format and is built into Outlook, Apple Mail, Thunderbird and many mobile clients. It uses the same hybrid method, but its public keys come in X.509 certificates issued by certificate authorities (CAs), the same technology as website certificates (TLS, Certificates and PKI). A certificate binds an email address to a public key and is signed by a CA the recipient's software trusts. Organizations often issue S/MIME certificates to employees from a corporate CA and distribute them through the directory (Kerberos, LDAP and RADIUS).

The trust problem: web of trust vs CA hierarchy

Encryption is only as good as your belief that a public key belongs to the right person. If an attacker substitutes their own key for Bob's, you will happily encrypt to the attacker. The two systems answer "whose key is this?" differently.

PGP S/MIME
Trust model Web of trust: users sign each other's keys after verifying identity Hierarchy: CAs sign certificates; software ships with trusted roots
Key source Keyserver, website, QR code, in person Certificate in a signed message or directory
Who decides You, key by key The organization or client vendor chooses trusted CAs
Cost and setup Free software, manual key management Often paid or corporate-issued certificates, simpler for staff
Revocation Revocation certificate uploaded or shared CRL or OCSP
Typical use Individuals, activists, developers (signing releases) Companies, governments, regulated industries

The web of trust lets you accept a key if enough people you trust have signed it. GnuPG uses rules such as "a key is valid if signed by one fully trusted person or three marginally trusted ones". You verify the fingerprint (a hash of the key) out of band, for example by phone or in person.

Python Web-of-trust key validity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

bob_key is accepted because Carol is fully trusted; erin_key because two marginal signers vouch for it (Gina's signature counts for nothing); mallory_key has no signatures and is rejected.

Limits and practical advice

  • Metadata is not protected. Subject lines (in most clients), sender, recipient and timestamps remain visible. Only the body and attachments are encrypted.
  • Private keys are everything. Protect them with a strong passphrase, store backups safely, ideally keep them on a hardware token or smart card, and create a revocation certificate in advance (Randomness, Key Management and Secrets).
  • No forward secrecy. If a private key is stolen later, previously captured encrypted mail can be decrypted. Chat protocols that rotate keys avoid this.
  • Usability. Key exchange, lost keys and mixed client support limit adoption; scanning gateways and search cannot read encrypted mail, so organizations plan for that.
  • Verify fingerprints before trusting a key for sensitive communication, and remember a signed email shows who sent it, not that the content is true.
  • Phishing still works. A valid signature on a malicious message from a compromised account is still malicious (Phishing and Social Engineering).
  • Format bugs have occurred. Keep clients updated; some past attacks exploited how clients displayed or decrypted mixed content.

For many users, an encrypted messaging app is easier, but PGP and S/MIME remain important for signing software releases, regulated mail and long-lived archives.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do PGP and S/MIME use a session key instead of encrypting the whole message with the recipient's public key?

    Hybrid encryption combines the speed of a symmetric cipher with the key-distribution convenience of public-key cryptography.
  2. 2. Which key does the SENDER use to create a digital signature?

    Only the holder of the private key can produce a signature that the matching public key verifies.
  3. 3. What is the main difference between PGP and S/MIME trust?

    PGP users vouch for each other's keys, while S/MIME certificates are issued by CAs that clients already trust.
  4. 4. Which part of an encrypted email typically stays visible to mail servers?

    Message content is protected, but the metadata that routing needs is not, and many clients also leave the subject unencrypted.

Summary

  • PGP (OpenPGP) and S/MIME protect email end to end: confidentiality, authentication, integrity and non-repudiation.
  • Both use hybrid encryption: sign the hash with the sender's private key, encrypt with a one-time session key, and wrap that key with each recipient's public key.
  • PGP's keys are validated through a web of trust and fingerprints; S/MIME uses X.509 certificates from certificate authorities.
  • Metadata is not hidden, there is no forward secrecy, and key management is the hardest part.
  • Protect private keys, publish a revocation certificate plan, and verify fingerprints.

Next: Identifying Hashes, Ciphers and Steganography.