;colony/science  / Computers, Visually  / How does encryption keep a secret?
Computers, Visually

How does encryption keep a secret?

Two strangers, shouting across a crowded room where everyone can hear, can still agree on a secret password nobody else learns. The trick is a sum that's easy to do and almost impossible to undo.

Plate 58 — A secret in the open Diffie–Hellman · easy to mix, hard to un-mix
Step through the paint swap — both reach a blend a spy can’t un-stir.
Predict firstBefore you swap colours: can two people agree a secret in plain sight a spy can't copy?
public colouranyone can see thisAliceBobEveryone agrees on one starting colour, out loud. Spies can hear it too.
PLATE 58 · A SECRET IN THE OPEN
The exchange step 1 / 4
A public colour: Everyone agrees on one starting colour, out loud. Spies can hear it too.
Step
1of 4
Secret matches
Imagine Alice and Bob want a secret colour, but they can only shout to each other across a crowded room. They agree on a public colour everyone hears. Each secretly picks their own colour and mixes it into the public one, then swaps the blends openly. Finally each stirs in their own secret again — and both end up with the exact same colour. A spy heard the public colour and both blends, but un-mixing paint is impossible, so the spy is stuck.
Try with the plate
  • Mix and swap the public colours, then re-add your private one.
  • Watch both sides land on the same secret an eavesdropper can't reach.

Encryption lets two strangers agree a secret key in plain sight, even where an eavesdropper hears everything. Each mixes a private value into a shared public one and swaps the blends openly; each then re-adds their private value to arrive at the same secret. Un-mixing the blends is computationally infeasible.

The short answer

Imagine you and a friend each pick a secret colour of paint, and there's one shared colour everyone can see. You each mix your secret into the public colour and swap the results out in the open. Then you each stir in your own secret again — and amazingly, you both end up with the exact same final colour, one a spy who saw the swap can't reproduce, because un-mixing paint is too hard. That shared colour is your secret key.

The common mix-up

Most people assume a secret key has to be hidden during the exchange. In fact both sides build the same key in plain sight — mixing is easy but un-mixing is computationally infeasible, so an eavesdropper who sees everything still can't compute it.

What's actually happening

Here is a puzzle that sounds impossible: you and a stranger want to agree on a secret password, but the only way to talk is by shouting across a room where an eavesdropper hears every word. How can you possibly end up sharing a secret that the listener doesn't also learn? The answer, found in 1976 and now protecting nearly every secure connection on the internet, is one of the most beautiful ideas in all of computing.

The intuition is paint. Everyone, including the eavesdropper, can see one shared public colour. You privately pick a secret colour and keep it to yourself; the stranger does the same. Now you each mix your private colour into the public one and swap the resulting blends out loud, in the open. The eavesdropper sees both mixed blends but, crucially, cannot separate them back into their ingredients, because un-mixing paint is hopeless. Finally, you stir your own private colour into the blend the stranger sent you, and the stranger stirs their private colour into yours. Because mixing doesn't care about order, you both arrive at the exact same final colour: public + yours + theirs. That shared colour is your secret key, and the eavesdropper, missing either private colour, can never reach it. The simulator walks through the four steps and shows both sides landing on the identical blend a spy can't reproduce.

Real encryption swaps paint for numbers, but the principle is identical: it uses a mathematical operation that is easy to do and brutally hard to undo. Mixing is a kind of multiplication of huge numbers (modular exponentiation); un-mixing means solving the "discrete logarithm", which would take the fastest computers longer than the age of the universe for the key sizes in use. So the secret key is agreed in plain sight and never transmitted, and everything that follows (your bank login, your messages, the padlock in your browser) is locked with it. The genius isn't hiding the conversation; it's building a sum that only the two of you can finish.

Remember this

The genius of encryption isn't hiding the conversation; it's a sum only the two of you can finish, which is why the secret key is never actually sent.

Try it at home The paint handshake
  1. 1You and a friend agree on a "public" colour out loud — say, yellow. Each of you secretly picks a private colour and tells no one.
  2. 2Each mixes a blob of the public yellow with their private colour and openly swaps the result.
  3. 3Now each of you adds your own private colour to the blob you received. Compare: you've both made the same final shade — a secret you agreed in the open that no onlooker can mix.

Common questions

How does the paint analogy work?

Everyone sees one public colour. You and the stranger each mix in a private colour and swap the results. Each then stirs in their own private colour again — and because mixing ignores order, you both reach the same final shade a spy cannot reproduce.

Why can't an eavesdropper compute the key?

The maths is easy one way and brutally hard the other. Mixing is a kind of multiplication of huge numbers; un-mixing means solving the discrete logarithm, which would take the fastest computers longer than the age of the universe.

Where is this used?

Every "https" page begins with a key exchange like this, agreeing a shared secret with the server before any private data is sent — over a line anyone could be listening to.

Built & checked by Nilesh Singh · how this is made · last updated June 2026