Cryptography / RSA encryption

RSA encryption

How Bob can publish a lock anyone may close, while keeping the only key that opens it. Seven steps, small numbers, no magic.

Beginner7 steps~6 min
Alice — wants to send a secret
Bob — builds and owns the keys
Eve — sees everything on the wire
// THE SHORT VERSION

In 30 seconds

RSA lets anyone lock a message that only one person can open. Bob multiplies two secret primes and publishes the product together with a public exponent: that pair is the lock.

Anyone can encrypt with it. Decrypting needs a private exponent, and working that out requires knowing the two primes.

It holds because multiplying two primes is easy, while splitting a huge product back into them takes longer than anyone can wait.

RSA encryption, step by step

STEPS01 / 07

Toy numbers keep the math visible. Real RSA keys use an n that is 2048 bits or longer.

STEP 01 / 07 Pick two primes
THE MATH
p = 61 q = 53
  1. Step 1: Pick two primes. Bob picks two prime numbers and tells no one. Everything else in RSA is built from these two.
  2. Step 2: Multiply them. n becomes part of the public key. Multiplying is instant; splitting a huge n back into p and q is the hard problem RSA relies on.
  3. Step 3: Compute φ(n). φ(n) counts the numbers below n that share no factor with it. Knowing p and q makes it trivial; knowing only n does not.
  4. Step 4: Choose public e. e must share no factor with 3120, so 3, 5 and 13 are out. Any survivor works; we take 17. Real systems usually use 65537.
  5. Step 5: Find private d. d is the number that undoes e. Finding it needs φ(n), which is why p and q must stay secret.
  6. Step 6: Encrypt. Alice turns her message into a number (65 is the letter A) and locks it with Bob’s public key. Anyone could do this step.
  7. Step 7: Decrypt. Raising to d reverses raising to e. Bob gets 65 back — and only he could, because only he knows d.
VALUES SO FAR
p61secret
q53secret
n····public
φ(n)····secret
e····public
d····secret
m····Alice
c····wire
// DIG DEEPER

The theory underneath

Step 2: Multiply them

Why nobody can split n

Why can’t Eve just split n back into p and q?

She can, for this n. 3233 is tiny. One of its two factors has to be smaller than √3233, which is about 57, so trying every number up to there finds 53 almost at once.

Try it. Then try the bigger ones and watch how the number of attempts grows.

So what changes with a real key?

Only the size, and the size is everything. A real n has 2048 bits, which is about 617 digits. Trying divisors one by one would take around 10³⁰⁸ attempts.

The best method anyone knows, the general number field sieve, is enormously faster than that and still nowhere near fast enough. The largest RSA-style number factored in public so far had 829 bits. It fell in 2020 and took roughly 2,700 core-years of computing.

61 × 53 = 3233        instant
3233 = ? × ?          56 tries
2048-bit n = ? × ?    10³⁰⁸ tries

Has anyone proved there is no fast way?

No, and it is worth being honest about that. Nobody has proved that factoring is hard. It is a bet, one that has held through centuries of very clever people trying to win it.

There is one known way to lose the bet. Shor’s algorithm factors quickly on a large quantum computer. No machine that large exists yet, but the prospect is the reason post-quantum cryptography is being rolled out now.

Step 3: Compute φ(n)

Euler’s totient φ(n)

Where does (p − 1)(q − 1) come from?

φ(n) answers one question: of the numbers from 1 to n, how many share no factor with n?

Pick two small primes and count for yourself. Red cells are multiples of p, blue cells are multiples of q, and the plain ones that are left are what φ counts.

Why is the count always (p − 1)(q − 1)?

Count what gets coloured in. There are q multiples of p and p multiples of q, and the very last cell, n itself, is on both lists.

So the plain cells number pq − p − q + 1, and that happens to factor neatly into (p − 1)(q − 1). With Bob’s primes it is 3233 − 53 − 61 + 1.

