Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How public key cryptography works

After this lesson: I can explain how a public key and a private key work together.

60 min lesson5 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

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

WordWhat it meansExample
Public keythe key you hand out to everyone
Private keythe key you never share
Key paira public key and a private key made together
Remainderwhat is left after dividing17 % 5 is 2
Key exchangetwo sides agreeing a secret key in the open
Hybrid encryptionpublic 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

StepWhoWorkingResult
MessageYouThe letter A is 6565
EncryptYou, public key65 to the power 17, remainder after dividing by 32332790
SendThe internetAnyone can see 27902790
DecryptShop, private key2790 to the power 2753, remainder after dividing by 323365

The cells students fill in are left blank in this preview.

Encrypt 65 with the public key

Command
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

Command
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

Code: python
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

Command
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

StepSide A (private 6)Side B (private 15)
Send5 to the power 6 gives 85 to the power 15 gives 19
ReceiveGets 19 from BGets 8 from A
Work out19 to the power 6 gives 28 to the power 15 gives 2

The cells students fill in are left blank in this preview.

The two numbers each side sends

Command
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

Command
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.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.