Two people have never met, and everything they say to each other can be overheard. How can they end up sharing a secret key that nobody listening can work out?
Two keys instead of one
In lesson 1.3, both sides needed the same secret key, and getting it to them safely was the hard part. Public-key cryptography, also called asymmetric cryptography, takes a different approach. Each person or device has a key pair:
- A private key, which its owner keeps secret and never shares.
- A public key, which can be given to anyone, published, or sent over an open network.
The two keys are created together and are mathematically linked. But the link only works one way: with a sound algorithm and a large enough key, knowing the public key doesn't let anyone work out the private key using today's computers.
What a key pair can do depends on the scheme it was made for. Some schemes encrypt, some agree on keys, and some sign. A key pair is created for one scheme, and good practice gives each key pair a single purpose.
The mailbox analogy
Think of a mailbox with a slot. Anyone walking past can drop a letter through the slot, but only the person with the mailbox key can open it and read what's inside. The slot is like the public key: it's available to everyone. The mailbox key is like the private key: only its owner has it.
This is public-key encryption: anyone can use your public key to protect something that only you can unlock with your private key, without the two of you ever sharing a secret first. That solves the problem from lesson 1.3.
Like every analogy, this one has limits. In practice, public-key methods are rarely used to encrypt whole messages. They're slow compared with AES, so they're mostly used to set up a fresh symmetric key, which then protects the data.
Easy one way, hard the other
Public-key cryptography rests on mathematical problems that are easy to do in one direction and extremely hard to undo. A classic example: multiplying two large prime numbers takes a computer a fraction of a second, but starting from the result and finding those two primes again is, for large enough numbers, far beyond any known method on today's computers.
The RSA algorithm is built on that problem. Elliptic-curve cryptography (ECC) uses a different one-way problem. Lesson 1.7 looks at both, and Module 6 explains why future quantum computers threaten these particular problems.
What a key pair makes possible
Different public-key schemes open up three things that a single shared key can't do on its own:
- Agreeing on a key in the open. In a key-agreement scheme such as Diffie–Hellman, first published by Whitfield Diffie and Martin Hellman in 1976, two parties swap only their public keys. Each combines the other's public key with its own private key, and both arrive at the same shared secret, which they can turn into a symmetric key. Someone who sees both public keys still can't work it out. The demo below uses this kind of scheme.
- Protecting something for one recipient. With a public-key encryption scheme, the one the mailbox illustrates, anyone with your public key can protect data, or a symmetric key, so that only your private key can recover it.
- Digital signatures. With a signature scheme, the private key signs data and anyone with the public key can check the signature. That's how a smart card proves it controls its private key, as lessons 1.5 and 1.6 explain.
Try it: agree on a secret in the open
Your browser plays Alice and Bob. Each makes a real key pair (elliptic-curve Diffie–Hellman, P-256), they swap only their public keys, and each combines the other's public key with their own private key. Nothing is sent or stored.
Alice
…
Kept inside Alice's browser, never shown or sent
…
Bob
…
Kept inside Bob's browser, never shown or sent
…
An eavesdropper
She has seen both public keys, and has a key pair of her own. Combining Alice's public key with her private key gives:
…
This demo doesn't check who sent each public key. Without that check, an attacker who can change messages could swap in her own public key; see "Whose public key is it?" below.
Whose public key is it?
Public keys can be shared openly, but that raises a new question: when you receive a public key, how do you know it really belongs to the person or device you think? If an attacker can intercept the exchange, she could hand each side her own public key instead. Each side would then agree a key with the attacker, who could read and pass on everything in between. This is called a man-in-the-middle attack.
So key agreement on its own isn't enough; it has to be authenticated. The usual tool is a certificate: a statement, signed by an issuer called a certificate authority, that binds a public key to a name. To authenticate an exchange, the verifier checks three things: that the certificate chains up to an issuer it already trusts, that the name in it is the one it expected, and that the other side actually controls the matching private key in this exchange, for example by signing something fresh. Certificates and certificate authorities are the subject of Module 2.
Key pairs in smart cards
A secure smart card can generate a key pair on its own chip, as lesson 1.1 showed. The private key stays on the card. The public key is shared, usually inside a certificate, so that a reader can verify the card's signature on a fresh challenge. A valid signature shows the card controls the certified private key; a copy of the card's public certificate alone can't answer the challenge. Lesson 1.6 walks through that exchange.
Go deeperOptional technical detail for readers who want more
A short history
Whitfield Diffie and Martin Hellman published the idea of public-key cryptography and their key-agreement method in 1976, in "New Directions in Cryptography". Ron Rivest, Adi Shamir and Leonard Adleman published RSA the following year. Researchers at the UK's GCHQ, among them James Ellis, Clifford Cocks and Malcolm Williamson, had developed equivalent ideas in secret a few years earlier; their work was made public in 1997.
How Diffie–Hellman agrees on a secret, intuitively
A popular way to picture it uses paint. Alice and Bob agree in public on a common colour. Each mixes in a secret colour of their own and sends the result to the other. Each then adds their own secret colour to what they received. Both end up with the same final mixture, but an eavesdropper who saw only the public colour and the two mixtures can't easily separate the paints. In real systems the "mixing" is a mathematical operation that's easy to do and very hard to undo, on very large numbers or on an elliptic curve.
Why the demo's two results match
In elliptic-curve Diffie–Hellman, each private key is a large random number, and the public key is a point on the curve derived from it. Combining Alice's private key with Bob's public key, and Bob's private key with Alice's public key, lands on the same point, so both get the same shared secret. The eavesdropper's private key leads somewhere else. NIST SP 800-56A specifies these key-agreement methods, and SP 800-56C covers deriving keys from the shared secret.
Strength and key size
Public-key keys need to be much longer than symmetric keys for similar strength. NIST rates RSA with a 2048-bit key at about 112 bits of security, and the P-256 elliptic curve used in the demo at about 128 bits, similar to AES-128. That's one reason elliptic curves are popular on smart cards, where memory and processing are limited.
Try it yourself
With OpenSSL installed, these commands create a P-256 key pair and print the public key, which is safe to share:
$ openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out private.pem $ openssl pkey -in private.pem -pubout
Keep private.pem to yourself, and delete it when you're done.
Sources
- NIST SP 800-56A Rev. 3: Pair-wise key establishment using discrete logarithm cryptography (key agreement)
- NIST SP 800-56C Rev. 2: Recommendation for key-derivation methods in key-establishment schemes (turning the shared secret into keys)
- NIST SP 800-57 Part 1: Recommendation for key management (security strength of key sizes)
- FIPS 186-5: Digital Signature Standard (digital signatures)
- NIST SP 800-186: Recommendations for discrete logarithm-based cryptography: elliptic curve domain parameters (the P-256 curve)
Draft lesson, awaiting technical review · Last updated 19 November 2026