Glossary

Caesar Cipher

Caesar cipher is a substitution cipher that shifts every letter a fixed number of places along the alphabet, so a shift of 3 turns A into D and Attack into Dwwdfn. The shift is the whole key. It is named after Julius Caesar, who is reported to have used a shift of 3, and today it is a teaching example and puzzle staple, never a way to protect data.

How it works

Number the letters A=0 through Z=25. Encryption adds the shift k to each letter and wraps around modulo 26. Decryption subtracts k. Written as a formula, that is E(x) = (x + k) mod 26 and D(x) = (x - k) mod 26.

  • Key space: shifts 1 to 25 change the text, and shift 0 (or 26) leaves it unchanged. That leaves 25 useful keys.
  • Case and symbols: the classic version shifts only the 26 Latin letters. Spaces, digits and punctuation pass through unchanged, which leaks word lengths.
  • Decrypting: a message encrypted with shift k is decrypted with shift 26 - k. A shift of 13 is its own inverse, which is why ROT13 is the same operation both ways.
  • Non-ASCII: accented letters and other scripts fall outside A to Z and are normally left alone, so a Caesar implementation must say what it does with them.

The Python below encrypts with a shift of 3, then prints every candidate shift for a short ciphertext. Only one line reads as English.

def caesar(t, k):
    return ''.join(chr((ord(c) - 65 + k) % 26 + 65) if c.isupper()
                   else chr((ord(c) - 97 + k) % 26 + 97) if c.islower()
                   else c for c in t)

print(caesar("Attack at dawn", 3))
for k in (1, 2, 3, 4, 25):
    print(k, caesar("Dwwdfn dw gdzq", -k))
Dwwdfn dw gdzq
1 Cvvcem cv fcyp
2 Buubdl bu ebxo
3 Attack at dawn
4 Zsszbj zs czvm
25 Exxego ex hear

How many keys does the Caesar cipher have?

The Caesar cipher has 25 usable keys, one for each shift from 1 to 25. An attacker tries all 25 and reads the one that makes sense, which takes a fraction of a second by hand or by script. Even without brute force, counting letter frequencies finds the shift, because the most common ciphertext letter usually maps to E.

Common pitfalls

  • Treating it as encryption: with 25 keys and no secret beyond a small integer, the output is obfuscation. Do not use it for passwords, tokens or personal data.
  • Wrong direction: some tools shift forward to encrypt and others shift backward. If decrypting with shift 3 gives garbage, try 23, which is 26 - 3.
  • Forgetting case: lowercasing the input before shifting loses capitalization, and shifting only uppercase leaves lowercase letters readable. Handle both ranges.
  • Shifting digits and symbols: a variant that rotates all of ASCII, not only letters, is not the Caesar cipher and breaks interoperability with other tools.
  • Assuming word breaks hide anything: keeping spaces tells an attacker word lengths, and short words such as a and the give the shift away.

Related terms

  • ROT13 — the Caesar cipher with a fixed shift of 13, which is its own inverse.
  • Vigenere Cipher — uses a keyword of shifts, so each letter gets its own Caesar shift.
  • ASCII — the character codes that most code implementations shift to do the arithmetic.
  • Autokey Cipher — a Vigenere-style cipher whose key stream extends with the message.

See also

  • Tool: Caesar Cipher — encrypt and decrypt with any shift from ROT1 to ROT25, with a brute-force view of all shifts.
  • Cheatsheet: Encryption Algorithms Cheatsheet — compares classical ciphers with modern ones.