How it works
A substitution cipher has a key that is an entire alphabet in scrambled order. Line it up under the normal alphabet and replace each letter of your message with the one beneath it.
Remembering 26 random letters is awkward, so the usual trick is a keyword. Write the keyword, drop any repeated letters, then continue with the unused letters of the alphabet:
Plain: 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: Z E B R A S C D F G H I J K L M N O P Q T U V W X Y
In the key box above you can type either a keyword or a complete 26-letter alphabet.
The Caesar, Atbash and affine ciphers are all substitution ciphers with especially tidy alphabets. This is the general case.
How to break it
With around four hundred million billion billion keys, trying each one is hopeless. It doesn’t matter. The cipher changes what the letters are but not how they behave:
- Frequency. In English, E, T, A, O, I and N make up nearly half of all letters. The commonest symbols in the ciphertext are almost certainly those.
- Patterns. A one-letter word is A or I. A three-letter word that appears constantly is THE. A word shaped like
ABCCBmight beHELLO; one shapedABBAmight beNOONorOTTO. - Pairs. Double letters are usually LL, EE, SS, OO or TT. Q is followed by U.
Solving by hand is a satisfying pencil puzzle, which is why “cryptograms” still run in newspapers. The solver here automates the same reasoning statistically. If you would rather do it yourself, the frequency analysis tool gives you the counts.
History
Frequency analysis was first written down by the Arab polymath al-Kindi in the ninth century, in A Manuscript on Deciphering Cryptographic Messages. In Europe the technique took centuries longer to spread, and simple substitution was trusted far longer than it deserved.
Its most famous failure came in 1586. Mary, Queen of Scots, corresponded with conspirators plotting to assassinate Elizabeth I, using a substitution cipher padded with code symbols. Elizabeth’s codebreaker Thomas Phelippes read the letters by frequency analysis. Mary was tried and executed the following year.
Questions people ask
How many possible keys are there?
Any ordering of 26 letters is a key, which gives about 4 × 10²⁶ possibilities, far too many to try one by one. The cipher is weak anyway, because you never need to try them all.
How does the automatic solver work?
It starts from a guess based on letter frequencies, then repeatedly swaps pairs of letters in the key, keeping any swap that makes the decoded text look more like English. It restarts many times from different guesses and keeps the best result. This is called hill climbing.
How much text does the solver need?
Around 100 letters is usually enough for a perfect result. With 40 to 80 letters it often gets most of the message, with a few rare letters such as J, Q, X and Z left wrong. Very short messages may not have a unique solution at all.
What is a keyword cipher?
A substitution cipher whose alphabet is built from a memorable word. Write the keyword without repeated letters, then follow it with the rest of the alphabet in order. ZEBRAS gives ZEBRASCDFGHIJKLMNOPQTUVWXY.