Public-Key Cryptography

AdvancedAbout 12 minCybersecurity

How can two strangers on opposite sides of the world share a secret — when everything they send could be seen by anyone?

The Key-Sharing Problem

On the Encryption Basics page, the same key locked and unlocked the message. That works — but first you have to get the key to the other person. If you send it over the internet, anyone watching could copy it, and then read everything you send afterwards.

You can't meet in person with every website you use. So how does your browser set up a secret with a website it has never talked to before?

The Open Padlock Trick

Imagine Sam buys a box of padlocks that all open with one key. Sam keeps the key safe at home, and hands out open padlocks to anyone who asks — friends, strangers, it doesn't matter.

Anyone can snap one of Sam's padlocks shut on a box. But once it's closed, only Sam's key can open it. Not even the person who locked it can get back in.

Public key = the open padlock

Shared with everyone. Anyone can use it to lock a message for Sam.

Private key = the only key

Never shared. It's the only thing that can unlock those messages.

Two different keys: one for locking that everyone can have, one for unlocking that only the owner has. That's why it's also called asymmetric encryption.

Watch a message travel

Step 1 of 6

The scrambled text is an illustration — real encrypted data is just as unreadable, but made with far more complex math.

Why Can't Eve Just Work Out the Key?

The public and private keys are linked by math that is easy to do in one direction and practically impossible to undo. Here's a small taste:

  • Multiplying is easy: 61 × 53 = 3,233.
  • Going backwards is much harder: "which two numbers multiply to make 3,233?" means trying lots of possibilities.

Real keys use numbers hundreds of digits long. Multiplying them takes a computer a split second. Splitting the answer back into the original numbers would take every computer on Earth longer than a human lifetime — many lifetimes. That one-way difficulty is what keeps the private key safe even though the public key is out in the open.

You Use This Every Day

When you see the padlock next to a website's address, public-key encryption has just been used. Your browser used the website's public key to safely agree on a brand-new shared secret key. From then on, both sides switch to ordinary, faster same-key encryption using that secret. The public-key step solved the "how do we share a key?" problem; the same-key step does the heavy lifting.

But Is It Really Sam's Padlock?

There's one more trick an attacker could try: handing out their own padlocks while pretending to be Sam. To stop this, websites get their public keys vouched for by a trusted organization called a certificate authority — a bit like an official ID card that says "this padlock really does belong to this bank." Your browser checks that ID automatically every time.

The same pair of keys can also work the other way round. If Sam locks something with the private key, anyone can open it with the public key — which proves it could only have come from Sam. That's called a digital signature.

Back to basicsEncryption Basics →