8. RSA & Public-Key Cryptography

Encrypt with one key, decrypt with another

Every cipher we've seen so far is symmetric: the same key encrypts and decrypts. This creates a chicken-and-egg problem — how do you securely share the key in the first place? You'd need encryption to protect the key, but you need the key to have encryption.

In 1977, Ron Rivest, Adi Shamir, and Leonard Adleman published a revolutionary idea: a cipher with two keys. One key encrypts; the other decrypts. You can publish the encryption key to the whole world — it's your public key. Only your private key can decrypt. This is asymmetric encryption.

The Core Idea

Symmetric vs. asymmetric encryption

Symmetric (AES):
  Alice ──key──→ Bob     (must share key secretly first)
  Alice ─encrypt(key, msg)─→ Bob ─decrypt(key, msg)─→ plaintext

Asymmetric (RSA): Bob publishes his public key to the world Alice ─encrypt(bob_public, msg)─→ Bob ─decrypt(bob_private, msg)─→ plaintext No secret exchange needed!

Trapdoor Functions

RSA relies on a trapdoor one-way function: a computation that's easy to do in one direction but practically impossible to reverse — unless you know a secret (the trapdoor). For RSA, that function is modular exponentiation, and the trapdoor is the prime factorization of a large number.

How RSA Works

  1. Choose two large primes p and q

    In practice, each is 1024+ bits. Their product n = p × q is the modulus.

  2. Compute φ(n) = (p−1)(q−1)

    This is Euler's totient — the count of numbers less than n that are coprime to n. This value stays secret.

  3. Choose e (public exponent)

    Pick a small number coprime to φ(n). The standard choice is e = 65537 (0x10001).

  4. Compute d (private exponent)

    Find d such that e × d ≡ 1 (mod φ(n)). This is the modular multiplicative inverse, found using the extended Euclidean algorithm.

Public Key

(n, e)
Published openly. Used to encrypt.

Private Key

(n, d)
Kept secret. Used to decrypt.

Encryption and Decryption

Encrypt:  C = M^e mod n     (raise message to power e, mod n)
Decrypt:  M = C^d mod n     (raise ciphertext to power d, mod n)

This works because of Euler's theorem: M^(e·d) mod n = M^(1 + k·φ(n)) mod n = M mod n

A Small Worked Example

Toy RSA with small primes

Step 1: Choose primes p = 61, q = 53
Step 2: n = 61 × 53 = 3233
Step 3: φ(n) = (61-1)(53-1) = 60 × 52 = 3120
Step 4: Choose e = 17  (coprime to 3120)
Step 5: Find d such that 17 × d ≡ 1 (mod 3120)
        d = 2753  (because 17 × 2753 = 46801 = 15 × 3120 + 1)

Public key: (n=3233, e=17) Private key: (n=3233, d=2753)

Encrypting the message M = 65

C = 65^17 mod 3233 = 2790

Decrypting: M = 2790^2753 mod 3233 = 65 ✓

In practice, the primes are 2048+ bits long, making n a number with over 600 digits. The modular exponentiation is computed efficiently using repeated squaring.

Why RSA Is Hard to Break

To find d from the public key (n, e), an attacker needs φ(n), which requires knowing p and q — the prime factors of n. This is the integer factorization problem.

Multiplication is Easy, Factoring is Hard

Multiplying two 1024-bit primes takes microseconds. Factoring their 2048-bit product is believed to require more computation than exists in the universe. The largest RSA number factored to date (RSA-250, 829 bits) took 2700 CPU-years in 2020. A 2048-bit RSA key is astronomically harder.

Digital Signatures

RSA can also work in reverse: encrypt with the private key, and anyone can verify with the public key. This creates a digital signature.

Signing and verifying

Signing (private key):
  signature = hash(message)^d mod n

Verifying (public key): recovered_hash = signature^e mod n compare recovered_hash with hash(message) if equal → signature is valid (message is authentic)

Only the holder of the private key could have produced a valid signature, but anyone with the public key can verify it. This proves both authenticity (it came from the claimed sender) and integrity (the message wasn't modified).

RSA in Practice

Raw RSA has important limitations:

  • Slow — RSA is ~1000x slower than AES. You don't encrypt bulk data with RSA
  • Deterministic — the same message always produces the same ciphertext (without padding)
  • Small messages only — you can only encrypt data smaller than n (typically 256 bytes for RSA-2048)

The solution: hybrid encryption. Use RSA to encrypt a random AES key, then use AES to encrypt the actual data.

Hybrid encryption (how TLS actually works)

1. Alice generates a random 256-bit AES key
2. Alice encrypts the AES key with Bob's RSA public key
3. Alice encrypts the message with AES-GCM using that key
4. Alice sends: RSA_encrypt(aes_key) + AES_encrypt(message)

Bob:

  1. Decrypts the AES key using his RSA private key
  2. Decrypts the message using AES

RSA protects the key exchange; AES protects the data.

Additionally, raw RSA must use a padding scheme like OAEP (Optimal Asymmetric Encryption Padding) to add randomness and prevent various attacks on the mathematical structure.

Key Size Comparison

Security level Symmetric (AES) RSA key size
80 bits — 1024 bits (deprecated)
112 bits — 2048 bits (minimum today)
128 bits 128 bits 3072 bits
192 bits 192 bits 7680 bits
256 bits 256 bits 15360 bits

RSA keys are much larger than symmetric keys for equivalent security because factoring is easier than brute-forcing a key space. This is one reason elliptic curve cryptography (page 8) has become popular — it achieves the same security with much smaller keys.

Real-World Usage

System How it uses RSA
TLS certificates RSA signatures verify server identity (though ECDSA is replacing it)
PGP / GPG RSA for key exchange and signing of emails
SSH RSA key pairs for authentication (ssh-rsa, being phased out)
Code signing RSA signatures verify software authenticity
JWT tokens RS256 = RSA signature with SHA-256

Try It Yourself — RSA Key Generator

Pick two small primes and watch RSA key generation unfold step by step. The widget computes n, φ(n), and finds the private exponent d.

Encrypt & Decrypt

Enter a number smaller than n and see RSA encryption and decryption in action. The math uses the keys generated above.

Digital Signature Demo

RSA works in reverse too: sign with the private key, verify with the public key. Enter a number to see signing and verification.

Key Takeaways

  • RSA uses two keys: a public key (n, e) for encryption and a private key (n, d) for decryption
  • Security relies on the difficulty of factoring large numbers
  • RSA is slow — in practice it's only used to encrypt small values (like AES keys), not bulk data
  • Hybrid encryption = RSA for key exchange + AES for data — this is how TLS works
  • RSA can also create digital signatures: sign with private, verify with public
  • RSA keys are large (2048+ bits) compared to symmetric keys (128-256 bits) for equivalent security

RSA solved the key distribution problem, but it requires one party to choose and send a key. What if two parties could jointly compute a shared secret over a public channel, without either one choosing it alone? That's Diffie-Hellman key exchange.