seegongsik
Security

How does key exchange work

For two people to use the same lock, they need the same key. But how do you hand that key over? Mail it and someone in the middle might copy it; meet in person and the distance gets in the way. Surprisingly, there is a way for two people to end up with the same key without ever sending it. They build it together.

01

A fast key has to be shared safely

The public-key encryption from the last lesson is safe but computationally heavy and slow. Exchanging long contents entirely that way takes too long, so the actual contents are locked quickly with symmetric encryption.
The trouble is that symmetric encryption needs both people to hold exactly the same key. The key distribution problem that blocked us in the symmetric encryption lesson comes back here. One option is to lock the symmetric key with a public key and send it, but this time we look at a different route that never sends the key at all.

Which key method to use
Tap the two methods to compare their pros and cons.

Tap public key and symmetric key to compare them. The public key is safe but slow; the symmetric key is fast but has to be shared.

The wish to use a fast symmetric key is clear. But simply sending that key is risky: if someone in the middle copies it, no lock is strong enough to help.
So we need a way for two people to hold the same key without ever putting the key itself on the road. Let's picture that way as mixing paint.

02

Mix a secret color into a public color

Call the two people Garam and Narae. First they agree on one public color to share. It does not matter who sees it. Next, each picks a secret color that they will never show anyone.
Garam mixes the public color with Garam's secret color to make a new color, and Narae mixes the public color with Narae's secret color to make another. Then they send each other only these mixed colors. What matters is that once paint is mixed, it is very hard to separate out which colors went in.

Garam
public color
+
private color
color to send
Narae
public color
+
private color
color to send
Each blends a private color into the public color. The private color is not sent.

Tap so that Garam and Narae each mix a secret color into the public color. Only the two mixed colors are sent across; the secret colors stay in their hands.

Now Garam holds the mixed color Narae sent, and Narae holds the one Garam sent. Both also know the public color. For now they hold different colors, but one more step makes them the same.
If each mixes their own secret color into the color they received, both arrive at exactly the same color. Let's check why.

03

Both reach the same final color

What Garam holds already contains Narae's secret color, and Garam mixes in Garam's own secret color. The result is the public color with both secret colors mixed in. Narae does the same, mixing Narae's secret color into the color that contains Garam's.
The order of mixing differs, but the same colors went in, so the result is the same. Both arrive at exactly the same final color. That final color is a shared secret only the two of them know, and it is used as their symmetric key.

Garam
received color
+
my private color
final color
Narae
received color
+
my private color
final color
Blend your own private color back into the color you received.

Tap so that Garam and Narae each mix their secret color once more into the color they received. You will see both reach exactly the same final color.

The final colors in the two boxes became identical. The key never went out on the road; all that passed was the public color and the mixed colors. Even so, both now hold the same key.
Not sending the same secret but building it together: that is the heart of key exchange. It is called Diffie-Hellman key exchange, after the two people who first proposed it, and in practice it uses calculations with large numbers instead of paint. So what happens if someone watches every color that passes?

04

Watching is not enough to make the final color

Imagine someone watching every color that passes on the road. This person knows the public color and has seen both the mixed color Garam sent and the one Narae sent. Even so, they cannot make the final color.
Making the final color requires someone's secret color, and no secret color ever came out on the road. Pulling a secret color back out of a mixed one is also very hard. So the watcher cannot reach the same final color.

What the watcher saw
?final color (cannot make)
Tap one by one to mark what the watcher saw on the road.

Tap each thing the watcher saw on the road. The public color and the mixed colors are all visible, but without a secret color the final color cannot be made.

The watcher was left with an empty slot, because the last piece, a secret color, is missing. So Garam and Narae can safely use the final color as their own symmetric key.
There is a catch, though. Key exchange stops someone who only watches, but not someone in the middle who mixes colors separately with Narae while pretending to be Garam, and with Garam while pretending to be Narae. That is why key exchange has to be used together with signatures and certificates that confirm the other side is genuine. Also, key exchange is not the same as locking contents with a public key.

05

Wrapping up

In short, public-key encryption is safe but slow, so the actual contents travel with a fast symmetric key. That symmetric key is not sent but built together. Each person mixes a secret color into the public color and exchanges the result; mixing their own secret color back into what they received brings both to the same final color, the shared secret.
A watcher, lacking any secret color, cannot make that color. Key exchange is the procedure of building the same secret together, and it is different from public-key encryption, which locks contents with a public key.

Separate the two ideas
Tap the two ideas to separate their meaning and use. They are different things.

Tap the two ideas to tell them apart. Key exchange builds a shared secret together; public-key encryption locks contents with a public key.

Now you know that two people far apart can share the same secret without ever dropping the key on the road. The principle behind this simple color mixing is used every time you connect securely to a website today. On each connection, the browser and the server use it to make a fresh symmetric key just for that connection and throw it away afterward. So even if the server's private key leaks later, past conversations cannot be unlocked; this property is called forward secrecy.
What remains is weeding out impostors who sneak into the middle. The next lesson covers digital signatures, which use a mark made with a private key to confirm that a message really came from that person.

In one linePublic-key encryption is safe but slow, so the actual contents travel with a fast symmetric key. The question is how to share that symmetric key safely, and the answer is to build it together. Pictured as mixing paint, two people each mix a secret color into a public color they both know and send each other only the mixed results. Mixing their own secret color back into what they received brings both to exactly the same final color, which becomes the shared secret, the symmetric key. A watcher who sees the public color and the mixed colors still cannot make the same color without knowing a secret color. Key exchange cannot stop an attacker who pretends to be each side in the middle, though, so signatures and certificates are needed to confirm the other party.
← Security
Was this helpful? Support seegongsik