The Caesar cipher explained

The Caesar cipher is the oldest cipher with a famous name and the first one most people learn. It is simple enough to do in your head and weak enough to break in minutes, and that combination makes it the ideal place to see the two basic ideas of cryptography at work: a secret key that turns a message into nonsense, and the patterns that let an attacker read it anyway.

A cipher wheel with the plain alphabet on the outer ring and the alphabet shifted by three on the inner ring, A over D
A cipher wheel with the plain alphabet on the outer ring and the alphabet shifted by three on the inner ring, A over D

Caesar's cipher

The Roman historian Suetonius wrote that Julius Caesar, when he had something confidential to say, wrote it so that the order of the letters could not be made out: he replaced each letter with the one three places further on, D for A, and so on. His successor Augustus is said to have used a shift of one. Any fixed shift from 1 to 25 works the same way, and the shift is the key. On a cipher wheel, two rings of letters, the key is simply how far the inner ring is turned.

A Roman marble bust of Julius Caesar, often identified as the Tusculum portrait. According to Suetonius, Caesar replaced each letter with the one three places further on.
A Roman marble bust of Julius Caesar, often identified as the Tusculum portrait. According to Suetonius, Caesar replaced each letter with the one three places further on.

The arithmetic of shifting

Number the letters from A = 0 to Z = 25. Encrypting with key k turns letter x into (x + k) mod 26, and decrypting turns it back into (x − k) mod 26: this is the arithmetic of a clock with 26 hours. Because the alphabet has 26 letters, a shift of 13 is its own inverse, which is why ROT13 can both hide and reveal a punchline. Shifting by 26, or by 0, changes nothing, so there are 25 useful keys.

Breaking it by brute force

A cipher is only as strong as the effort it takes to try its keys. With 25 keys, an attacker can write out every possible decryption and see which one makes sense; with a computer, it takes less than a millisecond. Modern ciphers such as AES have 2¹²⁸ or more keys, so many that trying them all is impossible with any computer that could ever be built. A large key space is the first requirement of a secure cipher.

Frequency analysis

In the 9th century the Arab philosopher and scientist al-Kindi described a method that needs no guessing of keys: count how often each letter appears in the ciphertext and compare with how often letters appear in the language. In English about one letter in eight is an E, while J, Q, X and Z are rare. A Caesar cipher only moves this pattern along, so the tallest peak in the ciphertext is very likely E, and the shift follows at once. The method also breaks any cipher that always replaces a letter with the same symbol, however scrambled the alphabet.

Scoring with chi-squared

To let a computer judge which decryption looks most like a language, compare its letter counts with the counts the language would lead you to expect. The chi-squared statistic adds up, for every letter, the squared difference between the observed and the expected count, divided by the expected count. A text that matches the language closely scores low; gibberish scores high. Ranking all 26 shifts by this score almost always puts the right one first, as soon as the text has a few dozen letters.

Ciphers that fought back

Frequency analysis drove cipher makers to hide the frequencies. Homophonic ciphers gave common letters several symbols. The Vigenère cipher of the 16th century used a keyword to apply a different Caesar shift to each letter in turn, and was called the indecipherable cipher for three hundred years, until Charles Babbage and Friedrich Kasiski showed how to find the keyword's length and then break each shift separately, with frequency analysis again. Rotor machines such as Enigma went further still, and modern cryptography replaced letters with mathematics altogether.

Caesar ciphers in history

Weak as it is, the shift cipher has been used for real secrets far longer than it deserved. Mary, Queen of Scots, was convicted in 1586 on letters in a more elaborate substitution cipher that was broken by frequency analysis. As late as the early 20th century, armies still sent messages in simple shift ciphers, and in 2006 the Sicilian Mafia boss Bernardo Provenzano was caught partly through notes that wrote each letter as a number three higher than its place in the alphabet.

Explore it

Open the Caesar cipher cracker to encrypt and decrypt your own messages, turn the cipher wheel, and break messages without their key. The chart shows the letters of the result against the frequencies of the language you choose, so you can watch the peaks line up as the shift becomes right, and the table ranks all 26 shifts by their chi-squared score. Try the pangram example to see frequency analysis struggle with a text that uses every letter once.

Frequently asked questions

Is the Caesar cipher still used?

Not for secrets. ROT13 still hides spoilers and puzzle answers, the cipher is used to teach cryptography and modular arithmetic, and it appears in puzzles, escape rooms and geocaching. Anything that has to stay private uses modern ciphers with enormous numbers of keys.

What is the difference between a Caesar cipher and a substitution cipher?

A Caesar cipher is a special substitution cipher in which the replacement alphabet is the ordinary alphabet shifted along. A general substitution cipher can use any rearrangement of the alphabet, which gives about 4 × 10²⁶ keys, far too many to try. It still falls to frequency analysis, because each letter is always replaced by the same symbol.

How long must a text be to crack it reliably?

With the chi-squared score, a few dozen letters usually suffice for English, and a couple of sentences almost always do. Very short texts, or texts with an unusual choice of letters, can make a wrong shift score best, which is why the app shows the whole ranking and how clearly the winner stands out.

Why do the letter frequencies differ between languages?

Each language uses its letters differently: E is the most common letter in English, German, French and Dutch, but A is the most common in Portuguese and nearly ties with E in Spanish and Italian, and K, W and Y are rare in the Romance languages. Choosing the language of the message makes the score more reliable.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the Caesar cipher cracker