Lesson
The theory — Substitution Ciphers: 26! Keys, Broken by Counting Letters
A substitution cipher replaces each letter of the message by another letter, the same one every time, according to a key: a rearrangement of the alphabet, so there are 26! ≈ 4.03 × 10²⁶ keys. A Caesar cipher is the special case that moves every letter the same number of places along the alphabet, which leaves 25 keys. Frequency analysis attacks either one by comparing how often each ciphertext letter occurs with how often letters occur in ordinary text.
What each symbol means
χ²- chi-squared: the sum over the 26 letters of (O − E)² / E. The smaller it is, the more the decryption's counts look like English
O- how many times a letter occurs in the decryption being scored
E- how many times it would occur in a text of the same length with the letter shares of The Dancing Men
- Assumes
- Only the 26 letters are enciphered. Spaces and punctuation stay where they were, which gives away the length of every word; the dancing men did the same with their flags. “English” means the letter counts of one Holmes story, 39,859 letters, and the page's own messages are all cut from another, so none of them is scored against itself. The trials draw a random stretch of The Solitary Cyclist and a random key from a seeded generator, so the same figures come back every time.
Problem solved in full
-
Where counting stops and reading starts 4 steps
Press Substitution, 600 letters. How long would a computer trying a billion keys a second take to try them all? Then break the message: what do the counts give you, and what do the commonest words give you?
-
There are 26! = 403,291,461,126,605,635,584,000,000 keys, about 4.03 × 10²⁶. At 10⁹ a second that is 4.03 × 10¹⁷ seconds, and a year is 3.16 × 10⁷ seconds, so 1.28 × 10¹⁰ years: 12.8 billion.
-
The commonest ciphertext letter is I, 80 of the 600, 13.3%. In The Dancing Men e is 12.44%, so I is e. The next three, O, N and X, have 49, 47 and 45, too close to put in order with any confidence, and ranking pairs them with t, a and o, all three wrong. The results table shows the cost: pairing by rank alone gets 3 of the 23 letters right, 14.2% of the text.
-
Now the words. The three-letter words OBJ and XDI occur five times each, and XDI ends in I, which is e: the, so X is t and D is h. OBJ is then most likely and: O is a, B is n, J is d. The commonest two-letter word, PK, eight times, fits of, and XP, five times, then reads to, which agrees.
-
Type those eight letters into the boxes. e, t, h, a, n, d, o and f cover 321 of the 600 letters, 53.5%, and the first line reads f·o· the ·ea·· 1894 to 1901. The counts have nothing more to give. Before a date, f·o· is almost certainly from, which hands you r and m, and from there it is reading.
Answer
12.8 billion years to try every key. Counting gives one letter, e; three common words give seven more, and those eight letters already decipher 53.5% of the message.
-
Learning path
How codes are broken
References (1)
- Both passages the page counts, and Holmes's words quoted in the second and third insights: Arthur Conan Doyle, "The Adventure of the Dancing Men" and "The Adventure of the Solitary Cyclist," in The Return of Sherlock Holmes. Project Gutenberg eBook #108.