seegongsik
Security

How does public key encryption work

The last lesson showed symmetric encryption, which locks and unlocks with a single key. It was fast and clean, but it left homework: how do you secretly hand that key to someone far away? What if the locking key and the unlocking key were different? What if the locking key could be handed out to the whole world while only you kept the unlocking key? Public-key encryption grew out of exactly that idea.

01

There are two keys

Symmetric encryption had one key. Public-key encryption creates and uses a pair of two keys together. One is the public key, which, as the name says, can be handed to anyone. The other is the private key, which only you keep and never give away.
The two keys are mathematically paired, yet knowing the public key does not let anyone calculate the private key. They are built on mathematical problems that are easy in one direction and practically impossible in the other, like factoring a very large number.

Tap a key to inspect its role

Tap the two keys one at a time to see their roles. The public key is handed to anyone; the private key stays with you alone.

The public key was fine to leave wide open, while the private key had to stay deeply hidden. They are a pair, but their roles are opposite: one is announced to the world, the other kept secret to the end.
So with two keys, which one locks and which one unlocks? Let's look at locking first.

02

Lock with the public key

To send someone a secret message, you first get that person's public key. Anyone can have a public key, so it is easy to obtain. You lock the message with it.
Locking with a public key is like snapping an open padlock the other person handed out onto your message. Anyone can snap a padlock shut, but once it is shut, not even the person who did it can open it again, because a public key can only lock, not unlock.

HELLOmessage
public key
Anyone can lock with the public key they hold.

Press lock with public key. The public key that anyone holds snaps a padlock onto the message.

The message was locked into an unreadable shape. What locked it was the public key, which is no secret, so truly anyone can do the locking.
Yet even the person who locked it cannot open it again. The key that opens a padlock locked with a public key is a different one.

03

Only the private key opens it

A padlock locked with a public key opens only with the private key that forms its pair. The private key from the same pair opens it; any other key does not budge it. Not even the public key that locked it can open it.
So only the one person who holds the private key can read the message. Even if the whole world holds the public key, the only person who can read it is the owner of the private key.

Present a key to open the padlock

Try the keys one at a time. Only the matching private key opens the padlock; the public key that locked it and the wrong private key both fail.

It opened only with the matching private key. This asymmetry is the heart of public-key encryption: anyone can lock, but only one person can unlock. The power to lock is left wide open, and only the power to unlock is firmly guarded.
Thanks to this property, the homework from the last lesson is solved, because the need to hand over a key in secret disappears altogether.

04

The key distribution problem is solved

With symmetric encryption, both people had to hold the same key, so handing it over in secret was the headache. Public-key encryption has no such worry. Whoever takes the recipient's public key can only lock with it.
The private key that unlocks never leaves its owner's hands in the first place. With no key to hand over in secret, the risky delivery step vanishes completely.

Tap a way to compare its steps
Tap a way to compare its steps

Tap symmetric key and public key to compare their steps. Symmetric encryption has a secret delivery step wedged in; public-key encryption has none.

Side by side, the difference was clear. Symmetric encryption always had the risky step of secret delivery, and public-key encryption does not. Public-key encryption is computationally heavy, though, and slow for locking large amounts of data.
So in practice, public-key encryption is used to hand over a short symmetric key safely, and the data is locked with the fast symmetric key. One more piece of homework remains: you have to check that the public key you received really belongs to that person and was not swapped by someone in the middle. A later lesson covers that with certificates.

05

Wrapping up

In short, public-key encryption uses a pair of two keys. The public key is handed to anyone and the private key is kept by you alone. What the public key locks only the matching private key can unlock: anyone can lock, but only one person can unlock.
So the public key is safe to publish, and there is no key to hand over in secret at all. That is how the key distribution problem of symmetric encryption is solved.

1two keys
2lock with public key
3opens only with private key
4key distribution solved

Tap the key points in order to review. (two keys → lock with the public key → only the private key unlocks → key distribution solved)

Now you know the magic of two keys: a way to exchange secrets safely using a key you hand out and a key that is yours alone. Used in the reverse order, the same pair can also make a signature that cannot be forged, which the lesson on digital signatures covers.
Before that, the next lesson looks at key exchange, a way for two people to build the same secret key together without sending the key at all.

In one linePublic-key encryption uses a pair of two keys. The public key can be handed to anyone, and the private key is kept by you alone. What the public key locks opens only with the matching private key, not even with the public key that locked it, and the private key cannot be calculated from the public key. So a recipient's public key is safe to publish to the whole world, and whatever anyone locks with it can be opened only by the owner of the private key. The key distribution problem that troubled symmetric encryption disappears. Because the calculations are heavy, in practice public-key encryption hands over a symmetric key and the data is locked with that symmetric key, while certificates take care of checking that a received public key is genuine.
← Security
Was this helpful? Support seegongsik