MathLabs

Applied and computational mathematics

Cryptography

Using number theory and algebra to secure information, from RSA to modern encryption schemes.

IntuitionLocking a Message So Only the Right Person Can Open It

Imagine a padlock that anyone can snap shut, but that only one specific key can open. If everyone publishes their own padlock (but keeps their key secret), then anyone can lock a message for you, yet only you can read it back — no meeting in advance, no secret handshake needed. This is the core idea of public-key cryptography: the "locking" operation is easy to compute in one direction and, without the secret key, computationally infeasible to undo, even though the padlock design itself is public.

A point rotating around a unit circle, illustrating cyclic repetition analogous to modular exponentiation.
Modular multiplication x↦ax mod mx \mapsto a x \bmod m on Z/17Z\mathbb{Z}/17\mathbb{Z}: forward evaluation is instant, while inverting discrete-log / RSA power maps over large moduli is the basis of public-key cryptography.

SchoolModular Arithmetic: The Building Block

Definition: Congruence and modular exponentiation

Two integers aa and bb are congruent modulo nn, written a≡b(modn)a \equiv b \pmod{n}, when n∣(a−b)n \mid (a-b): their difference is an exact multiple of nn. Modular exponentiation is repeatedly multiplying a base by itself modulo nn, which is exactly the operation cryptographic schemes like RSA and Diffie-Hellman are built on, because it is fast to compute forward but hard to invert.

a≡b(modn)  ⟺  n∣(a−b)a \equiv b \pmod{n} \iff n \mid (a-b)

Here n∣(a−b)n \mid (a-b) means nn divides a−ba-b exactly, so a≡b(modn)a \equiv b \pmod{n} is really a statement about remainders: aa and bb leave the same remainder when divided by nn. Euler's theorem below extends Fermat's little theorem and is the engine behind why RSA decryption recovers the original message.

aϕ(n)≡1(modn)whenevergcd⁡(a,n)=1a^{\phi(n)} \equiv 1 \pmod n \quad \text{whenever} \quad \gcd(a,n)=1
Three cryptographic schemes compared
SchemeHard problem it relies onTypical key size (2026)
RSAFactoring n=pqn=pq into primes2048-4096 bits
Diffie-HellmanDiscrete logarithm modulo a prime2048-3072 bits
Elliptic-curve cryptography (ECC)Discrete logarithm on an elliptic curve group256-384 bits

UndergraduateWhy RSA Works, and How Diffie-Hellman Creates a Shared Secret

Let n=pqn = pq be a product of two distinct primes, and let ee and dd satisfy ed≡1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}. Then for every message mm with 0≤m<n0 \le m < n, encrypting with ee and decrypting with dd recovers the original message: (me)d≡m(modn)(m^e)^d \equiv m \pmod n.

Why is it true?

This is the theorem that makes RSA actually usable: it guarantees that whatever the sender encrypts with the public exponent, the holder of the private exponent can always decrypt, for every possible message, not just most of them — the proof has to handle the edge case where a message accidentally shares a factor with the modulus.

Proof

Since ed≡1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}, by definition of modular congruence there is a non-negative integer kk with ed=1+kϕ(n)ed = 1 + k\phi(n).

Case 1: gcd⁡(m,n)=1\gcd(m,n)=1. Euler's theorem gives mϕ(n)≡1(modn)m^{\phi(n)} \equiv 1 \pmod n, so raising both sides to the power kk and multiplying by mm gives med=m⋅(mϕ(n))k≡m⋅1k≡m(modn)m^{ed} = m \cdot (m^{\phi(n)})^{k} \equiv m \cdot 1^{k} \equiv m \pmod n.

