Public-Key Cryptography
Chapters and transcript
Public-Key Cryptography In the last lesson, both sides needed the same secret key before they could talk. But here is a website you have never visited. You share no secret with it, and an attacker is watching the network. Still, you open a secure connection in a moment. Public-key cryptography makes this possible. In this lesson, you will learn key pairs, key exchange, digital signatures, and the trust problem.
Two keys Public-key cryptography uses a key pair: two keys created together and linked by mathematics. The public key can be shared with anyone. The private key must stay secret with its owner. What one key does, only the other can undo. Think of a post box with a slot. Anyone can drop a letter in, but only the owner can open the box.
Two keys Watch it work. Omar publishes his public key openly. Sana encrypts her message with Omar's public key, and sends it. An attacker sees the message, but cannot read it. Only Omar's private key can decrypt it. Sana and Omar never had to share a secret in advance.
Two keys There is one catch. Public-key operations are much slower than symmetric encryption. So they are rarely used to encrypt large amounts of data. Instead, they do two smaller but vital jobs. Agreeing on keys, and signing.
RSA and elliptic curves Two families are in wide use. RSA, from nineteen seventy-seven, relies on the fact that splitting a huge number into its two primes is extremely hard. Elliptic curves give similar security with much smaller keys. Compare the key sizes, the speed, and the common uses. One note for the future. A large enough quantum computer could break both, so experts are already standardising new post-quantum algorithms.
Key exchange The most common job is key exchange: agreeing on a shared key while attackers watch. The best-known method is Diffie-Hellman. Picture it with paint. Ayesha and Bilal publicly agree on a common colour, yellow. Then each picks a secret colour. Ayesha picks red, and Bilal picks blue. They keep these private.
Key exchange Now Ayesha sends yellow mixed with red. Bilal sends yellow mixed with blue. The attacker sees both mixtures, but cannot easily separate them to find the secret colours. Each side adds their own secret colour to what they received. Both end up with the same final colour. In real Diffie-Hellman, mathematics replaces paint.
Key exchange Modern systems use an elliptic curve version called ECDHE. The E at the end means ephemeral. A brand-new key pair is made for every session, and thrown away afterwards. This gives forward secrecy. It means old recorded sessions stay safe, even if the long-term private key is stolen later. The old traffic stays locked, because the temporary keys no longer exist.
Digital signatures Public keys can also prove who created a message. This is a digital signature. The phone maker signs an update with its private key, and sends it. Your phone checks the signature with the maker's public key. It is valid, so the update installs. Now an attacker sends a fake update. He does not have the private key, so the check fails.
Digital signatures A digital signature gives three things. Authenticity: it came from the holder of the private key. Integrity: if even one character changes, the check fails. Non-repudiation: the signer cannot easily deny signing it. But note this. A signature does not hide the message. If you need secrecy too, you must also encrypt.
The trust problem One problem remains. How do you know a public key really belongs to Bilal? In a man-in-the-middle attack, the attacker gives Ayesha his own key, pretending to be Bilal. He gives Bilal his own key too. Ayesha encrypts to the attacker. He decrypts and reads it, then re-encrypts it for Bilal. The mathematics works perfectly, but the keys belong to the wrong person.
The trust problem There are two main solutions. Check the key directly. Compare a short fingerprint of the key, a hash of it, in person or on a phone call. Messaging apps call these security codes or safety numbers. Or trust a third party. A trusted organisation signs a statement saying this public key belongs to this name. That statement is a certificate.
Quick check Quick check. You want people to be sure that a document really came from you, and was not changed. What should you do? Choose one.
Protect private keys All of this depends on one thing: the private key stays private. If it is stolen, an attacker can read your messages and sign in your name. So, never email, upload or paste a private key. Protect key files with a passphrase. In companies, keep important keys in a hardware security module or a key management service. And if a key may have leaked, revoke and replace it at once.
Try it yourself Try it yourself. Most computers include OpenSSH. The command ssh-keygen, with type ed25519, creates a modern elliptic curve key pair. When asked, set a passphrase. It saves two files. The file ending in dot pub is your public key, and you can share it. The other file is your private key. It must never leave your control. Notice how short the public key is.
Key takeaways Let us put it together. Share the public key, and keep the private key secret. Key exchange, such as ECDHE, agrees on a key in public, with forward secrecy. Sign with the private key, and verify with the public key. Check whose key it is, with a fingerprint or a certificate. And if a private key leaks, revoke and replace it.
In the last lesson you saw the big weakness of symmetric encryption: both sides need the same secret key before they can talk. Public-key cryptography solves this problem. It is the reason you can open a secure connection to a website you have never visited before.
Two keys instead of one
Public-key cryptography, also called asymmetric cryptography, uses a key pair: two keys that are created together and are mathematically linked.
- The public key can be shared with anyone. You can post it on a website or send it by email.
- The private key must stay secret with its owner. It should never be sent to anyone.
The two keys work as a team. What one key does, only the other key can undo. And knowing the public key does not let anyone work out the private key in any practical amount of time.
A helpful picture is a post box with a slot. Anyone can drop a letter through the slot (the public key), but only the person with the key to the box can open it and read the letters (the private key).
For encryption, it works like this:
Sana encrypts with Omar's PUBLIC key --> ciphertext
Only Omar's PRIVATE key can decrypt --> plaintext
Notice that Sana and Omar never had to share a secret in advance. Omar can publish his public key openly, and anyone can send him a private message.
Public-key operations are much slower than symmetric encryption. That is why they are rarely used to encrypt large amounts of data directly. Instead, they are used for two smaller but vital jobs: agreeing on keys and signing.
RSA and elliptic curves
Two families of public-key algorithms are in wide use today.
RSA was published in 1977 and is named after its three inventors, Rivest, Shamir and Adleman. Its security depends on a simple fact: multiplying two very large prime numbers is easy, but splitting the result back into those two primes is extremely hard. RSA keys are large. Today 2048 bits is the usual minimum, and 3072 or 4096 bits are also common.
Elliptic curve cryptography (ECC) is based on a different area of mathematics. Its big advantage is that it gives similar security with much smaller keys. A 256-bit elliptic curve key is generally considered about as strong as a 3072-bit RSA key. Smaller keys mean faster operations and less data, which is useful for phones and busy servers. Names you may see include ECDSA, ECDH, Ed25519 and X25519.
| RSA | Elliptic curves | |
|---|---|---|
| Typical key size | 2048 to 4096 bits | 256 bits |
| Speed | Slower | Faster |
| Common uses | Older certificates, signatures | Modern TLS, SSH keys, messaging apps |
A note about the future: a large enough quantum computer could break both RSA and today's elliptic curve methods. Such computers do not exist yet, but experts are already standardising new post-quantum algorithms, and some apps and browsers have started using them alongside current methods.
Key exchange: agreeing on a secret in public
The most common job of public-key cryptography is key exchange: letting two sides agree on a shared symmetric key over a network that attackers can watch.
The best-known method is Diffie-Hellman, published in 1976. A simple way to understand it is with paint:
- Ayesha and Bilal publicly agree on a common colour, say yellow.
- Each picks a secret colour and keeps it private. Ayesha picks red, Bilal picks blue.
- Each mixes their secret colour with yellow and sends the mixture to the other. An attacker sees both mixtures.
- Each adds their own secret colour to the mixture they received.
Both end up with the same final colour (yellow + red + blue), but the attacker cannot easily separate the mixtures to find the secret colours. In real Diffie-Hellman, mathematics replaces paint, and "separating the colours" is a problem that computers cannot solve in practice.
Modern systems use an elliptic curve version called ECDHE. The "E" at the end means ephemeral: a brand-new key pair is made for every session and thrown away afterwards. This gives forward secrecy. If an attacker records your encrypted traffic today and steals the server's long-term private key next year, they still cannot decrypt the old sessions, because the temporary keys no longer exist.
Digital signatures
Public-key cryptography can also prove who created a message and that it was not changed. This is a digital signature.
Signing uses the keys the other way round:
Signer: hash of message + signer's PRIVATE key --> signature
Anyone: message + signature + signer's PUBLIC key --> valid or invalid
Only the owner of the private key can create a valid signature. Anyone with the public key can check it. If even one character of the message changes, the check fails.
A digital signature gives:
- Authenticity: it came from the holder of the private key.
- Integrity: it has not been changed since it was signed.
- Non-repudiation: the signer cannot easily deny signing it.
Note that a signature does not hide the message. A signed message can still be read by anyone. If you need secrecy as well, you must also encrypt.
Where are signatures used?
- Software updates: your phone and computer check the maker's signature before installing an update. A fake update fails the check.
- App stores and code signing: apps and programs are signed by their developers.
- Certificates: website certificates are signed by certificate authorities, as you will see in the final lesson.
- Documents and transactions: signed PDFs, and approvals in some banking and government systems.
The trust problem
Public-key cryptography has one remaining problem. When you receive a public key that claims to belong to Bilal, how do you know it really is his?
An attacker who can sit between two people can perform a man-in-the-middle attack. He gives Ayesha his own public key while pretending to be Bilal, and gives Bilal his own key while pretending to be Ayesha. Each side encrypts to the attacker, who decrypts, reads, possibly changes, and re-encrypts the messages. The mathematics works perfectly, but the keys belong to the wrong person.
There are two main solutions:
- Check the key directly. Compare a short fingerprint of the key, a hash of it, through a separate trusted channel, such as in person or on a phone call. Messaging apps do this with "security codes" or "safety numbers" that two contacts can compare.
- Trust a third party. A trusted organisation signs a statement saying "this public key belongs to this name". That statement is a certificate, and it is how the web solves the problem.
Protecting your private keys
All of this security depends on one thing: the private key stays private. If it is stolen, an attacker can decrypt messages meant for you and sign anything in your name.
- Never email, upload or paste a private key into a chat or a code repository.
- Protect private key files with a passphrase, so a copied file is not immediately usable.
- In companies, keep important keys in a hardware security module (HSM) or a cloud key management service, where the key cannot easily be copied out.
- If a private key may have leaked, revoke and replace it at once.
Try it yourself on your own computer. Windows 10 and later, macOS and Linux usually include OpenSSH. This command creates a modern elliptic curve key pair for SSH logins:
ssh-keygen -t ed25519
It creates two files. The file ending in .pub is your public key, and you can share it. The other file is your private key, and it must never leave your control. Open the .pub file in a text editor and see how short an elliptic curve public key is.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. Sana wants to send Omar a secret message using public-key encryption. Which key does she use to encrypt it?
2. How is a digital signature checked?
3. An attacker sits between two people and swaps their public keys for his own. What is this attack called?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake