Please enable JavaScript to use CodeHS

Cybersecurity // Cryptography

What Is Public-Key Cryptography?

How can two people who have never met share a secret across a network full of snoops? Public-key cryptography (also called asymmetric encryption) solves it with a lock anyone can close and a key only you hold.

Security ~15 min 3 interactive labs
01 // the problem

What problem does public-key cryptography solve?

Cryptography is the science of keeping messages secret. The usual method is encryption: you scramble a message (the plaintext) into nonsense (the ciphertext) using a key, which is a secret value. Someone with the right key can unscramble it again.

You may have tried this already. In a Caesar cipher, the key is a number: "shift every letter by 3." Both people use the same key, so this style is called symmetric encryption. It is fast and works well. But it has a catch.

That is the key-sharing problem. To talk in secret, Alice and Bob must first share a secret key. But to share the key, they need a secret way to talk. It is a chicken-and-egg loop.

On the internet this is a huge deal. When you open a website, your device and the site have never met. They cannot sneak off to agree on a key first. And every message passes through routers and Wi-Fi networks that strangers can watch.

The big idea: what if the thing you share in the open could not be used to read anything? Public-key cryptography splits the key into two linked halves. One half is safe to hand out. The other never leaves home. Want to see how the basics work first? Try Encryption and Ciphers.


02 // the padlock idea

How does the padlock-and-key analogy work?

Picture a padlock that is open. Anyone can snap it shut, even without a key. But once it clicks closed, only one key can open it again.

Now imagine Bob makes thousands of copies of his open padlock and leaves them in a bowl at the post office. He keeps the one matching key at home. Anyone who wants to send Bob a secret can grab a padlock, lock their box, and mail it. Even the mail carrier cannot open it. Only Bob can.

Public key (the open padlock)

  • Safe to share with the whole world
  • Used to lock (encrypt) a message for you
  • Cannot unlock anything

Like an open padlock in a bowl. Anyone can take one.

Private key (the one key)

  • Never shared. Only the owner has it
  • Used to unlock (decrypt) messages sent to you
  • If it leaks, the whole pair is no longer safe

Like the key in Bob's pocket. It matches only his padlocks.

In real life there is no metal. The two keys are very large numbers, made together as a pair and linked by math. What the public key locks, only the matching private key can unlock. Try it below. You choose what Alice and Bob do at every step, and the simulation tells you what happens.

lab_01: alice_sends_bob_a_secretstep 1 / 4
the open internet Alice Bob Eve eavesdropper private key public padlock copy copy

Notice: Alice and Bob never shared a secret ahead of time. The only thing that crossed the open internet was a padlock anyone is allowed to have. That is the whole trick.


03 // try the math

How does the math behind public and private keys work?

The most famous public-key method is RSA, named after the three people who published it in 1977: Rivest, Shamir, and Adleman. Here is the recipe in plain steps.

  1. Pick two prime numbers (numbers only divisible by 1 and themselves), like 5 and 11. Call them p and q.
  2. Multiply them to get n. For 5 and 11, n = 55. The number n is public.
  3. Use p and q to build two linked exponents: a public one e and a private one d.
  4. Lock a number m with the public key: c = me mod n. Unlock with the private key: m = cd mod n.

"Mod" means the remainder after dividing, like clock math. If that is new, read Encryption and the Modulus first. The magic is that anyone can compute me mod n from the public key. But going backward needs d, and finding d means splitting n back into its two primes. That is easy for 55 and brutally hard for a number with hundreds of digits.

lab_02: toy_rsa.calctoy numbers only

1. Make a key pair

2. Encrypt with the public key

ciphertext c =
0

3. Try to decrypt it

Choose which key to decrypt with
result =
0

This is a toy. Real RSA keys use primes with hundreds of digits (a 2048-bit key makes an n over 600 digits long), and real systems add extra steps called padding. With tiny primes like these, anyone could break the code in seconds. Never use toy numbers to protect anything real.


04 // the eavesdropper challenge

Who can read the secret message?

Security is about what happens when things go wrong. In each round below, a locked box is on its way. Tap everyone who can open it and read the note, then check your answer.

lab_03: who_can_read_itround 1 / 4


05 // in the real world

How does public-key cryptography protect HTTPS and messaging?