Case 2: gcd⁡(m,n)≠1\gcd(m,n) \ne 1. Since n=pqn = pq, this means pp divides mm or qq divides mm (not both, since mm is smaller than nn). Working modulo pp: if pp divides mm then both mm and medm^{ed} are congruent to 00 modulo pp; otherwise gcd⁡(m,p)=1\gcd(m,p)=1 and Fermat's little theorem gives mp−1≡1(modp)m^{p-1} \equiv 1 \pmod p, and since (p−1)(p-1) divides ϕ(n)\phi(n) the same computation as Case 1 shows med≡m(modp)m^{ed} \equiv m \pmod p. The identical argument modulo qq shows med≡m(modq)m^{ed} \equiv m \pmod q.

By the Chinese Remainder Theorem, a congruence that holds modulo pp and modulo qq separately also holds modulo their product n=pqn=pq, so (me)d≡m(modn)(m^e)^d \equiv m \pmod n holds for every message mm, not just those coprime to nn.

Fix a prime pp and a base gg. If Alice picks a secret aa and sends A=ga mod pA = g^a \bmod p, and Bob picks a secret bb and sends B=gb mod pB = g^b \bmod p, then computing Ba mod pB^a \bmod p and Ab mod pA^b \bmod p both yield the same value gab mod pg^{ab} \bmod p, even though neither aa nor bb was ever transmitted.

Why is it true?

This is what lets two parties agree on a secret key over a public channel that an eavesdropper is watching: the eavesdropper sees the base, the modulus, and both public values, but recovering the shared secret from these requires solving a discrete logarithm, believed to be computationally hard for well-chosen parameters.

Proof

By definition A=ga mod pA = g^a \bmod p and B=gb mod pB = g^b \bmod p are the results of modular exponentiation, so Bob receives AA and computes Ba=(gb)a mod pB^a = (g^b)^a \bmod p, while Alice receives BB and computes Ab=(ga)b mod pA^b = (g^a)^b \bmod p.

Modular exponentiation obeys the same law of exponents as ordinary exponentiation, because repeated multiplication modulo pp composes exactly like repeated multiplication of integers, only reduced modulo pp at each step: (gb)a=gba=gab=(ga)b(g^b)^a = g^{ba} = g^{ab} = (g^a)^b as exponents of gg, and this equality survives reduction modulo pp at every stage.

Therefore Ba mod p=gab mod p=Ab mod pB^a \bmod p = g^{ab} \bmod p = A^b \bmod p: Alice and Bob independently compute the same quantity gab mod pg^{ab} \bmod p from their own secret exponent and the other party's public value, without either secret aa or bb ever appearing on the channel.

The security argument is separate from this correctness argument: correctness only shows both parties land on the same number; hardness of computing aa from gg, pp, and AA (the discrete logarithm problem) is what keeps that shared number secret from an observer.

UndergraduateReal-World Applications and Worked Examples

Every HTTPS connection, secure messaging app, and cryptocurrency wallet relies on the theorems above: TLS handshakes use Diffie-Hellman (or its elliptic-curve variant) to agree on a session key, banking systems and software updates use RSA or ECDSA signatures to guarantee authenticity, and blockchain wallets use elliptic-curve discrete logarithms to make a private key computationally infeasible to derive from a public address. The two worked examples below carry out RSA and Diffie-Hellman by hand on small numbers so every step is checkable.

Example: RSA by hand with small primes

Using primes p=5p=5 and q=11q=11, with public exponent e=3e=3 and message m=2m=2, find the ciphertext and verify that decryption recovers the message.

Solution

Step 1: Compute the modulus n=5×11=55n = 5 \times 11 = 55 and ϕ(n)=(5−1)(11−1)=40\phi(n) = (5-1)(11-1) = 40.

Step 2: Check gcd⁡(3,40)=1\gcd(3,40)=1, so e=3e=3 is a valid public exponent.

Step 3: Find the private exponent dd with 3d≡1(mod40)3d \equiv 1 \pmod{40}. Testing d=27d=27 gives 3×27=81=2×40+13 \times 27 = 81 = 2 \times 40 + 1, so d=27d=27 works.

