The problem it solves
Until the 1970s every cipher was symmetric: the key that encrypted a message also decrypted it. From the Caesar cipher to Enigma, sender and receiver had to agree on a secret key before they could talk privately, using couriers, code books handed over in person, or a trusted central office that held everyone's keys. None of that works when millions of strangers need to talk securely over a public network. A shop can't meet every customer in advance to hand over a key.
Public key cryptography removes that need. Each person has two related keys. The public key can be published anywhere. The private key is kept secret. Data encrypted with the public key can only be decrypted with the private key, and knowing the public key doesn't help anyone work out the private one.
- Whitfield Diffie and Martin Hellman published the idea in "New Directions in Cryptography" in 1976.
- Ron Rivest, Adi Shamir and Leonard Adleman published RSA, the first practical public key encryption system, in 1977.
- At the British intelligence agency GCHQ, James Ellis had the idea around 1970, and Clifford Cocks found an RSA-like system in 1973. Their work stayed classified until 1997.
A mailbox is a fair picture of it: anyone can push a letter through the slot (the public key), but only the owner has the key that opens the box (the private key).
One-way functions with a trapdoor
Public key systems are built on mathematical operations that are easy to do and very hard to undo, unless you know a secret, the trapdoor. Mixing two paints into green takes seconds, while separating green paint back into the original colours is practically impossible. Multiplying two prime numbers is like mixing the paint:
Easy: 61 × 53 = 3233 Hard: 3233 = ? × ?
For a four-digit number you can find the factors by trial, but real RSA uses numbers of 2048 bits or more, over 600 decimal digits. No known method on ordinary computers can factor a well-chosen number of that size in any useful time.
RSA with small numbers
Here is the whole of RSA with numbers small enough to check by hand. Real keys work the same way with much larger primes.
1. Make the key pair
- Pick two primes: p = 61 and q = 53.
- Multiply them: n = 61 × 53 = 3233. This is the modulus, part of both keys.
- Compute φ = (p − 1) × (q − 1) = 60 × 52 = 3120.
- Choose a public exponent e that shares no factor with 3120: e = 17. (Real keys almost always use 65537.)
- Find the private exponent d such that e × d leaves a remainder of 1 when divided by φ: d = 2753, because 17 × 2753 = 46801 = 15 × 3120 + 1.
Public key: n = 3233, e = 17 Private key: n = 3233, d = 2753
2. Encrypt with the public key
Turn the message into a number smaller than n, say m = 65. Raise it to the power e and keep the remainder after dividing by n:
c = 65^17 mod 3233 = 2790
3. Decrypt with the private key
m = 2790^2753 mod 3233 = 65
Anyone who knows n and e can encrypt, but decrypting needs d, and computing d needs φ, which needs the factors p and q. With a 2048-bit n, finding them is the hard problem. In practice RSA also needs a padding scheme such as OAEP; raw "textbook" RSA as shown here is insecure for real messages.
Diffie-Hellman key exchange
Diffie-Hellman lets two people agree on a shared secret over a public channel without ever sending the secret. It relies on a different hard problem: given ga mod p, finding a is hard (the discrete logarithm problem). With small numbers:
Public: p = 23, g = 5 Alice picks secret a = 6 sends A = 5^6 mod 23 = 8 Bob picks secret b = 15 sends B = 5^15 mod 23 = 19 Alice computes 19^6 mod 23 = 2 Bob computes 8^15 mod 23 = 2 shared secret: 2
Someone listening sees 23, 5, 8 and 19, but to get the secret they would need a or b. With numbers thousands of bits long, or with the elliptic curve version, that is infeasible. Today's TLS connections use this method in its elliptic curve form (ECDHE, usually X25519), and the shared secret becomes the key for a fast symmetric cipher.
Elliptic curve cryptography
Elliptic curve cryptography (ECC) builds the same kind of one-way function from points on a curve of the form y² = x³ + ax + b over a finite field. Adding a point to itself many times is easy; working out how many times from the result is hard. The advantage is size: a 256-bit elliptic curve key gives about the same security as a 3072-bit RSA key. Operations are faster and use less power, which suits phones and small devices. X25519 for key exchange and Ed25519 and ECDSA for signatures are the common choices.
Digital signatures
The same key pairs can prove who wrote something. In outline:
- The signer hashes the message, for example with SHA-256.
- The signer applies their private key to the hash. The result is the signature.
- The message and signature are sent together.
- The receiver hashes the message and uses the signer's public key to check the signature against that hash.
- If it checks out, the message came from the holder of the private key and hasn't changed since it was signed.
For textbook RSA, signing is the decryption operation applied to the hash, which is where the common description "encrypting with the private key" comes from. Ed25519 and ECDSA use different mathematics but give the same guarantees: authentication, integrity, and non-repudiation, meaning the signer can't plausibly deny having signed. Digital signatures explained goes into more detail.
Where public key cryptography is used
HTTPS
The server's certificate contains its public key. During the TLS handshake the browser and server agree on a session key with elliptic curve Diffie-Hellman, and the server signs the handshake with its private key to prove its identity. The page data itself is then encrypted with a fast symmetric cipher such as AES. Older TLS versions could also have the browser encrypt a random session key with the server's RSA key; TLS 1.3 dropped that method. How HTTPS works has the full handshake.
Email encryption
With PGP, or its open source implementation GnuPG, you get the recipient's public key from them or a key server, encrypt the message with it, and optionally sign it with your private key. Only the recipient's private key can decrypt it. Thunderbird has OpenPGP support built in.
SSH
Developers log in to servers with SSH key pairs. You put your public key on the server; when you connect, the server sends a challenge, your computer signs it with your private key, and the server checks the signature with the public key. No password crosses the network.
Software and cryptocurrency
App stores, operating system updates and Windows programs are signed by their publishers, and your device checks the signature before installing. In Bitcoin and similar systems, your address is derived from a public key, and every transaction must be signed with the matching private key. Lose the private key and the funds can't be spent by anyone.
Knowing whose key it is
Public keys bring a new problem: how do you know a public key really belongs to the person or website it claims to? An attacker who swaps in their own key can read everything.
Certificate authorities
The web uses a public key infrastructure (PKI). Root certificate authorities, such as Let's Encrypt or DigiCert, have their certificates built into browsers and operating systems. They sign intermediate certificates, which sign the certificates of individual websites. When an authority is compromised, attackers can issue certificates for any domain, as happened with DigiNotar in 2011; browsers removed trust in DigiNotar entirely, and later in Symantec's certificates after a series of mis-issuance problems found in 2017. Certificate Transparency logs now record every public certificate so misuse can be spotted.
Web of trust
PGP uses a decentralized model instead. People check each other's identities and sign each other's keys. If you trust Alice and Alice has signed Bob's key, you may decide to trust Bob's key too. It needs no central authority, but it also needs users to do the checking.
Threats
- Weak randomness. Keys generated from a poor random source can be guessed or can share factors with other keys.
- Side-channel attacks. Timing or power measurements of a device can leak a private key if the implementation isn't constant time.
- Man-in-the-middle attacks. An attacker who can replace a public key during the first exchange can read and change everything. Certificates, out-of-band fingerprint checks and certificate pinning defend against this.
- Careless key handling: private keys left unencrypted, copied around or never rotated.
Quantum computers
Shor's algorithm, run on a large and reliable quantum computer, would break RSA, Diffie-Hellman and elliptic curve cryptography. Today's quantum computers are nowhere near that size; a 2025 estimate put breaking RSA-2048 at just under a million noisy qubits running for about a week. In 2024 NIST published post-quantum standards built on other problems: ML-KEM for key exchange (based on CRYSTALS-Kyber), and ML-DSA and SLH-DSA for signatures (based on CRYSTALS-Dilithium and SPHINCS+), with a FALCON-based standard on the way. The transition uses hybrids, classical and post-quantum algorithms together, so an attacker would have to break both.
In practice
If you are building software:
- Use a tested library such as libsodium or OpenSSL. Don't implement RSA or elliptic curves yourself.
- For new designs use X25519 for key exchange and Ed25519 for signatures. Where you need RSA, use at least 2048-bit keys, and 3072 for long-term use.
- Use public key cryptography only to agree on keys and sign things; encrypt the data itself with AES-GCM or ChaCha20-Poly1305.
- Generate keys on trusted machines with a secure random source, never with an online key generator.
- Store important private keys in a hardware security module, a smart card or the operating system's key store; at minimum, encrypt key files with a strong passphrase.
- Give keys an expiry date, rotate them, and revoke any key you think has leaked.
As a user, you already rely on public key cryptography every time you see a padlock in the address bar, update an app or use an end-to-end encrypted messenger. The symmetric vs asymmetric encryption article explains how it works together with symmetric ciphers.
Further reading
- Symmetric vs Asymmetric Encryption: The Difference One shared key or a key pair, and why HTTPS uses both.
- Digital Signatures Explained: How They Prove Authenticity How a key pair proves who sent a file and that nobody changed it.
- How HTTPS Works: TLS, Certificates and Encryption What the padlock in the address bar actually checks.