← simple-learning

How RSA encryption works

A lock anyone can close and only one person can open. Built from two prime numbers. Named after Rivest, Shamir and Adleman, who found it in 1977.

Needs multiplication and remainders. Leads to HTTPS, digital signatures, key exchange.

Start with a padlock

You want to send a letter to a friend across town, and the courier reads everything. Your friend mails you an open padlock and keeps the only key. You put the letter in a box, snap the padlock shut, and send it. Anyone can close that padlock. Only your friend can open it. Nobody had to meet, nobody had to whisper a password.

The problem

That is exactly what you need online. Every message crosses wires and servers you do not own, and anyone in the middle can read it. The old fix is a shared secret agreed in advance, like a password you both know. That works for two spies who met once, not for talking to a shop you have never met, right now, over a channel everyone can see. You need the padlock: something anyone can close and only one person can open.

RSA is that padlock made of numbers. There are two keys: one locks and is given to the world, the other unlocks and stays with you. One way to recover the private key is to split a huge number into its two prime factors. With large, carefully chosen primes, that is very hard.

Alice

wants to send m

sees: public key

c = me mod n
Bob

made the keys

holds: private key

Eve

reads the wire

sees: c, e, n · not d

1Two primes

Bob picks them and tells nobody. Click two, or type your own.


      

2The modulus

Multiply them: that is n, and it goes public. From the same two primes make a second number, φ (phi): one less than each prime, multiplied. φ stays private, because with it anyone can work out the private key.

Before you look: how many digits will n have?


        
n is publicp, q, φ are private

3Public exponent e

Any number that shares no factor with φ, so their greatest common divisor, written gcd, is 1. The world standardised on 65537. Try another and nothing breaks.


      

4Private exponent d

The number that undoes e: e · d leaves remainder 1 when divided by φ. "mod" below means exactly that: the remainder after dividing. d is found by a few lines of division with remainders (Euclid's algorithm), which needs φ, which needs p and q.


        
public key · give to anyone
private key · never leaves Bob

5Encrypt, decrypt

A message is a number smaller than n. Alice raises it to the power e and keeps the remainder after dividing by n. Bob does the same to the result with d, and the original falls out. Text is a row of numbers, one per letter.


      

6Be Eve

You see n and e. One way to read the message is to find d, using φ. For that, split n back into p · q. Try the simplest way: divide by every number until one fits. Faster methods exist; this one is easy to watch.


      

?Check yourself

New numbers, same idea. Bob picks p = 11 and q = 13, and uses e = 7. Alice sends the number m = 2.

Things to try

  1. Change only e. The private key changes, the ciphertext changes, the decrypted text does not.
  2. Pick 5 and 11, with e = 3. Now n = 55: only the numbers 0 to 54 fit. The number n is public; its prime factors and d must stay secret.
  3. Pick two six-digit primes and press "Be Eve". Still instant. Real keys use primes of 300 digits. The same loop would run longer than the universe has existed.
  4. Type a message with an emoji on small primes. Its code is bigger than n, and the round trip loses it. Real RSA never encrypts raw text; it encrypts a short key for a faster cipher.

What this leaves out

Textbook RSA, which is what you just used, is deterministic: the same letter always becomes the same number, so Eve can build a dictionary. Real RSA pads the message with randomness first (a scheme called OAEP), and is mostly used to agree on a key for a symmetric cipher, one where both sides use the same key, such as AES, rather than to encrypt the data itself. The arithmetic is exactly what you saw. The engineering around it is what makes it safe.