TL;DR
In public-key cryptography, each party holds a pair of mathematically linked keys. The public key can be shared with anyone; the private key never leaves its owner. Anyone can encrypt a message with your public key, and only your private key can decrypt it. Your private key can also sign a message that anyone can check with your public key.
This solves the problem that two strangers cannot agree on a shared secret over a channel that an eavesdropper can read.
Where you'll see it: in HTTPS, where a browser and a server agree on a session key; in software signing, where a publisher signs an update so that users can verify it; and in messaging apps that exchange keys between devices.
This lesson explains how a cryptographic construction works. It is not advice about choosing or configuring cryptographic software.
Intuition
A mailbox with a slot
Picture a mailbox with a slot. Anyone can drop a letter through the slot, but only the owner has the key to open the box. The slot is the public key: it lets anyone send in. The key that opens the box is the private key. You can publish the slot's design on your website, and nobody who reads it can take letters back out.
Signing works the other way round. You seal a message with a stamp that only you can make, and anyone with a copy of your stamp design can check that the stamp is genuine.
Why the idea exists
Symmetric encryption, where both parties share one secret key, is fast and well understood. Its weakness is distribution: two parties who have never met have no secure way to agree on that key over an open network. Before 1976 the usual answer was a trusted courier or a pre-shared secret.
Public-key cryptography removes that step. The two parties can exchange public information in the open and end up with a shared secret that an eavesdropper, who sees all the public information, cannot compute. The mathematics that makes this work is a One-Way Function: easy to compute in one direction, infeasible to reverse without extra information.
The math
A public-key system consists of three algorithms:
- : generates a public key and a private key .
- : encrypts message into ciphertext .
- : decrypts using the private key.
Correctness requires for every message. Security requires that an attacker who knows and cannot recover with non-negligible probability. The keys are linked by a One-Way Function with a trapdoor: deriving from must be infeasible, but the owner knows the trapdoor that makes decryption easy.
RSA
RSA uses modular exponentiation (see Modular Arithmetic). Choose two large primes and , and let and .
- Choose a public exponent with .
- Compute the private exponent with .
- Public key: . Private key: (and the factors, which make decryption faster).
- Encrypt: . Decrypt: .
Decryption works because , and for some integer . Euler's theorem (a generalisation of Fermat's little theorem) then gives for messages that are coprime to ; the identity also holds for the other messages, which can be checked prime by prime. The message must be encoded as a number below . Knowing is equivalent to knowing the factors of , so recovering from amounts to factoring , which is believed to be infeasible for large . That is the trapdoor.
Diffie–Hellman key exchange
Diffie–Hellman does not encrypt a message. It lets two parties agree on a shared secret. Fix a prime and a generator (both public). Alice picks a secret and publishes . Bob picks a secret and publishes . Each computes the shared secret from the other's public value:
An eavesdropper sees , , and . Recovering from is the Discrete Logarithm Problem, which is assumed hard.
sequenceDiagram
participant Alice
participant Eve as Eavesdropper
participant Bob
Alice->>Bob: A = g^a mod p (public)
Bob->>Alice: B = g^b mod p (public)
Note over Eve: sees p, g, A and B only
Note over Alice,Bob: both compute g^(ab) mod p
Signatures
To sign a message with RSA, the signer computes , where is a hash function. Anyone checks by computing and comparing it with . Only the holder of can produce a value that passes this check.
Worked examples
Public values: , . Alice's secret , Bob's secret .
- Alice publishes . Successive powers: , , . So .
- Bob publishes . Using and , we get . So .
- Alice computes . Since , , and . Shared secret: .
- Bob computes , which also gives .
- An eavesdropper knows and . For a modulus this small, trying until finds in six steps. This is why real systems use primes hundreds of digits long.
Interpretation. Both sides reach the same secret without ever sending it. At realistic sizes the eavesdropper's search is infeasible.
Take , , so and . Choose . Then , since .
- Public key: . Private key: .
- Encrypt : .
- Decrypt : .
- An attacker who knows only must factor to find . Trial division finds the factors in a few dozen steps. At 2048-bit sizes that search is infeasible.
Interpretation. The example is correct but insecure by design: the modulus is tiny, so factoring is trivial. The mechanism is the same at real sizes, where only the parameters change.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Bulk data encryption | Public-key operations are slow | Use public-key cryptography to agree on a key, then symmetric encryption for the data |
| Small or poorly chosen parameters | Factoring or discrete logs become feasible | Use modern key sizes and standard curves |
| Unpadded RSA encryption | Deterministic encryption leaks information and allows attacks | Use padded schemes such as OAEP |
| Unauthenticated Diffie–Hellman | A man in the middle can impersonate either side | Authenticate the public values with signatures or certificates |
| Quantum computers | Shor's algorithm breaks RSA and discrete-log schemes at scale | Post-quantum public-key schemes |
| Lost private key | Encrypted data cannot be recovered | Key backup or escrow designed with the threat model in mind |
When to use it. Use public-key cryptography when parties cannot share a secret in advance, or when you need signatures that third parties can verify.
When not to. Do not use it to encrypt large amounts of data directly. It is far slower than symmetric encryption, which is why protocols combine the two.
Alternatives. Symmetric Encryption is faster but needs a pre-shared key. Elliptic-curve schemes give comparable security with smaller keys and are common in modern protocols. Hybrid encryption combines a public-key step with a symmetric cipher.
History
Ralph Merkle proposed public-key distribution through hard puzzles in the mid-1970s. Whitfield Diffie and Martin Hellman (1976), in New Directions in Cryptography, proposed public-key key exchange and the idea of separate encryption and decryption keys. Ron Rivest, Adi Shamir and Leonard Adleman (1977, published 1978) gave the first practical public-key encryption and signature scheme, RSA.
Researchers at the UK's GCHQ had independently developed similar ideas earlier in the 1970s, but their work was classified until the late 1990s. The public nature of the 1976–1978 work is what made the approach spread: it let commercial and academic systems adopt it, and it became the foundation of secure internet protocols.
Check yourself
QuestionRecall: what does a public key let someone do, and what does it not let them do?Show answer
A public key lets anyone encrypt a message to its owner or verify a signature made by the owner. It does not let anyone decrypt messages or create signatures, because those operations need the private key.
QuestionComputation: with and , Alice's secret is . What is her public value ?Show answer
.
QuestionWhy: why does Diffie–Hellman need authentication to be secure against an active attacker?Show answer
The protocol is secure against a passive eavesdropper, who only watches the public values. An active attacker can sit between the parties, replace each public value with its own, and establish a separate shared secret with each side. Each party then believes it is talking to the other, but it is talking to the attacker. Signatures or certificates that bind the public values to identities close this gap.
QuestionApplied: a messaging app sends the full message encrypted under RSA with a large public exponent for every message. What would you recommend?Show answer
Use RSA only to protect a short symmetric key, and encrypt the message itself with a symmetric cipher. RSA encryption is much slower per byte than symmetric encryption, and encrypting large data directly with unpadded RSA leaks information. The hybrid approach gets the key-distribution benefit of public-key cryptography at the speed of symmetric encryption.