Shared-key encryption: lock and unlock with the same key
When the key that encrypts and the key that decrypts are the same, it’s called shared-key (symmetric) encryption. The Caesar cipher belongs to this family. Today’s best-known example is AES, with keys 128 or 256 bits long. Even at 128 bits there are 2¹²⁸ (about 3.4×10³⁸) possible keys — worlds apart from the Caesar cipher’s 25. It’s fast, so it suits encrypting large amounts of data.
Its weakness is how to hand the same key to the other person safely (the key-distribution problem). And you need a different key for each partner. For n people to all talk to each other in secret, you need n(n−1)÷2 keys: 45 for 10 people, and 4,950 for 100.
Public-key encryption: different keys to lock and unlock
Public-key (asymmetric) encryption uses a pair of two keys. One, the “public key,” can be handed to anyone. The other, the “private key,” is kept only by you. Whatever is encrypted with the public key can only be decrypted with its paired private key.
Think of the public key as “a padlock anyone can snap shut” and the private key as “the one key that opens that padlock.” Anyone who wants to send A a secret just locks a box with A’s padlock and sends it. Only A, who holds the key, can open it. That solves the key-handover problem. Well-known examples are RSA and elliptic-curve cryptography, but they are much slower than shared-key encryption.
HTTPS takes the best of both
When you talk to a website over HTTPS, public-key techniques are used first to agree safely on a shared key used only for that session. After that, the bulk of the data is encrypted with fast shared-key encryption. The two methods split the work: one for agreeing on a key safely, the other for sending lots of data fast.
Hashes and digital signatures
A hash function (such as SHA-256) turns data of any length into a fixed-length value, like a “fingerprint.” Change even one character of the original and the value becomes completely different, and you can’t get the original data back from the value. It’s used not to recover data but to check whether it has been tampered with.
With a digital signature, the sender signs with their own private key, and the receiver checks it with the sender’s public key. Only the sender holds the private key, so you know “the sender really sent it” and “it wasn’t altered on the way.”
| What you want to do | Key to use |
|---|---|
| Send secret data (encrypt) | The receiver’s public key |
| Read received data (decrypt) | The receiver’s private key |
| Sign | The sender’s private key |
| Verify a signature | The sender’s public key |
When you install a certificate on a web server, you put its private key file there too. If it leaks, someone can build an impostor site, so the golden rules are to lock down permissions on where it’s stored and never send it by email or chat.
Also, today’s public-key encryption could be broken by future quantum computers. The switch to new methods that are hard even for quantum computers (post-quantum cryptography) has already begun.
Practice questions
Four-choice questions in the style of Japan’s Fundamental Information Technology Engineer Examination. Tap below to see the answer.
Q1With shared-key encryption, 10 people each want to talk to every other person without anyone else knowing. How many keys are needed?
- A10
- B20
- C45
- D100
Show the answer and explanation
Answer: CFor n people you need one key per pair: n(n−1)÷2. 10×9÷2 = 45. With public-key encryption, one pair (public and private key) per person is enough.
Q2With public-key encryption, when A sends B a secret message, which key is used to encrypt it?
- AA’s public key
- BA’s private key
- CB’s public key
- DB’s private key
Show the answer and explanation
Answer: CIf you encrypt with the receiver’s (B’s) public key, only B’s private key can decrypt it.
Q3With a digital signature, which key does the receiver use to verify the signature?
- AThe sender’s public key
- BThe sender’s private key
- CThe receiver’s public key
- DThe receiver’s private key
Show the answer and explanation
Answer: AThe signature is made with the sender’s private key and checked with the sender’s public key. Only the sender holds the private key, so you can confirm both that the sender sent it and that it wasn’t altered.
- Shared-key encryption is fast, but handing over keys is the challenge. n people need n(n−1)÷2 keys
- Public-key encryption encrypts with the receiver’s public key, and the receiver decrypts with their private key
- Hashes check for tampering. A digital signature is made with the sender’s private key