Chapter 01 · Classical ciphers
Hiding letters: from Caesar to Vigenère
For two thousand years encryption meant replacing letters by other letters. The ciphers grew more clever, from Caesar's shift to Vigenère's keyword, and the breakers grew more patient, from al-Kindi counting letters in ninth-century Baghdad to Babbage and Kasiski measuring the distance between repetitions.
In this chapter
The oldest known “encrypted” text is a tomb inscription in Menet Khufu, Egypt, from around 1900 BC, whose scribe used unusual hieroglyphs, probably to give the text dignity rather than to hide it. Real secrecy shows up a little later: a Mesopotamian clay tablet of about 1500 BC hides a recipe for pottery glaze by writing its words with rare signs, and Spartan generals wound a strip of leather around a staff of fixed thickness, the scytale, wrote along it and sent the strip: unwound, the letters were a jumble; wound on a staff of the same thickness, they lined up again. The scytale transposes letters, changing their order. Most of this chapter is about the other great family: ciphers that substitute letters.
Caesar's shift
Suetonius tells us that Julius Caesar wrote confidential letters “by so changing the order of the letters of the alphabet that not a word could be made out”, and that whoever wanted to read them had to “substitute the fourth letter of the alphabet, namely D, for A, and so with the others”. Number the letters . Caesar's cipher with key is
Caesar used ; Augustus, his nephew, used . Hebrew scribes had their own variant, Atbash, which reverses the alphabet (); it appears in the Book of Jeremiah. ROT13, the shift by 13 that hid joke punchlines on Usenet, is its own inverse because .
The weakness is the size of the key space: 25 useful keys can be tried by hand in minutes. The lesson, still valid, is the first rule of cipher design.
Any cipher can be attacked by trying every key and recognising the right plaintext. A cipher with keys is at most as strong as a search of trials; to be secure, must be beyond the attacker's means. Today that means at least .
Every permutation of the alphabet
The obvious fix is to allow any rearrangement of the alphabet, not just shifts: write the 26 letters in a scrambled order, often derived from a keyword, and replace each letter by the one under it. This monoalphabetic substitution was the workhorse of diplomacy for a millennium.
The keys of a monoalphabetic substitution are the permutations of 26 letters, so there are
of them: more than the keys of DES (), and far too many to try one by one, even today.
And yet substitution ciphers fall in minutes to a person with a pencil. A large key space is necessary but not sufficient: the cipher must also not leak structure. Monoalphabetic substitution leaks the most important structure of all.
Al-Kindi counts letters
In ninth-century Baghdad, at the House of Wisdom, the philosopher Abu Yusuf Yaqub ibn Ishaq al-Kindi wrote A Manuscript on Deciphering Cryptographic Messages, rediscovered in an Istanbul archive in 1987. In it he describes, for the first time, frequency analysis:
One way to solve an encrypted message, if we know its language, is to find a different plaintext of the same language long enough to fill one sheet or so, and then we count the occurrences of each letter. […] Then we look at the cipher text we want to solve and we also classify its symbols.al-Kindi, c. 850
Letters do not occur equally often. In English, E makes up about 12.7% of letters, T 9.1%, A 8.2%, while J, Q, X and Z together barely reach 0.5%; in Spanish, E (13.7%) and A (12.5%) lead. A substitution cipher renames letters but keeps their counts: the most frequent symbol of a long ciphertext is almost surely E. To measure how much a candidate decryption “looks like” the language, compare the observed counts with the expected ones :
A small means the text has the letter statistics of the language. This single number breaks any shift cipher automatically: decrypt with all 26 shifts and keep the one with the smallest .
For a general substitution the attacker matches the most frequent symbols first, then uses pairs (TH, HE, IN in English; DE, EN, EL in Spanish), doubled letters and short words to fix the rest. Edgar Allan Poe made a cult of it in The Gold-Bug (1843), and Arthur Conan Doyle gave Sherlock Holmes a substitution of dancing men to break in 1903. In real life, frequency analysis sent Mary, Queen of Scots, to the scaffold in 1587: Thomas Phelippes broke the nomenclator she used to correspond with the Babington plotters.
The indecipherable cipher
The defence against counting is to make each plaintext letter go to many different ciphertext letters. Leon Battista Alberti, the Renaissance architect, built a cipher disk in 1467 and suggested turning it every few words. Johannes Trithemius tabulated all 26 shifts in his tabula recta (Polygraphia, 1518). In 1553 Giovan Battista Bellaso added the decisive idea: choose the shift of each letter from a keyword that repeats. Blaise de Vigenère described a stronger variant in 1586, and history gave his name to Bellaso's cipher. With a keyword of length , :
With the keyword LEMON, ATTACKATDAWN becomes LXFOPVEFRNHR: the two Ts of ATTACK become X and F; the four As become L, O, E and N. The letter counts are flattened, and the cipher earned the name le chiffre indéchiffrable. It kept it for three hundred years; as late as 1917 Scientific American called it “impossible of translation”. It had been broken sixty years earlier.
Breaking Vigenère: Babbage, Kasiski, Friedman
The weakness is the repetition of the keyword. If we knew its length , we could split the ciphertext into columns (letters ; letters ), and each column would be a simple Caesar cipher, broken by frequency analysis. Charles Babbage found the length around 1854 but never published; the Prussian officer Friedrich Kasiski published the method in 1863:
When the same plaintext fragment falls on the same position of the keyword, it is encrypted identically. So the distances between repeated fragments of the ciphertext tend to be multiples of , and their greatest common divisor is usually or a small multiple of it.
In 1922 William Friedman, the founder of American cryptanalysis, replaced the search for repetitions with a statistic that uses every letter of the text.
The index of coincidence of a text of letters, with occurrences of letter , is the probability that two letters drawn at random from it are equal:
If a text's letters have probabilities , then as grows. For uniformly random letters this is ; for English it is about and for Spanish about . The IC is unchanged by any substitution, because a substitution only renames the letters.
Why the IC does not change under substitution
depends only on the multiset of counts , not on which letter has which count. A substitution is a bijection of the alphabet, so it permutes the counts and leaves the sum unchanged. In a Vigenère column built with the right , every letter was shifted by the same amount: the column is a substitution of plain language, so its IC is the language's. With a wrong , the column mixes several shifts, its letter distribution is flatter, and its IC drops towards .
So the attack is mechanical: for each candidate length , cut the ciphertext into columns and average their IC. The first whose average jumps to the language's value is the key length. Then each column is a Caesar cipher, and the of the previous section recovers each letter of the keyword.
Playfair and the digraphs
Another way to flatten frequencies is to encrypt pairs of letters: there are pairs, and their frequencies are far flatter than those of single letters. Charles Wheatstone invented such a cipher in 1854; his friend Lord Playfair promoted it so well that it carries his name. The key is a 5×5 square holding the letters of a keyword followed by the rest of the alphabet (I and J share a cell). The plaintext is split into pairs, with an X between doubled letters; each pair is replaced by the other two corners of its rectangle in the square, or by the letters to its right (same row) or below (same column). The British army used it in the Boer War and the First World War, for tactical messages that only had to stay secret for a few hours; it is still breakable by hand, using the frequencies of pairs. The desktop app includes it with Wikipedia's classic example.
What the classics teach
- The key space must be huge, or exhaustive search wins (Caesar).
- A huge key space is not enough: the ciphertext must not leak the statistics of the plaintext (substitution and al-Kindi).
- Anything that repeats is a foothold: a repeating key turns one hard cipher into several easy ones (Vigenère and Kasiski).
- Statistics beat secrecy: Friedman's IC needs no guess about the plaintext at all.
The next step was mechanical: a key that never repeats in practice, produced by a machine. That machine was Enigma.
Further reading
- David Kahn, The Codebreakers (1967; revised 1996). The classic history, from Egypt to the computer age.
- Simon Singh, The Code Book (1999). Chapters 1–2 cover Mary Stuart, frequency analysis and Babbage.
- Ibrahim A. Al-Kadi, “Origins of cryptology: the Arab contributions”, Cryptologia 16(2), 1992.
- William F. Friedman, The Index of Coincidence and Its Applications in Cryptography (1922).