Back

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.

Author
Faisal Husain
Published
Reading time
7 min read
Share
Public and Private Keys: How Do You Share a Secret When Everyone Is Listening? blog image

After my last post on 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.

My message doesn't fly straight to RiyaA message from me hops through my Wi-Fi router, my internet provider, and a bunch of random computers before reaching Riya. A snoop sitting on the network can read it at every hop.me → … → RiyameWi-FiinternetproviderrandomcomputersRiyasnoop: “I can read all of it”

Every hop in the middle can see what goes past. That's the 'air' this post keeps talking about.

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.

Everything goes through the air

Step through what happens when I try to send my friend Riya something private.

MMe
👀 snoop on the network
exam answers: B, D, A…
RRiya

What the snoop sees

  • ✓ exam answers: B, D, A…

01I send Riya a message

It doesn’t go straight to her phone. It hops through my Wi-Fi, my internet provider, a bunch of computers in between, then hers. Any of them can read it.

1 / 4

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:

One password per pair of people

If every two people need their own shared password, how many passwords does a group need?

People · 10

Shared passwords

45

Each one has to be handed over secretly, somehow, before anyone can talk.

Key pairs

10

One per person. The public half can be shouted out loud. Spoiler for later in this post.

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.
Riya hands out open padlocks and keeps the keyRiya keeps her key in her pocket and gives an open padlock to anyone who asks: me, a friend, and even the snoop. The open padlock is her public key. The key is her private key.Riyaprivate key:stays with mefree padlocks for anyonemea friendsnoopdoesn't matteropen padlock = public key

Giving the snoop a padlock is fine. A padlock can only lock. The key that opens it never leaves Riya.

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.

Lock it with her padlock, only her key opens itI put my message in a box and snap Riya's padlock shut on it. The box travels through the air past the snoop, who has the same padlock but no key, so they can't open it. Riya opens it with her private key.1. snap Riya’spadlock shutthrough the airsnoop has the same padlock,still can’t open itRiya’s keyopens it

Once the padlock clicks shut, even I can't open the box again. Only the key in Riya's pocket 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.

Lock it with Riya's open padlock

Riya made a key pair in your browser just now. Her public key is out in the open, so you, me, and the snoop all have it. Her private key stays with her.

Riya's public key · everyone has this

making a key pair…

Riya's private key · only Riya has this

🔒 never leaves her device

These are real keys, made and used inside this tab. Try locking the same message twice: the scrambled text comes out different every time, but Riya can still open both.

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.
Riya stamps it, anyone can check the stampRiya stamps her message with her private key. When the message reaches me unchanged, the stamp matches her public key. When the snoop changes ₹200 to ₹2,000 on the way, the stamp no longer matches.anyone can check the stamp with Riya’s public keyRstamped withprivate key“Give Faisal ₹200”matchessnoop changes itto ₹2,000doesn't match

Only Riya's private key can make the stamp. Everyone can check it with her public key. Change one letter and it stops matching.

Be the snoop and try to get away with it:

Prove it's really from Riya

This time Riya uses her private key to sign a message. Anyone can check it with her public key. Then play the snoop and try to mess with it.

Change even one letter and the check fails. The snoop can make their own signature, but it won't match Riya's public key, because only her private key can make one that does.

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:

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.

Let's work together

Have a product problem worth solving?

I help teams build fast, dependable products with thoughtful interfaces and strong engineering foundations.