Every time you see the lock icon and https:// in your browser, public-key cryptography helped set up the connection. Here is the short version of the "handshake" your browser does with a website. (The full story is in What Is HTTPS and SSL?)

Browser
public keyThe website sends its public key, inside a certificate that vouches the key really belongs to that site.
Website
Browser
public keyUsing public-key math, the two sides agree on a fresh secret key. Snoopers watching cannot work it out.
Website
Browser
shared keyBoth switch to fast symmetric encryption with that new key for the rest of the visit.
Website

Messaging apps use the same idea. Many of them offer end-to-end encryption, where each person's device has its own key pair. Your phone locks a message with your friend's public key. The company's servers carry it along but cannot open it. Only your friend's private key can.

Symmetric vs. asymmetric encryption

If public-key encryption is so clever, why not use it for everything? Because it is much slower. So the two types team up: public-key methods solve the sharing problem, then symmetric encryption does the heavy lifting.

Symmetric

one shared secret key

Keys
One key locks and unlocks.
Speed
Very fast, good for big files and video.
Weak spot
Both sides must share the key safely first.
Example
AES, used to encrypt data in transit and on disks.

Asymmetric (public-key)

a public key and a private key

Keys
Public key locks. Private key unlocks.
Speed
Much slower, so used for small jobs.
Strength
No secret needs to be shared ahead of time.
Example
RSA and elliptic-curve methods.

Looking ahead: a large enough quantum computer could break RSA's math. Researchers are already building replacements. Read about them in What Is Post-Quantum Cryptography?


06 // proving who sent it

What is a digital signature?

Key pairs can also run backward. In encryption, you lock with the public key so only the owner can read. In a digital signature, the owner makes a stamp with their private key, and anyone can check it with the public key.

A signature does not hide the message. It proves two things: the message came from whoever holds that private key, and nobody changed it along the way. Software updates, apps, and website certificates all use signatures so your device can tell real from fake. (In real systems the signer stamps a short fingerprint of the message, called a hash. You can learn about those in What Is Password Hashing?)

Try it with a toy key. The signer stamps the message with the private key. The checker uses the public key. Then flip the switch to let a sneaky Mallory change the message in transit.

lab_04: sign_and_verifytoy numbers only

Signer (private key)

message 42
signature = messaged mod n
0

Checker (public key)

message received 42
signaturee mod n = 42

07 // quick answers

Public-key cryptography FAQ

What is public-key cryptography?

Public-key cryptography is a way to protect messages using a pair of linked keys. The public key can be shared with anyone and is used to lock a message. The private key is kept secret and is the only key that can unlock it. This means two people can share secrets without first meeting to agree on a password.

Is it safe to share a public key?

Yes. A public key is designed to be shared. It can lock a message but it cannot unlock one. What must stay secret is the private key. If someone steals your private key, they can read messages meant for you, so you should create a new key pair.

What is the difference between symmetric and asymmetric encryption?

Symmetric encryption uses one secret key to both lock and unlock data. It is fast, but both sides must somehow share the key safely. Asymmetric encryption, also called public-key encryption, uses a public key and a private key. It solves the key-sharing problem but is slower, so websites often use it to agree on a secret key and then switch to symmetric encryption.

How is a digital signature different from encryption?

Encryption hides a message so only the right person can read it. A digital signature does not hide anything. It proves who sent a message and that it was not changed along the way. The sender creates the signature with a private key, and anyone can check it with the matching public key.


08 // check yourself

Quick quiz

1. Which key is safe to share with everyone?

2. Alice wants to send Bob a secret using public-key encryption. What does she use to lock it?

3. What does a digital signature prove?

Key takeaways

  • The key-sharing problem: to use one secret key, two people must first share it safely.
  • Public-key cryptography uses a linked pair: a public key to lock and a private key to unlock.
  • You can hand out your public key freely. You must never share your private key.
  • HTTPS uses public-key methods to agree on a secret, then switches to fast symmetric encryption.
  • A digital signature proves who sent a message and that it was not changed.

Keep learning cryptography

You now know why public and private keys exist. Keep going with these tutorials, or practice real security skills in a CodeHS course.

Want hands-on practice? Explore the CodeHS Cyber Range for cybersecurity labs, or see all CodeHS courses.