φ(pq) = pq − p − q + 1
      = (p − 1)(q − 1)
φ(3233) = 60 × 52 = 3120

Why does Bob have to keep φ(n) secret?

Because it hands over the primes. Take n − φ(n) + 1 and you get 114, which is p + q.

Now Eve knows two numbers that add up to 114 and multiply to 3233, and school algebra finds them. Knowing φ(n) is exactly as good as factoring n.

p + q = 3233 − 3120 + 1 = 114
p × q = 3233
x² − 114x + 3233 = 0
x = 57 ± 4   →   61 and 53

Is this what real RSA uses?

Almost. Modern standards swap φ(n) for a slightly smaller number, λ(n) = lcm(p − 1, q − 1), which is 780 here. It gives a shorter private key, 413 instead of 2753, and that key opens exactly the same messages.

Step 4: Choose public e · Step 5: Find private d

The extended Euclidean algorithm

How does Bob actually find d?

He needs the number that undoes 17 when you work mod 3120: d × 17 has to leave a remainder of 1. Guessing would take forever at real sizes.

A method about 2,300 years old does it in three lines. Divide, keep the remainder, and repeat with the smaller pair.

The last remainder before zero is the greatest common divisor. Here it is 1, which is also the check from step 4: 17 shares no factor with 3120.

3120 = 183 × 17 + 9
  17 =   1 × 9  + 8
   9 =   1 × 8  + 1

Fine, but where is d in that?

Run the same three lines backwards. Each remainder can be written using the two numbers above it, so you can unwind all the way to the top and express 1 using only 3120 and 17. That this is always possible is called Bézout’s identity.

Now look at the result mod 3120. The 3120 term disappears and you are left with −367 × 17 ≡ 1. So −367 undoes 17, and written as a positive remainder that is 2753.

1 = 9 − 8
  = 2 × 9 − 17
  = 2 × 3120 − 367 × 17
d = 3120 − 367 = 2753
17 × 2753 = 15 × 3120 + 1  ✓

What if Bob had picked a different e?

Try a few. Most of them give a d straight away. Some share a factor with 3120, and for those you will see the algorithm stop at a number bigger than 1. No d exists for them, which is exactly why step 4 ruled them out.

Could Eve run this too?

The method is public and fast. What she is missing is the 3120 to run it against. Without φ(n) there is nothing to divide into, and getting φ(n) means factoring n.

Step 5: Find private d · Step 7: Decrypt

Euler’s theorem

Why does raising to d undo raising to e?

Because of a fact Euler proved: take any number that shares no factor with n, raise it to the power φ(n), and mod n you land on 1. Every time.

Try some messages. The first line is Euler’s theorem; the second is the full trip, encrypt then decrypt.

How does that give the message back?

Decrypting computes (mᵉ)ᵈ, which is m raised to e × d. Bob chose d so that e × d is exactly one more than a multiple of φ(n).

Every full 3120 in the exponent turns into a 1, fifteen times over. What is left is a single m: the message.

e × d = 17 × 2753 = 1 + 15 × 3120
m^(ed) = m × (m³¹²⁰)¹⁵
       ≡ m × 1¹⁵ = m   (mod n)

But why is m to the φ(n) always 1?

One idea is enough. Write down all 3120 numbers that share no factor with n. Multiply every one of them by m. You get the same 3120 numbers back, in a different order.

So the product of the list equals m³¹²⁰ times the product of the list. Cancel the product from both sides and m³¹²⁰ ≡ 1 is all that remains. Fermat’s little theorem is the same argument for a prime n.

In the demo, 61 did not give 1. Is RSA broken for it?

Good catch. 61 shares a factor with n, so Euler’s theorem promises nothing about it. And yet the full trip still returned 61.

That means the theorem is not the whole story. The piece that covers every message is the Chinese remainder theorem, waiting on step 7.

Step 7: Decrypt

The Chinese remainder theorem

How does Bob decrypt four times faster?

