# Public and Private Keys: How Do You Share a Secret When Everyone Is Listening?

If everything on the internet passes through other people's computers, how does anything stay secret? I started with the obvious answer, watched it fall apart, and ended up at one of the cleverest ideas I've come across: a key you can give to everyone.

Published: 9th July 2023

After my last post on [why passwords need 8 characters](/blog/why-passwords-need-8-characters), another question started bugging me.

When I send a message to my friend Riya, it doesn't fly straight from my phone to hers. It goes through my Wi-Fi router, my internet provider, a bunch of computers I've never heard of, and then finally to her.

Any of those computers can look at it.


So how does *anything* on the internet stay private? My bank password, my WhatsApp chats, the card number I typed into a shopping site. All of it goes through the same air.

## The obvious answer

My first thought: just scramble the message with a password. Riya and I agree on a password, I scramble the message with it, and she unscrambles it with the same password. Anyone in the middle only sees junk.

Sounds good. Step through it and see where it breaks.


That's the problem. **To send a secret safely, I first need to send a secret safely.** The password has to get to Riya somehow, and the only road to her is the one the snoop is sitting on.

## "Just meet in person then"

Okay, fine. Riya and I meet at college and I tell her the password face to face. No snoop.

That works for one friend. But think about every website I use. I can't go to Amazon's office and whisper a password to them. And they can't meet millions of customers either.

It gets worse when more people are involved. If every two people need their own password, the number of passwords blows up fast:


A class of 60 already needs 1,770 secret passwords. The whole internet? Not happening.

So "share a password first" doesn't scale, and "send the password online" doesn't work. I was stuck here for a while.

## The trick: two different keys

The idea that fixes this is weirdly simple once you see it.

What if the key that **locks** and the key that **opens** were *different* keys?

Think of a padlock. The kind you snap shut with your hand, no key needed. But to open it again, you need the key.

Here's what Riya does:

1. She buys a padlock and its key.
2. She keeps the **key** with her. Always. Nobody else ever sees it.
3. She makes copies of the **open padlock** and hands them out to anyone who asks. Me, her other friends, even the snoop. She doesn't care.


Now when I want to send her something:

1. I put my message in a box.
2. I snap **her** padlock shut on it.
3. I send the box through the air.

The snoop sees the box. The snoop even has the same padlock. Doesn't matter. A padlock can only *lock*. The only thing that opens it is the key in Riya's pocket.

Even I can't open the box after I lock it. Only Riya can.


That's it. That's the whole idea:

- The open padlock is Riya's **public key**. It's out in the air, and that's fine.
- The key in her pocket is her **private key**. It never leaves her.

And notice what's missing: **we never had to share a secret.** The only thing that travelled through the air was something everyone is allowed to have.

## Try it

This is the real thing, not an animation. Riya's key pair was made in your browser when this page loaded.


Try the snoop button. The snoop has the public key, the locked message, everything that went through the air. It still doesn't open.

## Everyone gets a pair

This isn't just Riya. **Everyone** can have their own pair.

- My public key is out there for anyone who wants to send me something.
- My private key stays on my phone.
- If I want to message Riya, I lock it with *her* public key.
- If she wants to reply, she locks it with *mine*.

Remember the slider above? With shared passwords, 1,000 people needed almost half a million secrets passed around in person. With key pairs, they need 1,000 pairs, and none of the public halves have to be hidden at all.

Somewhere in the air, there's a public key for pretty much every website and every chat app user. Nobody is hiding them. They're *meant* to be seen.

## Flip it around: proving who sent it

While reading about this, I found out the keys work the other way round too, and it's just as useful.

Locking stops people from **reading** a message. But there's a different problem. How do I know a message is really from Riya, and that nobody changed it on the way?

The snoop could catch Riya's message, change "₹200" to "₹2,000", and pass it along. I'd never know.

So Riya does the opposite of before. She uses her **private** key to stamp the message. That stamp is called a **signature**. Then anyone can check that stamp with her **public** key.

- Only Riya can make the stamp, because only she has the private key.
- Anyone can check it, because everyone has the public key.
- If even one letter of the message changes, the stamp stops matching.


Be the snoop and try to get away with it:


You can change the message. You can even make your own signature with your own key. But you can't make one that matches **Riya's** public key, so the check catches you every time.

## How can that even work?

This was the part I couldn't get my head around. How can one key lock something that it can't unlock? How can a key be public and still be safe?

I'm not going to pretend I understand all the maths. But the basic idea is this: there's some maths that's **easy to do one way and really, really hard to undo**.

Here's an example I could actually follow. Multiply two numbers:

```txt
61 × 53 = 3233
```

Easy. Now go backwards. I give you `3233`. Which two numbers multiply to make it?

You'd have to start trying: 2? 3? 5? 7? …and keep going until something fits. Now imagine the number has **hundreds of digits** instead of four. That's back to the same "try every possibility" problem from my password post, except this time it would take longer than the universe has existed.

Very roughly, the public key is built from the big number, and the private key is built from the two numbers that made it. Anyone can use the big number to lock. Opening needs the two secret pieces, and nobody can work those out in any reasonable time.

## Where I already use this without knowing

Once I understood this, I started seeing it everywhere:

- **The padlock in the browser bar.** When I open my bank's website, the site sends me its public key. My browser uses it to set up a secret connection, without the two of us ever sharing a password first.
- **WhatsApp's "end-to-end encrypted" message.** My phone has my private key. Messages to me get locked with my public key, so even WhatsApp's own computers in the middle can't read them.
- **Adding an SSH key to GitHub.** I did this without understanding it. I gave GitHub my *public* key, kept the private one on my laptop, and now GitHub can tell it's really me without a password.

## What I figured out

1. **Everything online goes through other people's computers.** So anything you send can be seen in the middle.
2. **A shared password doesn't solve it.** You'd need to send the password first, through the same air. And everyone would need a separate password for everyone else.
3. **Two different keys fix it.** The public key locks and goes out to everyone. The private key opens and never leaves you.
4. **It works the other way too.** Stamp with the private key, check with the public key. That proves who sent it and that nobody changed it.
5. **It's safe because of one-way maths.** Easy to do, practically impossible to undo.

What gets me is that the thing that keeps my messages secret is something I hand out to *everyone*. It sounds wrong when you say it out loud. But it's exactly why it works.

Bye for now.

Original page: https://faisalhusa.in/blog/public-and-private-keys
