Say you have a line like Wood wo li dro qboox qkdo kd cofox. and no key. If it was made with a Caesar cipher, you don't need the key, because the cipher gives itself away. This article explains why there are so few keys to try, how letter frequencies point to the right one and when that method goes wrong. To encode or decode text, or to see every shift at once, use the Caesar cipher tool. Its page covers the how-to.

A cipher with only 25 useful keys

A Caesar cipher swaps each letter for the one a fixed number of places further along the alphabet. The oldest description comes from the Roman biographer Suetonius. He wrote that anyone who wanted to read Julius Caesar's confidential letters "must substitute the fourth letter of the alphabet, namely D, for A, and so with the others" (Suetonius, Life of Julius Caesar, 56). That is a shift of three.

The key is just that number. With 26 letters there are 26 possible shifts, as Trinity College's notes on the cipher point out. One of them, shift 0, leaves the text unchanged. A shift of 26 brings every letter back to where it started, so a shift of 27 is the same as a shift of 1, and so on. That leaves 25 keys that actually hide anything.

That is few enough to try every one by hand. A general substitution cipher lets any letter stand for any other, which gives 26 factorial keys, a 27-digit number. Trying every shift is the simplest way to break a Caesar cipher. Frequency analysis tells you which shift to try first, and it still works on substitution ciphers where trying every key is impossible.

Frequency analysis: some letters are much more common

The idea is more than a thousand years old. The first known written account comes from the 9th-century scholar al-Kindi in Baghdad, in a treatise known as A Manuscript on Deciphering Cryptographic Messages. As Simon Singh summarises it, al-Kindi noticed that letters appear with different frequencies in ordinary text. He advised counting the letters in the ciphertext and matching the most frequent one to the most frequent letter of the language.

English letter frequencies are well documented. Here are two tables built from very different samples:

Rank Google Books n-grams, 743.8 billion word occurrences (Norvig) Sample of 40,000 words (Cornell)
1 E 12.49% E 12.02%
2 T 9.28% T 9.10%
3 A 8.04% A 8.12%
4 O 7.64% O 7.68%
5 I 7.57% I 7.31%
6 N 7.23% N 6.95%

In both tables, Z, Q, J and X each make up less than a quarter of a percent of letters.

A Caesar cipher moves every letter by the same amount, so this pattern survives encryption intact. The whole distribution just slides along the alphabet. The most common letter in the ciphertext is probably E, and the distance between the two letters is the key.

Worked example: cracking a short message

Here is the ciphertext again:

Wood wo li dro qboox qkdo kd cofox.

First, count the letters, ignoring case. There are 27 letters, and O stands out:

Ciphertext letter Count
O 9
D 4
K, Q, W, X 2 each
B, C, F, I, L, R 1 each

Next, assume O stands for E. Counting A as 0, O is at position 14 and E is at position 4. 14 − 4 = 10, so the candidate key is 10.

Then check the second most common letter. If the key is 10, D (position 3) should be T (position 19). 3 − 19 = −16, which is 10 once you add 26. Two different letters point to the same key.

Finally, confirm it. Paste the ciphertext into the Caesar cipher tool, switch to decode and find shift 10 in the grid of all 26 shifts. It reads Meet me by the green gate at seven. No other entry looks like English. Clicking the entry also moves the slider to that shift. The letter count gave you the key, and the grid lets you confirm it at a glance. The check matters because a frequency guess can be wrong.

When letter counts mislead

Short texts

A message of a few words is a tiny sample. Encrypt Attack at dawn with shift 10 and you get Kddkmu kd nkgx. The most common letter is K, with 4 of the 12 letters. If you assume K is E, you get a shift of 6 and the nonsense Exxego ex hear. The plaintext's most common letter is A, not E. Treat the frequency ranking as a list of candidates and work down it: K as T fails, and K as A gives the correct shift of 10. Even the two large tables above differ slightly (H is 5.05% in one and 5.92% in the other), and a single sentence varies far more.

Upper and lower case

Capitals don't change the cipher. The tool shifts A–Z and a–z by the same amount and keeps each letter's case. When you count by hand, though, combine upper and lower case. Otherwise E and e land in separate columns and both look rarer than they are. All-caps strings also cause a different problem: they are often abbreviations, codes or lists rather than sentences, and those don't follow a table built from ordinary prose.

Text that isn't English

Every frequency table describes one language. Al-Kindi's method rests on the fact that each language has its own typical letter frequencies (Singh). If the plaintext might be Polish, French or Spanish, compare against that language's table. In this tool there is also a visible clue: only unaccented a–z and A–Z are shifted, so letters such as ą, ę or ñ stay as they are in the ciphertext.

Scoring every key: the chi-squared idea

Matching one letter uses only a small part of the evidence. The chi-squared statistic uses all of it. In plain words:

  1. Decode the ciphertext with one candidate shift.
  2. For each letter, compare how many times it appears with how many times English would lead you to expect in a text of that length.
  3. Square each difference, divide it by the expected count and add the results for all 26 letters.

The result is one number. A small value means the decoded text has letter frequencies close to English; a large value means it doesn't. Do this for every shift and pick the lowest. That is the method described by the University of Southampton and Trinity College. Squaring makes large mismatches count heavily. Dividing by the expected count scales each mismatch to what is normal for that letter, so a few extra Zs or Qs push a wrong shift's score up sharply.

Chi-squared is what most automatic Caesar solvers do, but it is still a statistic. On a very short message it can rank a wrong shift first, for the same reason a single letter count can. Use the score to rank the candidates, then read the best few.

Why this matters beyond Caesar

Any cipher that always replaces a given letter with the same substitute keeps the language's letter frequencies, just under different labels. That is the weakness al-Kindi described, and it is why a Caesar shift, ROT13 included, only hides text from a casual glance. It does not keep anything confidential. Use it for puzzles and spoilers, not secrets.

FAQ

Why did treating the most common letter as E give me nonsense?

Because your text is not an average English sample. Short messages, lists and headlines often have A, T or O as their most common letter. Try the next candidates (T, A, O) in turn, or score every shift with chi-squared and read the top results.

How long must a ciphertext be for frequency analysis to work?

There is no fixed minimum. The longer the text, the closer its counts come to the table and the more reliable the first guess. A Caesar cipher has only 25 keys to check, so even for a one-line message you can confirm the guess by reading the decoded text.

Does frequency analysis break substitution ciphers that are not a simple shift?

Yes, but with more work. In a general substitution cipher each letter has its own substitute, so you have to match many letters one by one, using common pairs and short words as well as single-letter counts. With a Caesar cipher, one correct match reveals the whole key.

Can I use English frequencies on a message in another language?

Only as a rough guide. E is common in many European languages, but the rest of the ranking differs. Compare against a table for the language you suspect, and remember that accented letters may have been left unshifted.