TExcelTAD
Home › Discrete Mathematics › The RSA Cryptosystem

The RSA Cryptosystem

RSA (Rivest, Shamir, Adleman, 1977) was the first practical public-key cryptosystem: anyone can encrypt a message with your public key, but only you can decrypt it. It still secures much of the web, code signing and SSH. Everything it needs is in the last three lessons: primes, modular inverses and Euler's theorem.

Key generation

  1. Pick two large distinct primes pp and qq (today 1024 bits or more each).
  2. Compute n=pqn = pq and ϕ(n)=(p−1)(q−1)\phi(n) = (p - 1)(q - 1).
  3. Pick ee with gcd⁡(e,ϕ(n))=1\gcd(e, \phi(n)) = 1 (usually e=65537e = 65537).
  4. Compute d=e−1 mod ϕ(n)d = e^{-1} \bmod \phi(n) with the extended Euclidean algorithm.

The public key is (n,e)(n, e); the private key is dd (and p,qp, q are kept secret too).

To encrypt a message mm (an integer with 0≤m<n0 \le m < n): c=me mod nc = m^e \bmod n. To decrypt: m=cd mod nm = c^d \bmod n.

A toy example

With p=61p = 61, q=53q = 53: n=3233n = 3233, ϕ(n)=3120\phi(n) = 3120, e=17e = 17, and d=17−1 mod 3120=2753d = 17^{-1} \bmod 3120 = 2753 (check: 17⋅2753=46801=15⋅3120+117 \cdot 2753 = 46801 = 15 \cdot 3120 + 1).

python · try it yourself
p, q = 61, 53
n, phi = p * q, (p - 1) * (q - 1)
e = 17
d = pow(e, -1, phi)
print("public key:", (n, e), " private d:", d)

message = "HI!"
ms = [ord(ch) for ch in message]                 # each character code is < n
cs = [pow(m, e, n) for m in ms]
print("plain codes:", ms)
print("ciphertext: ", cs)
back = [pow(c, d, n) for c in cs]
print("decrypted:  ", back, "->", "".join(chr(x) for x in back))
public key: (3233, 17)  private d: 2753
plain codes: [72, 73, 33]
ciphertext:  [3000, 1486, 1853]
decrypted:   [72, 73, 33] -> HI!

Why decryption works

We need (me)d=med≡m(modn)(m^e)^d = m^{ed} \equiv m \pmod{n}. Since ed≡1(modϕ(n))ed \equiv 1 \pmod{\phi(n)}, write ed=1+kϕ(n)=1+k(p−1)(q−1)ed = 1 + k\phi(n) = 1 + k(p-1)(q-1).

Work modulo pp first. If p∤mp \nmid m, Fermat gives mp−1≡1m^{p-1} \equiv 1, so

med=m⋅(mp−1)k(q−1)≡m⋅1=m(modp)m^{ed} = m \cdot \left(m^{p-1}\right)^{k(q-1)} \equiv m \cdot 1 = m \pmod{p}

If p∣mp \mid m, both sides are 0 mod p0 \bmod p. So med≡m(modp)m^{ed} \equiv m \pmod p in all cases; likewise modulo qq. Since pp and qq both divide med−mm^{ed} - m and are distinct primes, their product nn divides it (unique factorization). ■\blacksquare

Why it is believed secure

Computing dd from (n,e)(n, e) is easy if you know ϕ(n)\phi(n), and ϕ(n)\phi(n) is easy if you know pp and qq. So RSA's security rests on factoring nn being hard. The best known classical algorithm (the number field sieve) takes time that grows faster than any polynomial in the number of digits; a 2048-bit modulus is far out of reach. A large quantum computer running Shor's algorithm would break RSA, which is why "post-quantum" schemes are being deployed.

For the toy key, factoring is instant, and the attacker recovers the private key:

python · try it yourself
from sympy import factorint

n, e, c = 3233, 17, 3000
p, q = factorint(n)                # trivial for tiny n, infeasible for 2048-bit n
d = pow(e, -1, (p - 1) * (q - 1))
print("factors:", p, q, " recovered d:", d, " plaintext:", pow(c, d, n))
factors: 53 61  recovered d: 2753  plaintext: 72

Signatures

RSA also signs: the owner computes s=hd mod ns = h^d \bmod n for the hash hh of a document, and anyone checks se≡h(modn)s^e \equiv h \pmod n with the public key. Only the holder of dd can produce a valid ss.

Textbook RSA is not safe as is

The scheme above ("textbook RSA") is deterministic: the same message always gives the same ciphertext, so an attacker can test guesses. Small messages with small ee can be recovered by an ordinary integer root. Real systems add randomized padding (OAEP for encryption, PSS for signatures) and use vetted libraries. Never implement RSA yourself for production use.

Exercises

Exercise 1: Make a key

With p=11p = 11, q=17q = 17 and e=7e = 7, compute nn, ϕ(n)\phi(n) and dd.

Answer

n=187n = 187, ϕ(n)=160\phi(n) = 160, d=23d = 23.

Solution

n=11⋅17=187n = 11 \cdot 17 = 187, ϕ=10⋅16=160\phi = 10 \cdot 16 = 160. We need 7d≡1(mod160)7d \equiv 1 \pmod{160}: 7⋅23=161=160+17 \cdot 23 = 161 = 160 + 1, so d=23d = 23.

Exercise 2: Encrypt and decrypt (code)

With the key of Exercise 1, encrypt m=88m = 88 and decrypt the result.

Answer

c=11c = 11; decrypting gives 88.

Solution
python · try it yourself
n, e, d = 187, 7, 23
c = pow(88, e, n)
print(c, pow(c, d, n))
11 88

Exercise 3: Why must gcd⁡(e,ϕ(n))=1\gcd(e, \phi(n)) = 1?

What goes wrong if e=4e = 4 and ϕ(n)=160\phi(n) = 160?

Answer

ee has no inverse modulo ϕ(n)\phi(n), so there is no decryption exponent; and encryption is not injective.

Solution

gcd⁡(4,160)=4\gcd(4, 160) = 4, so no dd satisfies 4d≡1(mod160)4d \equiv 1 \pmod{160}. Moreover m↦m4m \mapsto m^4 maps mm and n−mn - m to the same value ((−m)4=m4(-m)^4 = m^4), so two messages share a ciphertext and decryption cannot be unique.

Test yourself

In RSA, which values form the public key?

  1. (p,q)(p, q)
  2. (n,e)(n, e)
  3. (n,d)(n, d)
  4. (ϕ(n),d)(\phi(n), d)

How is dd computed from ee?

  1. d=e2d = e^2
  2. d=e−1 mod ϕ(n)d = e^{-1} \bmod \phi(n)
  3. d=n−ed = n - e
  4. d=e mod nd = e \bmod n

RSA's security relies mainly on the difficulty of…

  1. computing gcds
  2. factoring n=pqn = pq
  3. fast exponentiation
  4. testing primality