Sharing a secret with a new shop
Public key cryptography uses two linked keys: a public key that anyone may have, and a private key that only its owner keeps. What one locks, only the other opens.
It solves a real problem. Your browser meets a shop for the first time. They must agree a secret over a line that anyone can listen to.
By the end of this lesson
Note
I can explain how a public key and a private key work together.
Key words
| Word | What it means | Example |
|---|---|---|
| Public key | the key you hand out to everyone | |
| Private key | the key you never share | |
| Key pair | a public key and a private key made together | |
| Remainder | what is left after dividing | 17 % 5 is 2 |
| Key exchange | two sides agreeing a secret key in the open | |
| Hybrid encryption | public keys agree a key, then a fast cipher does the work |
The cells students fill in are left blank in this preview.
Strangers cannot share a key openly
A shared key works only if both sides already have it. Sending the key over the same line lets any listener copy it.
A key pair breaks the deadlock. The shop makes a pair and publishes the public key. You encrypt with it. Only the shop's private key can decrypt the answer.
A toy RSA key pair
RSA is the best known key pair method. Pick two primes, 61 and 53. Multiply them to get the modulus, 3233. That number is part of both keys.
The public key is 3233 with an exponent 17. The private key is 3233 with an exponent 2753. To encrypt, raise the message to the power 17 and keep the remainder after dividing by 3233. To decrypt, use 2753.
Worked example: encrypting the number 65
| Step | Who | Working | Result |
|---|---|---|---|
| Message | You | The letter A is 65 | 65 |
| Encrypt | You, public key | 65 to the power 17, remainder after dividing by 3233 | 2790 |
| Send | The internet | Anyone can see 2790 | 2790 |
| Decrypt | Shop, private key | 2790 to the power 2753, remainder after dividing by 3233 | 65 |
The cells students fill in are left blank in this preview.
Encrypt 65 with the public key
python3 -c "print(pow(65, 17, 3233))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Decrypt 2790 with the private key
python3 -c "print(pow(2790, 2753, 3233))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
RSA is safe only with huge numbers
Multiplying two primes is easy. Finding them again from the product is hard when the product is huge. Run the code and see how fast 3233 falls apart.
This toy also skips padding, which a real system adds to every message. Real RSA keys are at least 2048 bits, about 617 digits, and 3072 bits for long-lived data. Use a vetted library, never your own.
Break the toy key by trying divisors
n = 3233
divisor = 2
while n % divisor != 0:
divisor = divisor + 1
print("one prime is", divisor)
print("the other is", n // divisor)
# Change n to 143, the modulus of a smaller key, and press Run.
How many digits in a 2048-bit number
python3 -c "print(len(str(2 ** 2048)))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think public key encryption replaces the shared key. It is far slower than AES. Real systems use it only to agree a short key, then switch to a fast symmetric cipher. This is hybrid encryption.
A key exchange agrees a secret openly
Diffie-Hellman is the classic key exchange. Both sides agree two public numbers, here 23 and 5. Each picks a private number, here 6 and 15.
Each sends 5 to the power of their private number, remainder after dividing by 23. Each then raises the other's answer to their own private number. Both end with the same secret, 2.
Worked example: both sides reach 2
| Step | Side A (private 6) | Side B (private 15) |
|---|---|---|
| Send | 5 to the power 6 gives 8 | 5 to the power 15 gives 19 |
| Receive | Gets 19 from B | Gets 8 from A |
| Work out | 19 to the power 6 gives 2 | 8 to the power 15 gives 2 |
The cells students fill in are left blank in this preview.
The two numbers each side sends
python3 -c "print(pow(5, 6, 23), pow(5, 15, 23))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The secret each side works out
python3 -c "print(pow(19, 6, 23), pow(8, 15, 23))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Today the key exchange uses elliptic curves
A listener sees 8 and 19 but not 6 or 15. With real sizes, finding the private number is out of reach. Modern systems use elliptic curves such as X25519, which give the same safety with keys of about 256 bits.
A secure web connection runs a key exchange, then encrypts the traffic with AES-GCM or ChaCha20-Poly1305.