What is the Caesar cipher?
The Caesar cipher replaces each letter of a message with the letter a fixed number of places further along the alphabet. It takes its name from Julius Caesar. The Roman writer Suetonius reports that Caesar wrote private letters with a shift of 3, so that A became D. You can do it with a pen and paper, on a cipher wheel or with our Caesar cipher tool.
How does it work?
Picture two copies of the alphabet, one above the other, with the lower one slid along by a few letters. That distance is the key. When the lower row runs out of letters it starts again from A, so with a shift of 3, X becomes A, Y becomes B and Z becomes C. In numbers, count A as 0 and Z as 25, add the key k, and wrap around at 26:
E(x) = (x + k) mod 26 H is 7, and 7 + 3 = 10, which is K. To go back, subtract the key instead of adding it.
Caesar cipher example, step by step
- Choose a shift between 1 and 25. Here it is 3.
- Take the first letter of your message and count that many places forward. H moves to K.
- Do the same with every other letter. E becomes H, each L becomes O, and O becomes R.
- Leave spaces, digits and punctuation as they are. HELLO is now KHOOR.
| Text | H | E | L | L | O |
|---|---|---|---|---|---|
| Shift 3 | K | H | O | O | R |
Only letters change, so HELLO, WORLD! with a shift of 3 becomes KHOOR, ZRUOG!
How to decrypt a Caesar cipher
If you know the key, count backwards by the same number of places: K goes back to H, H to E, and KHOOR turns into HELLO again. If you do not know the key, you can still get there, because there are only 25 shifts to try. Our solver lists them all for you. On a longer text, the frequency analysis tool gets there faster: the most common letter in English is E, so whatever appears most often in the coded text is probably E, and the distance between the two gives you the key.
Which shifts can you use?
With 26 letters you have 25 useful shifts, because a shift of 26 puts every letter back where it started. Moving backwards gives you nothing new: 3 places back is the same as 23 forward. A shift of 13 has its own name, ROT13, and it undoes itself when applied twice. If you change the shift from letter to letter using a keyword, you have the Vigenère cipher, which is much harder to break.
Is the Caesar cipher secure?
No. Anyone can try all 25 keys in a few minutes by hand and in an instant by computer, so never use it to protect passwords or private data. Real data needs modern encryption such as AES. The cipher is still useful for learning: it shows what a key is and why a small number of keys is a weakness. You will also meet it in puzzles and escape rooms, and it is a popular first exercise in programming, as in our Python tutorial.
Try it yourself
This message was encrypted with a shift of 5. Set the demo at the top of this page to 5, find each letter in the lower row and read the one above it:
Ymj pjd nx zsijw ymj rfy Show the answer
The key is under the mat.
Then write a message of your own, encrypt it with any shift and give it to a friend without telling them the key. Check your work in the tool.
Frequently asked questions
Why is it called the Caesar cipher?
Because Julius Caesar used it. Suetonius, who wrote about his life, describes letters in which each letter was replaced by the one three places later.
What is the key in a Caesar cipher?
The number of places each letter moves. Sender and reader must use the same number, and the reader moves the letters in the opposite direction.
How do you solve a Caesar cipher without the key?
Try every shift until the text makes sense, or count the letters and compare them with the language. The solver does the first for you.
What is the difference between Caesar and ROT13?
ROT13 is a Caesar cipher with the shift fixed at 13. That is half of a 26-letter alphabet, so the same step both encrypts and decrypts.