Asymmetric encryption

One solid key and three outlined copies of a differently shaped key, around the same padlock.

Picture a padlock lying open on a table. Anyone walking past is allowed to put something in it and click it shut. But open it? Only you can, with a key you never take out of your pocket. A lock like that doesn't exist in metal. In math it does — and in 1976 it solved the problem Caesar was already stuck with.

Words you might need

Key pair
Two keys that mathematically belong together and are made together. Whatever one locks, only the other can open.
Public key
The open padlock. Anyone can have it — feel free to put it on your profile. Whoever wants to send you something secret encrypts it with this.
Private key
The key in your pocket. It stays with you, always. Only this unlocks the padlock again.
Asymmetric
Two different keys for locking and unlocking. The opposite of symmetric, where it's the same key.
RSA
The first system that could do this, from 1977, named after its three inventors Rivest, Shamir and Adleman. It works with prime numbers that are hundreds of digits long.

The magic trick with paint

How can two people who've never met agree on a secret while everyone is listening in? This is the picture the inventors, Whitfield Diffie and Martin Hellman, used to explain it themselves:

  1. You and your friend pick a colour together, out loud: yellow. Everyone hears that.
  2. Each of you secretly picks your own colour. You pick red, she picks blue. You tell no one.
  3. Each of you mixes your secret colour with yellow, and sends the mixture to the other. Everyone sees orange and green go by.
  4. You mix the green you got with your red. She mixes the orange you sent with her blue. You both end up with the exact same brown.

The eavesdropper saw yellow, orange and green. But you can't unmix paint — he can't get your secret red and blue back out of it. You now have a shared secret, and no one else has it. In the real world, "mixing paint" is a calculation that's easy to do forward and impossible to undo.

How it flips the roles

Symmetric (previous chapter)Asymmetric (this chapter)
Keysone, and you have to share ittwo, and one may be seen by anyone
Agree in advanceyes, over a secure channelnothing
Speedlightning fastslow
How big can the message be?unlimitedvery small — smaller than the key

Try it yourself

The key pair is made in your browser and disappears as soon as you close this tab.

  1. Click Make a key pair. You'll see your public key — that long block of text is fine to give to anyone. The private one stays invisible inside your browser.
  2. Click Encrypt with the public key. The ciphertext is always exactly 256 bytes, no matter how short your message is.
  3. Click Decrypt with the private key. Your message comes back.
  4. Now paste a long text into the message field — a few paragraphs — and encrypt. It doesn't work. Read on to find out why.

Why you hit a wall

RSA with a 2048-bit key can only encrypt 190 bytes. That's not a setting you can turn up: the message has to stay smaller than the number the math is built on. A bigger key barely helps and makes everything slower. Asymmetric encryption is therefore not built for sending messages.

So what's it for? Two things symmetric encryption can't do: safely handing a secret key to someone you've never met (the paint trick), and signing. That second one is chapter seven. How the two worlds work together is chapter eight.

But first, something more elegant. RSA calculates with prime numbers 600 digits long. There's a more elegant way that achieves the same thing with much smaller numbers: calculating with points on a curved line. That line sits in your ID card, in your browser, and in Bitcoin. The next chapter lets you click on it.

This is math: prime numbers as a one-way street

13 × 17 = 221. You can work that out in your head. But give someone 221 and ask "which two prime numbers is this?", and they have to try: 3? 7? 11? 13! With three-digit numbers that's a minute of work. With 300-digit numbers, no computer on Earth can do it — while the multiplication still takes a fraction of a second. That lopsided relationship between forward and back is called a trapdoor function, and RSA is built entirely on it. Number theory, the field that for centuries was called "useless but beautiful", turns out to be the backbone of the internet.