Step 4: Encrypt m=2m=2: the ciphertext is c=23 mod 55=8 mod 55=8c = 2^3 \bmod 55 = 8 \bmod 55 = 8.

Step 5: Decrypt by computing c27 mod 55=827 mod 55c^{27} \bmod 55 = 8^{27} \bmod 55. Using repeated squaring modulo 55: 82=64≡98^2 = 64 \equiv 9, 84≡92=81≡268^4 \equiv 9^2 = 81 \equiv 26, 88≡262=676≡168^8 \equiv 26^2 = 676 \equiv 16, 816≡162=256≡368^{16} \equiv 16^2 = 256 \equiv 36. Since 27=16+8+2+127 = 16+8+2+1, multiply 816⋅88⋅82⋅81≡36⋅16⋅9⋅8(mod55)8^{16} \cdot 8^{8} \cdot 8^{2} \cdot 8^{1} \equiv 36 \cdot 16 \cdot 9 \cdot 8 \pmod{55}, which reduces step by step to 22.

Step 6: The decrypted value is 22, exactly the original message, confirming the RSA correctness theorem on this concrete instance.

Example: Diffie-Hellman key exchange by hand

With prime p=23p=23 and base g=5g=5, Alice picks secret a=6a=6 and Bob picks secret b=15b=15. Compute the shared secret both ways and verify they match.

Solution

Step 1: Alice computes A=56 mod 23A = 5^6 \bmod 23. Stepping through powers of 5 mod 23: 52=25≡25^2=25\equiv2, 53≡105^3\equiv10, 54≡45^4\equiv4, 55≡205^5\equiv20, 56≡85^6\equiv8. So A=8A=8.

Step 2: Bob computes B=515 mod 23B = 5^{15} \bmod 23. Using 56≡85^6\equiv8 from above, 512≡82=64≡185^{12}\equiv8^2=64\equiv18, and 515=512⋅53≡18⋅10=180≡195^{15}=5^{12}\cdot5^3\equiv18\cdot10=180\equiv19. So B=19B=19.

Step 3: Alice computes the shared secret as Ba mod p=196 mod 23B^a \bmod p = 19^6 \bmod 23. Since 19≡−419\equiv-4, 196≡(−4)6=46=409619^6\equiv(-4)^6=4^6=4096, and 4096=178×23+24096 = 178\times23+2, so this equals 22.

Step 4: Bob computes the shared secret as Ab mod p=815 mod 23A^b \bmod p = 8^{15} \bmod 23. Writing 8=238=2^3 gives 815=2458^{15}=2^{45}; since 211≡1(mod23)2^{11}\equiv1 \pmod{23} (a direct check: 211=2048=89×23+12^{11}=2048=89\times23+1), and 45=4×11+145=4\times11+1, this reduces to 21=22^{1}=2.

Step 5: Both computations give 22, confirming Alice and Bob agree on the same shared secret without ever transmitting a=6a=6 or b=15b=15 over the channel.

In RSA with n=55n=55, ϕ(n)=40\phi(n)=40, and public exponent e=3e=3, which value of dd satisfies 3d≡1(mod40)3d \equiv 1 \pmod{40}?

What underlying computational problem does the security of Diffie-Hellman key exchange rest on?

If gcd⁡(a,n)=1\gcd(a,n)=1, Euler's theorem states that aϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod n where ϕ(n)\phi(n) counts which of the following?

A bank wants to sign software updates so customers can verify they truly came from the bank and were not tampered with. Which cryptographic tool directly provides this guarantee?

References

  1. Alfred J. Menezes, Paul C. van Oorschot, Scott A. Vanstone (1996). Handbook of Applied Cryptography
  2. Peter W. Shor (1994). Algorithms for Quantum Computation: Discrete Logarithms and Factoring · arXiv:quant-ph/9508027
  3. National Institute of Standards and Technology (2024). Module-Lattice-Based Key-Encapsulation Mechanism Standard (FIPS 203)