Caesar Cipher — Why ROT13 Passwords Leak in Logs
ROT13 passwords appeared plaintext in production logs.
- Caesar cipher shifts each letter by a fixed number (k) modulo 26
- Only 25 meaningful keys — brute force takes seconds
- Frequency analysis breaks it instantly: map most common ciphertext letter to 'E'
- Fails on confusion (linear key relationship) and diffusion (one-to-one mapping)
- ROT13 is Caesar(13) — self-inverse, zero security, pure obfuscation
The Caesar cipher shifts every letter by a fixed number. A shift of 3 turns 'HELLO' into 'KHOOR'. Julius Caesar used it to communicate with his generals. It is completely broken by modern standards — but understanding exactly why it breaks teaches you the two fundamental properties every secure cipher must have: confusion and diffusion.
The Caesar cipher is the entry point to cryptography for a reason — it is simple enough to understand completely, and broken in enough ways to illustrate every major weakness that centuries of cryptanalysis have identified. ROT13, the internet's favourite 'obfuscation', is a Caesar cipher with shift 13. The Vigenère cipher, which stumped cryptanalysts for 300 years, is just multiple Caesar ciphers stacked. And frequency analysis — the attack that breaks Caesar — is the same technique that cracked the Enigma machine.
Start with Caesar. Understand why it fails. Every modern cipher is essentially a series of answers to the questions Caesar's failure raises.
Implementation
Caesar cipher with shift k: encrypt by adding k mod 26 to each letter's position, decrypt by subtracting k. Non-letters pass through unchanged. The modulo operation ensures wrap-around (e.g., 'Z' shifted by 1 becomes 'A'). Here's a Python implementation that handles both upper and lower case.
Breaking Caesar — Brute Force and Frequency Analysis
Caesar has only 26 possible keys. Brute force tries all 26. But frequency analysis is more powerful: in English, 'E' appears ~13% of the time, 'T' ~9%, 'A' ~8%. The most frequent letter in the ciphertext is almost certainly 'E'. Find it, compute the shift, decrypt. This technique works for any monoalphabetic substitution cipher, not just Caesar.
Why Caesar Fails — The Two Properties of Secure Ciphers
Caesar fails on both criteria that define a secure cipher:
Confusion (key relationship to ciphertext should be complex): Caesar's relationship between key and ciphertext is linear and trivially invertible. Knowing one plaintext-ciphertext pair reveals the key instantly.
Diffusion (each plaintext bit should affect many ciphertext bits): Caesar maps each letter independently. 'E' always maps to the same letter. No letter affects any other. Letter frequencies are perfectly preserved.
Modern ciphers (AES, ChaCha20) address both: complex non-linear key schedules for confusion, and mixing operations that propagate each bit throughout the entire block for diffusion. Caesar has neither.
Historical Context and the Road to Vigenère
Julius Caesar used shift 3. His nephew Augustus used shift 1. Suetonius documented this in 'The Twelve Caesars' (121 AD) — making the Caesar cipher the oldest documented encryption method.
The cipher survived in various forms for 1500 years because letter frequency analysis was not documented until Al-Kindi described it around 850 AD. Once frequency analysis was understood, the Caesar cipher was immediately broken.
The response was the Vigenère cipher (1553) — use a different shift for each letter position, determined by a keyword. This defeated frequency analysis for 300 years until Charles Babbage (1854) and Friedrich Kasiski (1863) independently discovered how to detect the keyword length. The pattern of attack-and-response continues to define cryptographic history.
Common Mistakes in Implementing Caesar Cipher
Even a simple cipher like Caesar has pitfalls. Most common: forgetting to preserve case, mishandling non-alphabetic characters, using wrong modulo direction, and assuming encryption and decryption are symmetric when they aren't (e.g., using same function with positive shift for both). Also, many beginners treat ROT13 as a security measure — it's not.
- diff = ord(letter)
- ord('A'); result = chr((diff + shift) % 26 + ord('A')) — but forgets to handle lowercase bases.
- Using
shiftfor both encrypt and decrypt without negation. - Not filtering non-letters before frequency analysis — skews the count.
How a Startup Exposed Passwords Using ROT13
- Obfuscation is not encryption — ROT13, Base64, and simple XOR provide zero security.
- Any transformation of sensitive data must be reviewed by someone who understands the difference between encoding and encryption.
- When security is not a team competency, hire an external auditor before shipping.
Key takeaways
Common mistakes to avoid
3 patternsUsing ROT13 (or Caesar) for actual security
Forgetting to preserve case and non-alphabetic characters in implementation
ch.isalpha(); if true, apply shift to the correct case base (ord('A') for uppercase, ord('a') for lowercase). If false, append the character unchanged.Assuming encryption and decryption use the same shift direction
Interview Questions on This Topic
What are the two fundamental properties (confusion and diffusion) that a secure cipher must have, and why does Caesar fail both?
Frequently Asked Questions
That's Cryptography. Mark it forged?
3 min read · try the examples if you haven't