Caesar Cipher / ROT13Loading saved progress…

Caesar Cipher / ROT13

A Caesar cipher shifts every letter of a message a fixed number of places along the alphabet — shift by 3 and a becomes d, b becomes e — wrapping past z back to a, keeping each letter's case, and leaving spaces, digits, and punctuation untouched. It is one of the oldest known ciphers, named for Julius Caesar, and a common warm-up in coding interviews (background on Wikipedia). You will implement three functions on one object: encode to shift a message, decode to shift it back, and rot13, the shift-by-13 case that is its own inverse.

Signature

caesarCipher.encode(text, shift)  // shift each letter forward by `shift`, wrapping z -> a
caesarCipher.decode(text, shift)  // undo an encode that used the same shift
caesarCipher.rot13(text)          // encode with shift 13 — its own inverse

All three live on one object: caesarCipher = { encode, decode, rot13 }. shift is any integer.

Examples

caesarCipher.encode('abc', 3);            // 'def'
caesarCipher.encode('xyz', 3);            // 'abc'   — wraps past z
caesarCipher.encode('Hello, World!', 3);  // 'Khoor, Zruog!'
caesarCipher.encode('abc', 29);           // 'def'   — 29 mod 26 is 3
caesarCipher.encode('def', -3);           // 'abc'   — negative shift
caesarCipher.decode('Khoor, Zruog!', 3);           // 'Hello, World!'
caesarCipher.rot13('abc');                          // 'nop'
caesarCipher.rot13(caesarCipher.rot13('Secret!'));  // 'Secret!'  — self-inverse

Notes

  • Preserve caseA-Z stay uppercase and a-z stay lowercase. Anchor each letter to the start of its own case.
  • Wrap around — after z comes a again; the shift is taken modulo 26.
  • Pass non-letters through — digits, spaces, and punctuation are copied unchanged, never shifted.
  • Any integer shiftshift may be 0, larger than 26, or negative; normalize it with modulo 26. Watch that a negative shift % 26 is still negative in JavaScript.
  • decode undoes encodedecode(encode(text, s), s) returns the original text for every shift s.
  • Not encryption — the Caesar cipher is a toy anyone can undo; do not use it to protect real secrets.

FAQ

How does encode handle a shift larger than 26 or a negative shift?
It folds any integer shift into 0 to 25 with modulo 26. A shift of 29 acts like 3, and 26 acts like 0 (no change). Negatives need care because a plain shift % 26 stays negative in JavaScript, so the code adds 26 and takes the modulo again: ((shift % 26) + 26) % 26.
Why is ROT13 its own inverse?
ROT13 shifts by 13, and 13 is exactly half of 26. Shifting a letter by 13 twice moves it 26 places, one full loop around the alphabet, back to where it started. So the same function both scrambles and unscrambles, which is why forums use it to hide spoilers.
Is the Caesar cipher secure?
No. There are only 25 usable shifts, so anyone can break it by trying them all, and letter-frequency analysis recovers the shift instantly on longer text. Treat Caesar and ROT13 as obfuscation or a teaching exercise, never as real encryption.
Loading editor…