Public-Key Cryptography

Chapter 7: Number Theory

Learning objectives

  • Describe the RSA key-generation procedure
  • Encrypt and decrypt a small RSA example by hand
  • Explain why RSA decryption recovers the plaintext, using Euler’s theorem
  • Identify the computational asymmetry that protects the private key
Code ChallengeInteractive figure — enable JavaScript to interact.

Public-key cryptography is the single most consequential application of elementary number theory in the history of human communication. Before 1976, secret communication required both parties to share a secret key in advance — meaning either a courier or a leaked tape would lose the war. Diffie and Hellman, then Rivest, Shamir, and Adleman, showed that you can publish your public key in a newspaper and still receive secret messages from anyone, without ever exchanging a private secret in advance. Every HTTPS handshake, every signed software update, every encrypted message you send today is built on this trick — and the math underneath is exactly Euler’s theorem from the previous section.

The RSA cryptosystem

RSA, named for Rivest, Shamir, Adleman (1977), turns Euler’s theorem into a working encryption scheme. The recipient generates a key pair:

  • Pick two large distinct primes p,qp, q (each typically 10241024 bits in production).
  • Compute the modulus n=pqn = pq and the totient ϕ(n)=(p1)(q1)\phi(n) = (p - 1)(q - 1).
  • Pick a public exponent ee with 1<e<ϕ(n)1 < e < \phi(n) and gcd(e,ϕ(n))=1\gcd(e, \phi(n)) = 1. (In practice, e=65537e = 65537.)
  • Compute the private exponent de1(modϕ(n))d \equiv e^{-1} \pmod{\phi(n)} via the extended Euclidean algorithm.

The public key is (n,e)(n, e) — published openly. The private key is dd — kept secret.

Encryption and decryption

To encrypt a message mm with 0m<n0 \leq m < n: compute c=memodnc = m^e \bmod n. To decrypt: compute m=cdmodnm = c^d \bmod n.

Why decryption recovers mm. Because ed1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}, write ed=1+kϕ(n)ed = 1 + k\phi(n) for some integer k0k \geq 0. Then, when gcd(m,n)=1\gcd(m, n) = 1, Euler’s theorem gives:

cd=(me)d=med=m1+kϕ(n)=m(mϕ(n))km1k=m(modn)c^d = (m^e)^d = m^{ed} = m^{1 + k\phi(n)} = m \cdot (m^{\phi(n)})^k \equiv m \cdot 1^k = m \pmod{n}

The remaining case gcd(m,n)>1\gcd(m, n) > 1 (meaning pmp \mid m or qmq \mid m) is handled separately using the Chinese Remainder Theorem and Fermat’s little theorem; the answer is still mm.

Worked example

Take p=5,q=11p = 5, q = 11, so n=55n = 55 and ϕ(n)=410=40\phi(n) = 4 \cdot 10 = 40. Pick e=3e = 3 (coprime to 4040). Compute dd: 3d1(mod40)3d \equiv 1 \pmod{40}, so d=27d = 27 (verify: 327=81=240+13 \cdot 27 = 81 = 2 \cdot 40 + 1). Public key (55,3)(55, 3), private key 2727. Encrypt m=2m = 2: c=23mod55=8c = 2^3 \bmod 55 = 8. Decrypt c=8c = 8: m=827mod55m = 8^{27} \bmod 55. Using repeated squaring inside Z/55Z\mathbb{Z}/55\mathbb{Z}, this reduces to m=2m = 2.

Why RSA is secure

An attacker who sees (n,e)(n, e) wants the private dd. To compute dd they need ϕ(n)=(p1)(q1)\phi(n) = (p - 1)(q - 1). To compute ϕ(n)\phi(n) they need the factorisation n=pqn = pq. Factoring a 2048-bit semiprime is believed to take longer than the age of the universe on any classical computer. Multiplying two primes is fast (polynomial time); factoring their product is, as far as anyone knows, hard. This one-way trapdoor — easy forward, hard backward — is the foundation of every public-key system that descends from RSA.

Pause and think: What would go wrong if Alice published her public key (n,e)(n, e) but accidentally also leaked ϕ(n)\phi(n)? Show that an attacker who knows nn and ϕ(n)=(p1)(q1)\phi(n) = (p - 1)(q - 1) can recover pp and qq (and hence dd) by solving a quadratic equation. The leak is total.

Try it

  • Set up a toy RSA system with p=3,q=11p = 3, q = 11. Compute nn, ϕ(n)\phi(n), and find a valid (e,d)(e, d) pair. Encrypt m=4m = 4 and verify the ciphertext.
  • Predict before computing: with n=55,e=3n = 55, e = 3, what is the ciphertext of m=10m = 10? (Hint: 103=100010^3 = 1000; reduce mod 5555.)
  • Explain in your own words why ee must be coprime to ϕ(n)\phi(n). (Hint: otherwise no dd inverts ee mod ϕ(n)\phi(n).)
  • Two-step puzzle: an attacker intercepts the same plaintext mm encrypted with two different public moduli (n1,e),(n2,e)(n_1, e), (n_2, e) using a small exponent like e=3e = 3. Look up the “Hastad broadcast attack” and explain why small ee plus low padding is dangerous.

A trap to watch for

Textbook RSA — encrypting memodnm^e \bmod n with no further processing — is dangerous in practice. Reasons: (1) encrypting the same mm twice produces the same ciphertext, so an attacker can match repeated messages; (2) small messages with small exponents (m3<nm^3 < n) can be recovered by taking ordinary integer cube roots, no factoring needed; (3) homomorphic structure (c1c2(m1m2)ec_1 \cdot c_2 \equiv (m_1 m_2)^e) lets an attacker manipulate ciphertexts. Production RSA always uses a padding scheme like OAEP that randomises the encoded message before exponentiation. The number-theoretic core in this section is correct — but real deployments wrap it carefully.

What you now know

You can generate an RSA key pair, encrypt and decrypt by hand on small primes, justify correctness via Euler’s theorem, and articulate the factoring-hardness assumption that protects the private exponent. You have now traversed the entire arc from gcd\gcd (§7.1) to a working encryption scheme — the journey is short, the destination is the modern internet. Velleman’s Chapter 8 turns to infinite sets and cardinality, where similar number-theoretic intuition will guide your first encounters with 0\aleph_0 versus c\mathfrak{c}.

References

  • Rivest, R. L., Shamir, A., Adleman, L. (1978). “A Method for Obtaining Digital Signatures and Public-Key Cryptosystems.” Communications of the ACM, 21(2), 120–126.
  • Diffie, W., Hellman, M. E. (1976). “New Directions in Cryptography.” IEEE Transactions on Information Theory, 22(6), 644–654.
  • Velleman, D. J. (2019). How to Prove It: A Structured Approach (3rd ed.). Cambridge University Press, §7.5.
  • Stinson, D. R., Paterson, M. B. (2018). Cryptography: Theory and Practice (4th ed.). CRC Press, ch. 5–6.
  • Koblitz, N. (1994). A Course in Number Theory and Cryptography (2nd ed.). Springer, ch. 4.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.