RSA Encryption

RSA encryption is a public-key method that hides a message with modular powers. You publish n = pq, the product of two secret primes, and an exponent e. Anyone can encrypt a number m as c ≡ me (mod n), but only you can decrypt with m ≡ cd (mod n), because finding d requires factoring n.

Before this lesson: Fermat’s Little Theorem, Euclidean Algorithm and Chinese Remainder Theorem

The idea of public-key encryption

Picture a mailbox with a slot. Anyone can drop a letter in, but only the owner has the key that opens it. RSA works the same way: the public key (n, e) lets anyone lock a message, and the private key d is the only practical way to unlock it.

The lock is modular exponentiation, which is easy to do. Undoing it without d is believed to be as hard as factoring n, and nobody knows a fast way to factor a product of two primes with hundreds of digits. The method is named for Ron Rivest, Adi Shamir and Leonard Adleman, who published it in 1977.

A flow diagram with three boxes from left to right. The first box holds the message m = 9. An arrow labeled
Anyone can lock 9 into 48 with the public key, but turning 48 back into 9 takes the private key d = 103.

How RSA works

Making the keys takes five steps. Here φ(n) is the number of integers from 1 to n that are relatively prime to n; for n = pq it equals (p − 1)(q − 1).

  1. Choose two different primes p and q, and keep them secret.
  2. Compute n = pq. This is public.
  3. Compute φ(n) = (p − 1)(q − 1). Keep it secret.
  4. Choose a public exponent e with 1 ≤ e ≤ φ(n) and gcd(e, φ(n)) = 1.
  5. Find the private exponent d, the inverse of e mod φ(n), with the extended Euclidean algorithm.

A message is first turned into a number m with 0 ≤ m ≤ n − 1. Then:

\text{encrypt: } c \equiv m^{e} \pmod{n} \qquad \text{decrypt: } m \equiv c^{d} \pmod{n}

Why decryption works. Since ed ≡ 1 (mod φ(n)), we can write ed = 1 + k(p − 1)(q − 1). If p does not divide m, then Fermat’s little theorem gives mp−1 ≡ 1 (mod p), so med = m · (mp−1)k(q−1) ≡ m (mod p). If p divides m, both sides are 0 mod p. The same holds mod q, so the Chinese remainder theorem gives med ≡ m (mod n).

c^{d} \equiv (m^{e})^{d} = m^{ed} = m^{1 + k\varphi(n)} \equiv m \pmod{n}

Real keys use primes with about 300 digits each, so n has about 617 digits (2048 bits), and e is usually 65,537. The examples below use small primes so every step can be done by hand.

RSA encryption examples

Example 1: Making a key pair

Build RSA keys from p = 11 and q = 13 with e = 7.

n = 11 \cdot 13 = 143

Multiply the primes. This is the public modulus.

\varphi(n) = 10 \cdot 12 = 120

Multiply p − 1 and q − 1.

\gcd(7, 120) = 1

7 is prime and does not divide 120, so e = 7 is allowed.

120 = 7 \cdot 17 + 1

Divide 120 by 7 (the first step of the Euclidean algorithm).

1 = 120 - 7 \cdot 17

Solve for the remainder 1.

7 \cdot (-17) \equiv 1 \pmod{120}

Reduce mod 120, so −17 is an inverse of 7.

d \equiv -17 + 120 = 103

Add 120 to make d positive.

Answer: public key (n, e) = (143, 7), private key d = 103. Check: 7 · 103 = 721 = 6 · 120 + 1.

Example 2: Encrypting a number

Use the public key (143, 7) to encrypt m = 9. Compute 97 mod 143 by repeated squaring, reducing after every multiplication.

9^2 = 81

Square 9. 81 is less than 143, so no reduction is needed.

9^4 = 81^2 = 6561

Square again.

6561 = 143 \cdot 45 + 126

Reduce mod 143: 94 ≡ 126.

9^7 = 9^4 \cdot 9^2 \cdot 9

Split the exponent: 7 = 4 + 2 + 1.

126 \cdot 81 = 10206 = 143 \cdot 71 + 53

Multiply the first two factors and reduce.

53 \cdot 9 = 477 = 143 \cdot 3 + 48

Multiply by the last factor and reduce.

