Caesar cipher: the maths
Modular arithmetic, encryption and decryption formulas, key space: the Caesar cipher written as formulas.
If we number the letters from 0 (A) to 25 (Z), the Caesar cipher becomes a simple operation of modular arithmetic.
- Encryption
- C = (P + k) mod 26
- Decryption
- P = (C − k) mod 26
- Key
- k, an integer from 1 to 25 (k = 0 would leave the text unchanged)
- Key space
- 25 useful keys: very few
The mod 26 operation returns the remainder of division by 26: it is what lets the alphabet “wrap around”. For example, for Z with key 3: 25 + 3 = 28, and 28 mod 26 = 2, that is C.
Useful properties
- 01
Two encryptions are one encryption
Encrypting with k₁ and then with k₂ is the same as encrypting once with k₁ + k₂: adding steps does not add security.
- 02
Decrypting is encrypting
Decrypting with key k is the same as encrypting with key 26 − k.
- 03
ROT13 is its own inverse
With k = 13, encrypting twice returns the original text, because 13 + 13 = 26.
The big brother: the affine cipher
The Caesar cipher is a special case of the affine cipher, in which C = (a·P + b) mod 26. With a = 1 we are back to Caesar. For decryption to be possible, a must be coprime with 26: there are 12 valid values of a and 26 of b, hence 312 keys. Still far too few to resist a computer, and still vulnerable to frequency analysis.
Caesar cipher: how it is broken
Brute force, frequency analysis and known plaintext: three ways to break the Caesar cipher, each faster than the last.
The pages of this topic
Back to the overviewCaesar cipher: the history
Suetonius, Julius Caesar, Augustus and letters written in Greek characters: what we really know about Roman ciphers.
Caesar cipher: how it works
Two alphabets side by side, one shifted: the mechanism of encryption and decryption step by step.
Caesar cipher: a worked example
Let us encrypt and decrypt VENI VIDI VICI with key 3, letter by letter.
Caesar cipher: today’s uses
ROT13, puzzles, teaching and obfuscation: where the Caesar cipher still turns up, and where it must never be used.
Caesar cipher: try it yourself
Three exercises to solve with pen and paper: encrypt, decrypt with the key, and break a message without the key.
Caesar cipher: frequently asked questions
Short answers to the most common doubts about the Caesar cipher and its variants.