Ch.21: Algorithms: How RSA Encryption Actually Works

Outline

Transcript

0:00 RSA is an encryption algorithm with two keys. Anyone can copy its public instructions and encrypt a message. The recipient keeps private instructions for getting that message back. Picture 1,000 public instruction cards and one private recovery card. The problem is keeping the message secret from everyone holding a public card. Even an observer trying to read the message can encrypt with that public card. We'll build deliberately insecure tiny keys, encrypt a quote and calculate how it comes back.

0:28 Then we'll see a successful recovery that exposes the secret numbers behind the key. Imagine a supplier sending a customer a confidential quote. A complete scheme prepares the quote's bytes as a number. We'll use two as a stand-in, not an actual encoded quotation. First, the customer makes the key before any quote arrives. Choose two different odd primes, three and 11, whole numbers above one divisible only by one and themselves. Multiply them: 33. Publish that product; keep the two ingredients secret.

1:00 So 33 goes on the public card, along with seven. Seven tells us how many copies of the input to multiply. The customer also calculates a private power from these primes. We'll reconstruct it after seeing the return trip. What does the supplier actually send? With those public instructions, encrypt our quote, two. Multiply seven twos together, then divide by 33. Keep only the remainder. Seven twos make 128. Three groups of 33 use 99. 29 is left. That's our ciphertext, the encrypted output we send.

1:32 The RSA specification defines that operation for inputs zero through 32. It and the original paper are linked in the description. An outsider can test guesses. Suppose the observer knows only two quotes are possible, represented by two and three. These two guesses need no research budget. Why not encrypt both with the public instructions? Two produces 29. Three produces nine. An intercepted 29 identifies the first quote. That's uncomfortable: the observer has read it without recovering the private key.

2:02 So the quote is exposed while the private instructions stay secret. The customer still needs recovery without that list of guesses. Applying public seven again to 29 leaves 17, not two. What does the private instruction do instead? So for this first return trip, take the customer's private power as three. We'll calculate where it came from shortly. Multiply three copies of ciphertext 29, keeping a running remainder. First, 29 times 29 makes 841. Divide by 33 and keep 16. For the 3rd copy, multiply 16 by the fixed 29.

2:37 Divide by 33 and keep two. We've recovered our original quote number. Nice, one square and one multiply. That's the square-and-multiply method; larger powers combine those operations. We kept 16 after the square; 29 stayed fixed as our next multiplier. But we threw away most of 841. Why does multiplying only 16 still recover two? So compare multiplying the full 841 by 29 with multiplying only 16 by 29. Both leave two after division by 33. The discarded part was whole groups of 33. So the removed amount still divides exactly by 33 after the next multiplication. Exactly.

3:17 Using the same dividing number, you can keep either factor's remainder at every multiplication. The discarded parts still contain only whole multiples of that number. Now we've got two back. How did the customer choose private three? Stay with our original two. If a product of twos leaves one under 33, one extra two restores two. We need one copy count that makes that happen. The secret primes give us two smaller tests for choosing that count. For prime three, any input not divisible by three leaves one after multiplying two copies. That's Fermat's little theorem: the guaranteed count is the prime minus one.

3:55 Another input can reach one sooner. For example, with two, two times two makes four, leaving one under three. For prime 11, the same theorem gives 10 copies, provided the input isn't divisible by 11. We need one count of our twos that works for both primes. Multiply those counts: two times 10 makes 20. 20 copies of our original two form 10 pairs for the three check, or two 10-copy groups for 11. Both checks leave one. I'm not sure how they combine. What does that tell us under 33? Next, join those prime checks.

4:28 Taking out 33 removes a multiple of both three and 11, so those smaller remainders stay unchanged. Below 33, the only numbers leaving one under 11 are one, 12 and 23. 12 fails the three check because it divides exactly; 23 leaves two. Only one also leaves one under three. Those two checks force the answer to be one. That's the Chinese remainder theorem: for two different primes, each pair of remainders identifies one remainder under their product. For any input divisible by neither prime, multiplying 20 copies leaves one under 33.

5:03 That's our 20-copy repeat. The original RSA paper uses that product of the prime-minus-one counts. So one whole 20-copy group leaves one. Multiplying by our one extra original two restores two. And that count came from the key's primes, before any quote arrived. Then we need 21 original twos: 20 for the group, one extra two. But we actually send 29. How do those 21 factors relate to our ciphertext? Public seven made each ciphertext copy from seven original twos. Keeping remainders preserves the result.