Answer: c = 48. The ciphertext 48 is what travels over the network.

Example 3: Encrypting a word (word problem)

Maya wants to send the word HI to the owner of the key (143, 7). She codes letters as A = 1, B = 2, …, Z = 26 and encrypts each letter separately. What does she send?

H = 8,\ I = 9

Turn the letters into numbers.

8^2 = 64

Square 8.

8^4 = 64^2 = 4096 = 143 \cdot 28 + 92

Square again and reduce mod 143.

8^7 = 8^4 \cdot 8^2 \cdot 8

Split the exponent: 7 = 4 + 2 + 1.

92 \cdot 64 = 5888 = 143 \cdot 41 + 25

Multiply and reduce.

25 \cdot 8 = 200 = 143 + 57

Multiply by 8 and reduce.

9^7 \equiv 48 \pmod{143}

Use the result of Example 2 for I.

Answer: Maya sends 57, 48. Encrypting one letter at a time is only for practice: the same letter always gives the same number, so real systems encrypt larger blocks with random padding.

Example 4: Decrypting with the Chinese remainder theorem

Decrypt c = 48 with the private key d = 103 and n = 143 = 11 · 13. Instead of computing 48103 mod 143 directly, work mod 11 and mod 13.

48 \equiv 4 \pmod{11}

Reduce the base: 48 = 44 + 4.

103 = 10 \cdot 10 + 3

By Fermat’s little theorem, reduce the exponent mod 10.

4^3 = 64 \equiv 9 \pmod{11}

Compute and reduce: 64 = 55 + 9.

48 \equiv 9 \pmod{13}

Reduce the base: 48 = 39 + 9.

103 = 12 \cdot 8 + 7

Reduce the exponent mod 12.

9^3 = 729 = 13 \cdot 56 + 1 \equiv 1 \pmod{13}

Find a small power of 9 that is 1 mod 13.

9^7 = (9^3)^2 \cdot 9 \equiv 9 \pmod{13}

Split the exponent: 7 = 6 + 1.

m \equiv 9 \ (11),\ m \equiv 9 \ (13)

Combine with the Chinese remainder theorem.

m \equiv 9 \pmod{143}

9 satisfies both, and the solution is unique mod 143.

Answer: m = 9, the message from Example 2. Real RSA software uses this shortcut, because two half-size powers are much faster than one full-size power.

Example 5: Breaking a tiny key

Eve intercepts c = 82, sent with the public key (n, e) = (91, 5). Since 91 is small, she can factor it. Find the private key and the message.

91 = 7 \cdot 13

Factor n by trying small primes.

\varphi(91) = 6 \cdot 12 = 72

Compute φ(n) from the factors.

72 = 5 \cdot 14 + 2,\quad 5 = 2 \cdot 2 + 1

Run the Euclidean algorithm on 72 and 5.

1 = 5 - 2 \cdot 2 = 5 - 2(72 - 5 \cdot 14)

Back-substitute the remainder 2.

1 = 5 \cdot 29 - 2 \cdot 72

Collect the multiples of 5 and of 72, so d = 29.

82 \equiv 5 \pmod{7},\ 29 = 6 \cdot 4 + 5

Mod 7: reduce the base, and reduce the exponent mod 6.

5^5 = 5^4 \cdot 5 \equiv 2 \cdot 5 = 10 \equiv 3 \pmod{7}

52 = 25 ≡ 4, so 54 ≡ 16 ≡ 2.

82 \equiv 4 \pmod{13},\ 29 = 12 \cdot 2 + 5

Mod 13: reduce the base, and reduce the exponent mod 12.

4^5 = 4^4 \cdot 4 \equiv 9 \cdot 4 = 36 \equiv 10 \pmod{13}

42 = 16 ≡ 3, so 44 ≡ 9.

m \equiv 3 \ (7),\ m \equiv 10 \ (13)

Combine: 10 leaves remainder 3 when divided by 7.

Answer: d = 29 and m = 10. Check: 105 = 100,000 = 91 · 1098 + 82. With a 617-digit n, the first step, factoring, is far beyond any known computer.

Common mistakes

