From the archive
The Code War: From Caesar and Enigma to Public-Key Cryptography
How ancient ciphers, Enigma codebreaking and centuries-old number theory led to public-key cryptography, the math that protects online banking and messaging.

Every time you see a little padlock next to a web address, two computers that have never met have just agreed on a secret key, in public, while anyone on the network could be listening. Then they use that key to scramble everything they send. Online shopping, banking, messaging apps and software updates all rely on this trick.
It sounds impossible, and for most of history it was. The method depends on mathematics that was studied for centuries purely because it was beautiful: the properties of prime numbers. This article follows the long contest between codemakers and codebreakers, from ancient Rome through the Enigma machine to the public-key revolution of the 1970s, and looks at the new challenge posed by quantum computers.
Secrets in the ancient world
People have tried to hide messages for as long as they have written them. Ancient Greek writers describe the Spartan scytale: a strip of leather or parchment wound around a rod, written on, then unwound into an apparently random string of letters. Only someone with a rod of the same thickness could read it.
The Roman historian Suetonius reports that Julius Caesar shifted each letter of the alphabet three places along. That is a substitution cipher: each letter is consistently replaced by another.
Try it: Caesar shift of 3
Plain alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Cipher alphabet: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
SCIENCE becomes VFLHQFH.
Simple substitution has a fatal weakness. In any language some letters are far more common than others; in English, E, T and A turn up constantly. By the ninth century the Arab scholar al-Kindi had described how to break such ciphers by counting how often each symbol appears, a technique called frequency analysis. In the scrambled word above, notice that the two Cs both became F and the two Es both became H. In a long message those patterns give the game away.
Many alphabets: the Vigenère cipher
The obvious fix is to keep changing the substitution. In the sixteenth century several European writers, including Giovan Battista Bellaso and Blaise de Vigenère, described ciphers that use a keyword to switch between shifted alphabets letter by letter. The method now carries Vigenère's name.
Using the keyword KEY, where K means "shift 10," E "shift 4" and Y "shift 24":
| Plaintext | S | C | I | E | N | C | E |
|---|---|---|---|---|---|---|---|
| Key letter | K | E | Y | K | E | Y | K |
| Shift | 10 | 4 | 24 | 10 | 4 | 24 | 10 |
| Ciphertext | C | G | G | O | R | A | O |
The two Cs now become different letters (G and A), which defeats simple frequency counting. The cipher was long considered unbreakable. But look at the two Es: both fell under the key letter K and both became O. The key repeats, and repetitions leak. In the 1850s the English mathematician and computing pioneer Charles Babbage worked out how to find the key length from repeated patterns and then break each alphabet separately; the Prussian officer Friedrich Kasiski published a similar method in 1863.
The Enigma challenge
By the twentieth century, codemaking had become mechanical. The German Enigma machine, used by the armed forces during the Second World War, passed each keystroke through a set of rotating wheels and a plugboard of cables. The rotors stepped with every key press, so the substitution changed for each letter, like a Vigenère cipher with an astronomically long key. With three rotors chosen from a set and ten plugboard cables, the standard army machine could be set up in roughly 1020 different ways.
The first breakthrough came from Poland. In 1932 the mathematician Marian Rejewski of the Polish Cipher Bureau used permutation theory, a branch of pure mathematics, to reconstruct the wiring of the military Enigma. With colleagues Jerzy Różycki and Henryk Zygalski he developed methods and machines to recover daily settings. In July 1939, weeks before Germany invaded Poland, the Poles shared their results with British and French intelligence.
At Bletchley Park in England, the mathematician Alan Turing, working with Gordon Welchman and others, designed the electromechanical bombe, which tested possible Enigma settings at high speed by exploiting likely words in messages ("cribs") and the fact that Enigma never encrypted a letter as itself. The first bombe went into service in 1940. Thousands of people, a majority of them women, worked at Bletchley Park by the end of the war, and the intelligence they produced shaped campaigns from the Battle of the Atlantic onward. The work also helped push forward electronic computing; our history of how computers evolved picks up that thread.
The perfect cipher nobody can use
There is one cipher that can never be broken: the one-time pad, in which the key is truly random, as long as the message and never reused. In 1949 Claude Shannon proved mathematically that it gives perfect secrecy. The problem is practical. Both sides need identical copies of an enormous key, delivered secretly in advance. For thousands of strangers communicating over a network, that is impossible.
The public-key revolution
Every cipher so far was symmetric: the same key locks and unlocks. So the central problem of cryptography was always key distribution: how do you get the secret key to the other person in the first place?
In 1976 Whitfield Diffie and Martin Hellman at Stanford published "New Directions in Cryptography," building on ideas they had discussed with Ralph Merkle. They proposed that two people could agree on a shared secret over a public channel, and that cryptosystems could use two keys: a public key anyone can use to lock a message, and a private key, kept secret, that alone can unlock it. The idea depends on one-way functions: operations that are easy to perform but practically impossible to reverse without a secret extra piece of information.
A year later, at MIT, Ron Rivest, Adi Shamir and Leonard Adleman found a practical way to do it, now called RSA. Their one-way function is multiplication. Multiplying two large prime numbers takes a computer a fraction of a second; recovering the two primes from their product, if it is hundreds of digits long, is believed to be practically impossible with ordinary computers. In 1977 Martin Gardner described RSA in Scientific American, along with a challenge: a 129-digit number to factor, with a $100 prize.
Years later it emerged that mathematicians at Britain's signals intelligence agency, GCHQ, had found equivalent ideas earlier, James Ellis around 1970 and Clifford Cocks and Malcolm Williamson soon after, but their work stayed classified until 1997. Diffie and Hellman received the 2015 ACM Turing Award, often called the Nobel Prize of computing; Rivest, Shamir and Adleman received it in 2002.
How RSA works, with very small numbers
The mathematics goes back to the French lawyer and amateur mathematician Pierre de Fermat, whose "little theorem" of 1640 describes what happens when you raise numbers to powers and keep only the remainder after dividing by a prime. Leonhard Euler generalized it in the 1700s. For centuries this was pure number theory with no practical use at all.
Here is a toy version using numbers small enough to check by hand (real keys use primes hundreds of digits long):
- Choose two primes: p = 5 and q = 11. Multiply them: n = 55.
- Compute (p − 1) × (q − 1) = 4 × 10 = 40.
- Choose a public exponent that shares no factor with 40: e = 3. The public key is (55, 3).
- Find the private exponent d so that 3 × d leaves remainder 1 when divided by 40: d = 27, because 3 × 27 = 81 = 2 × 40 + 1.
- To send the message 7, compute 7³ = 343, and take the remainder after dividing by 55: 343 − 6 × 55 = 13. The ciphertext is 13.
- To decrypt, compute 13²⁷ and take the remainder after dividing by 55. The answer is 7, the original message.
Anyone can see 55 and 3. To find 27 you need to know that 55 = 5 × 11, which is trivial here but hopeless when n has hundreds of digits.
In practice, public-key methods are slow, so secure websites use them mainly to agree on a fresh symmetric key and to check digital signatures, which prove that a website or software update really comes from who it claims. The bulk of the data is then scrambled with a fast symmetric cipher. Our guide to how the internet works shows where this fits.
How safe is safe?
RSA's security rests on factoring being hard, and mathematicians have kept making it easier. Improved methods such as the quadratic sieve and later the number field sieve, combined with more computing power, steadily raised the size of numbers that can be factored. Gardner's 129-digit challenge, once expected to last for ages, was cracked in 1994 by a team of several hundred volunteers sharing their computers over the internet. That is why recommended key sizes have grown over the years.
In 2002 three researchers at the Indian Institute of Technology Kanpur, Manindra Agrawal, Neeraj Kayal and Nitin Saxena, showed that testing whether a number is prime can always be done efficiently. The result did not threaten RSA, since primality testing is not factoring, but it settled a long-standing question in mathematics.
The quantum challenge
The biggest long-term threat comes from physics. In 1994 Peter Shor, then at AT&T Bell Labs, showed that a large enough quantum computer could factor numbers efficiently, which would break RSA and related systems. No quantum computer yet comes close to the required scale, but data recorded today could be decrypted later, so governments and companies are preparing now.
In August 2024 the U.S. National Institute of Standards and Technology (NIST) published its first three post-quantum cryptography standards, FIPS 203, 204 and 205. They are based on mathematical problems, such as finding short vectors in high-dimensional lattices, that are believed to be hard even for quantum computers. In March 2025 NIST selected an additional backup algorithm, HQC. Browsers and messaging apps have begun adding these methods alongside existing ones.
The pattern of this whole story is consistent. Permutation theory broke Enigma. Fermat's and Euler's number theory, studied for its own sake, became the basis of internet security. And quantum mechanics, developed to understand atoms, now threatens that security and also offers new kinds of protection.
Summary and questions
- Substitution ciphers fall to frequency analysis; Vigenère's repeating key falls to pattern-finding.
- Polish mathematicians and Bletchley Park's codebreakers broke Enigma using mathematics, machines and enemy mistakes.
- Public-key cryptography (Diffie and Hellman, 1976; RSA, 1977) solved the key-distribution problem using one-way functions.
- Quantum computing could break RSA, so new post-quantum standards were published in 2024.
To discuss: Why is a cipher only as strong as its key? Why might a scientist study prime numbers with no practical use in mind? For screen-free puzzles that build the same logical skills, try the activities in computational thinking without a screen. Game theory, another branch of mathematics about strategy and hidden information, is the subject of our archive story on auction design.
Sources and further reading
- Bletchley Park: Enigma and codebreaking history, Bletchley Park Trust
- New Directions in Cryptography (Diffie and Hellman, 1976), IEEE Transactions on Information Theory
- NIST Releases First 3 Finalized Post-Quantum Encryption Standards (2024), National Institute of Standards and Technology
- Cryptology, Encyclopaedia Britannica
- Original Beyond Discovery article (PDF), National Academy of Sciences, 2003 (archived at brucealberts.org)