5:36 21 divided by seven gives three ciphertext copies. That's private three, positive and below 33. Now count the original twos: seven plus seven plus seven, 21 factors altogether. The same 21 twos form 1:20-copy group and one extra original two. The group leaves one; multiplying by the spare two gives two. That's why our private three recovers the original quote. That's satisfying: three ciphertext copies represent one whole repeat and one extra copy of the original message. If we send a different quote, do we still get 21 copies of that new message? With the same key, yes.

6:13 Public seven and private three give 21 copies of whichever message we're sending. For example, encrypt 27 with that same key. The ciphertext is three. The observer sees that three and knows public 33. Can they recover our secret factors from that three? 33 divided by three is 11. That transmission has exposed both of our secret prime factors. That's a nasty result. The first observer guessed a quote without finding a key; this one gets the ingredients for the private instruction. And does the customer recover 27 from that ciphertext three?

6:48 Yes. With private power three, multiply three copies of ciphertext three. Three times three makes nine; nine times three makes 27. Dividing by 33 leaves 27. Hold on, a recovery-only test would pass while the observer gets the private-key ingredients. Imagine a code reviewer asking, '27 recovers. Are we done?' No. Recovering 27 does not stop that key leak. Recovery gets the message back; secrecy keeps outsiders from reading it. Picture 100 pages in a transparent envelope. Every page arrives. Everyone along the route can read it.

7:23 But I'm missing why 27 recovers. It was divisible by three, outside our 20-copy guarantee under 33. Doesn't that break the argument we used for two? No. Use its two prime remainders: zero under three and five under 11. Every positive power of 27 stays divisible by three, so that check keeps zero. That side never needs to reach one. For the 11 check, we're multiplying 21 copies of that same message, 27. We want its original remainder, five. How do those 20-sevens restore five under 11? 11 doesn't divide 27, so our 20-copy group leaves one under 11.

8:03 Multiply by the extra 27, then divide by 11: the remainder is five. So both original remainders return: zero under three and five under 11. The uniqueness rule gives one matching remainder below 33, our original 27. For any allowed input, a zero stays zero; a nonzero remainder uses whole repeat groups and one extra original factor. That also covers multiples of 11 and zero. But recovery also depends on choosing the public power. Suppose we tried public two instead. Squaring two leaves four under 33.

8:38 Squaring 31 also leaves four. Two different inputs have the same encrypted output. The recipient gets four either way, so no recovery instruction can distinguish those original inputs. Where does our repeat-plus-one construction reject that choice? Every multiple of 20 is even; adding one makes it odd. No odd total divides evenly into groups of two. The factor two shared by public two and repeat 20 prevents the construction. And two isn't special here. Any shared factor above one divides every whole repeat.

9:10 Add one, and division by that factor leaves one. A public power sharing that factor can't divide the total either. Which public powers are allowed in the key recipe? So seven works with our count 20: they share no factor above one. That's the condition for finding the matching private power. The specification also bounds public powers: three through 32 for our dividing number 33; generally, at least three and below the dividing number. Knowing the primes lets the recipient construct that private instruction.

9:41 For suitable large primes, working backward from their public product is much harder than multiplying them. Does our factor-leaking transmission show how to break a real key? No. We deliberately chose 27 as a multiple of secret three. It tests an allowed input that still recovers. This tiny example doesn't tell us how often real prepared messages would expose factors. An observer can still encrypt a short list of guessed messages without knowing any secret factors. And the bare operation repeats its output whenever the input and key repeat.

10:14 How does a complete encryption scheme change that guessing comparison? The specification calls one such scheme OAEP; it prepares the message using fresh randomness before encryption. Send the same quote twice: different random choices can produce two different input numbers and therefore different ciphertexts. So encrypting my guessed quote once no longer gives one fixed answer to compare with every transmission. The customer applies the private operation, checks the recovered encoding, then gets the quote back. Right.

10:42 Randomness can repeat, and OAEP is a designed scheme, beyond what our tiny key can demonstrate. Real code should use a vetted complete implementation and a public key that belongs to the intended recipient. That complete implementation still uses the arithmetic roles we've followed: a public power, its matching private power and a shared dividing number. The secret primes let the recipient choose powers that restore the message. And getting the original number back is only the start of protecting the quote.

11:11 Thanks for listening to Learning Podcasts.