MistakeExample of the errorFix
Finding d mod n instead of mod φ(n)Inverting 7 mod 143Invert e mod φ(n) = 120, which gives d = 103.
Choosing e that shares a factor with φ(n)e = 5 with φ(n) = 120gcd(5, 120) = 5, so 5 has no inverse mod 120. Choose e with gcd(e, φ(n)) = 1.
Encrypting a message that is too largem = 150 with n = 143 decrypts to 7Keep 0 ≤ m ≤ n − 1, or split the message into smaller blocks.
Computing the whole power before reducingWriting out 97 = 4,782,969 firstReduce mod n after every multiplication, as in Example 2.
Treating n like a primeAssuming 2142 ≡ 1 (mod 143), as Fermat’s little theorem would for a prime143 = 11 · 13 is not prime, and 2142 ≡ 114 (mod 143). Work mod 11 and mod 13, reducing the exponent mod 10 and mod 12.
Making p, q or φ(n) publicPublishing φ(n) = 120 with the keyAnyone who knows φ(n) can compute d. Publish only n and e.

Related ideas: digital signatures

The keys also work in reverse. To sign a message m, the owner computes s ≡ md (mod n) with the private key. Anyone can check the signature by computing se mod n with the public key and comparing it with m. With the key (143, 7, 103), signing m = 2 gives s ≡ 2103 ≡ 63 (mod 143), and checking gives 637 ≡ 2 (mod 143).

Every step of RSA comes from earlier lessons in this unit: modular arithmetic for the powers, the Euclidean algorithm for d, linear congruences for the inverse, and the Chinese remainder theorem for fast decryption.

Practice problems

  1. For p = 3 and q = 11, find n and φ(n).
  2. With φ(n) = 20 and e = 3, find d.
  3. Encrypt m = 5 with the public key (33, 3).
  4. Decrypt c = 26 with n = 33 and d = 7.
  5. Why can’t e = 4 be used when φ(n) = 20?
  6. For p = 7, q = 17 and e = 5, find n, φ(n) and d.
  7. Encrypt m = 2 with the public key (119, 5).
  8. Decrypt c = 32 with n = 119 and d = 77, using the Chinese remainder theorem.
  9. Break the public key (35, 5): find d, then decrypt c = 33.
  10. A friend sends you 47 using your public key (143, 7) from Example 1, with letters coded A = 1, …, Z = 26. Use d = 103 to find the letter.
Show answers

1.

n = 3 \cdot 11 = 33

Multiply the primes.

\varphi(n) = 2 \cdot 10 = 20

Multiply p − 1 and q − 1.

Answer: n = 33 and φ(n) = 20

2.

20 = 3 \cdot 6 + 2,\quad 3 = 2 \cdot 1 + 1

Run the Euclidean algorithm.

1 = 3 - 2 = 3 - (20 - 3 \cdot 6)

Back-substitute the remainder 2.

1 = 3 \cdot 7 - 20

Collect the multiples of 3.

Answer: d = 7. Check: 3 · 7 = 21 ≡ 1 (mod 20)

3.

5^3 = 125

Compute the power.

125 = 33 \cdot 3 + 26

Reduce mod 33.

Answer: c = 26

4.

26 \equiv 2 \pmod{3},\ 2^7 \equiv 2 \pmod{3}

Mod 3: 2 ≡ −1, and an odd power of −1 is −1 ≡ 2.

26 \equiv 4 \pmod{11}

Mod 11: reduce the base. The exponent 7 is already less than 10.

4^2 = 16 \equiv 5,\ 4^4 \equiv 25 \equiv 3 \pmod{11}

Square twice, reducing each time.

4^7 = 4^4 \cdot 4^2 \cdot 4 \equiv 3 \cdot 5 \cdot 4 = 60 \equiv 5 \pmod{11}

Split the exponent: 7 = 4 + 2 + 1.

m \equiv 2 \ (3),\ m \equiv 5 \ (11)

Combine: 5 leaves remainder 2 when divided by 3.

Answer: m = 5, the message from Problem 3

5.

\gcd(4, 20) = 4

4 divides 20.

Answer: e must be relatively prime to φ(n), or it has no inverse d mod 20

6.

n = 7 \cdot 17 = 119,\ \varphi(n) = 6 \cdot 16 = 96

Multiply the primes, then multiply p − 1 and q − 1.

96 = 5 \cdot 19 + 1

Divide 96 by 5.