By working with small numbers instead of big ones. The Chinese remainder theorem says: if you know what a number leaves mod 61 and what it leaves mod 53, you know the number mod 3233. The two remainders describe it completely.

Bob knows p and q, so he can decrypt in two small halves and stitch the answers together. Pick a message and watch both halves.

Why is that faster?

Each half uses numbers half as long and an exponent half as long. An exponentiation on half-length numbers costs about an eighth as much, so two of them cost a quarter. Every real RSA library decrypts this way.

dp = 2753 mod 60 = 53
dq = 2753 mod 52 = 49
2790 mod 61 = 45,  45⁵³ ≡ 4
2790 mod 53 = 34,  34⁴⁹ ≡ 12
4 and 12  →  65

And the message that was a multiple of 61?

This is where the proof gets finished. Look mod 61 only. If 61 divides m, then m and m^(ed) are both 0 there, so they agree. If it does not, Fermat’s little theorem says m⁶⁰ ≡ 1, and ed − 1 is a multiple of 60, so they agree again.

The same argument works mod 53. Two numbers that agree mod 61 and mod 53 agree mod 3233. So decryption returns m for every message, with no exceptions.

ed − 1 = 46800 = 60 × 780
                = 52 × 900
m^(ed) ≡ m   (mod 61)
m^(ed) ≡ m   (mod 53)
m^(ed) ≡ m   (mod 3233)

Is there a catch?

One. If a hardware glitch corrupts just one of the two halves, the wrong answer can give p away to anyone who sees it. Careful implementations check the result before letting it out.

Step 6: Encrypt

Modular arithmetic

How do you compute 65¹⁷ without a giant number?

65¹⁷ has 31 digits, and a real RSA power would have more digits than there are atoms in the universe. Nobody ever writes it out.

Mod arithmetic is clock arithmetic: after each operation you divide by n and keep only the remainder. The rule that saves the day is that you may do this at any point, and the final answer comes out the same.

a ≡ b (mod n)
  means n divides a − b
(a × b) mod n
  = ((a mod n) × (b mod n)) mod n

So what does Alice actually do?

She squares. 65², then the square of that for 65⁴, again for 65⁸, again for 65¹⁶, reducing mod 3233 each time so nothing ever grows past four digits. One more multiplication by 65 makes 65¹⁷.

That is five multiplications instead of sixteen. Change the message and watch the ladder.

Does that still work with a real key?

That is the whole point of it. The number of squarings grows with the length of the exponent, not with its size. A 2048-bit exponent needs about four thousand multiplications at most, which a laptop does in milliseconds.

The method is called square-and-multiply. The modular arithmetic lesson builds it from scratch.

Step 6: Encrypt

Padding and real key sizes

Is this how RSA is really used?

Not quite, and the difference matters. What this page shows is called textbook RSA: the message goes straight into the formula. The trouble is that the same message always produces the same ciphertext.

Pick a letter for Alice to send. Eve sees only the ciphertext. Watch what she does with it.

How did Eve do that without the private key?

She never decrypted anything. The public key is public, so she encrypted every possible letter herself and looked for a match.

Whenever the list of likely messages is short (yes or no, a PIN, a name) textbook RSA gives the message away.

Is anything else wrong with it?

Two more things. Multiply two ciphertexts and you get the ciphertext of the two messages multiplied, so Eve can change a message without ever reading it.

And if a message is so small that mᵉ never wraps past n, an ordinary e-th root recovers it.

E(2) × E(65) = 1752 × 2790
             ≡ 3017  (mod 3233)
E(130)       = 3017

So what do real systems do?

They pad. A scheme called OAEP mixes the message with fresh random bytes before the exponentiation, so the same message encrypts differently every time and a tampered ciphertext is rejected.

Usually RSA encrypts only a short random key, and a faster symmetric cipher encrypts the message itself. The numbers are also much bigger: n here has 12 bits, while real keys use at least 2048, and e is almost always 65537.

// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?