5 \cdot (-19) \equiv 1 \pmod{96}

Solve for the remainder 1 and reduce.

d \equiv -19 + 96 = 77

Add 96.

Answer: n = 119, φ(n) = 96, d = 77. Check: 5 · 77 = 385 = 4 · 96 + 1

7.

2^5 = 32

32 is less than 119, so no reduction is needed.

Answer: c = 32

8.

32 \equiv 4 \pmod{7},\ 77 = 6 \cdot 12 + 5

Mod 7: reduce the base, and reduce the exponent mod 6.

4^5 = 4^3 \cdot 4^2 \equiv 1 \cdot 2 = 2 \pmod{7}

43 = 64 ≡ 1 and 42 = 16 ≡ 2.

32 \equiv -2 \pmod{17},\ 77 = 16 \cdot 4 + 13

Mod 17: 32 = 34 − 2, and reduce the exponent mod 16.

(-2)^{13} = -2^{13} = -(2^8 \cdot 2^4 \cdot 2)

An odd power keeps the minus sign; split 13 = 8 + 4 + 1.

-(1 \cdot (-1) \cdot 2) = 2 \pmod{17}

24 = 16 ≡ −1, so 28 ≡ 1.

m \equiv 2 \ (7),\ m \equiv 2 \ (17)

m = 2 satisfies both.

Answer: m = 2, the message from Problem 7

9.

35 = 5 \cdot 7,\ \varphi(35) = 4 \cdot 6 = 24

Factor n and compute φ(n).

5 \cdot 5 = 25 = 24 + 1

Try small inverses: 5 is its own inverse mod 24, so d = 5.

33 \equiv -2 \pmod{35}

Rewrite the ciphertext to make the power easy.

(-2)^5 = -32 \equiv 3 \pmod{35}

Compute and add 35.

Answer: d = 5 and m = 3

10.

47 \equiv 3 \pmod{11},\ 3^3 = 27 \equiv 5 \pmod{11}

Mod 11: 47 = 44 + 3, and 103 ≡ 3 (mod 10).

47 \equiv 8 \pmod{13},\ 8^2 = 64 \equiv -1 \pmod{13}

Mod 13: 47 = 39 + 8, and 64 = 65 − 1.

8^7 = (8^2)^3 \cdot 8 \equiv (-1)^3 \cdot 8 = -8 \equiv 5 \pmod{13}

103 ≡ 7 (mod 12); split 7 = 6 + 1.

m \equiv 5 \ (11),\ m \equiv 5 \ (13)

m = 5 satisfies both.

Answer: m = 5, which is the letter E

RSA encryption FAQ

What is RSA encryption?

RSA is a public-key method. You publish n = pq and an exponent e, anyone encrypts m as c ≡ me (mod n), and only you can decrypt with m ≡ cd (mod n), where d is the inverse of e mod (p − 1)(q − 1).

Why is RSA secure?

Finding d requires φ(n) = (p − 1)(q − 1), and the only known practical way to get it is to factor n. Multiplying two 300-digit primes is fast, but no known method can factor their 617-digit product in a reasonable time.

How do you find the private key d in RSA?

Run the extended Euclidean algorithm on φ(n) and e to write 1 = ex + φ(n)y. Then d is x reduced mod φ(n). For e = 7 and φ(n) = 120, this gives d = 103.

Why does RSA decryption give back the original message?

Because ed = 1 + kφ(n), Fermat’s little theorem makes med ≡ m mod p and mod q, and the Chinese remainder theorem combines these into med ≡ m (mod n).

Summary

  • Keys: choose primes p and q, set n = pq and φ(n) = (p − 1)(q − 1), choose e with gcd(e, φ(n)) = 1, and find d ≡ e−1 (mod φ(n)).
  • Publish (n, e); keep p, q, φ(n) and d secret.
  • Encrypt with c ≡ me (mod n) and decrypt with m ≡ cd (mod n), reducing after every multiplication.
  • Decryption works because ed ≡ 1 (mod φ(n)), by Fermat’s little theorem and the Chinese remainder theorem.
  • The security rests on how hard it is to factor n.

Previous lesson: Fermat’s Little Theorem

Next lesson: Sum and Product Rules for Counting

Back to the Discrete Math Index