Classical cryptography

Caesar cipher laboratory

Rotate the alphabet wheel, transform text instantly, or recover an unknown key with frequency analysis.

Plaintext

A → D

Ciphertext
RAW Tools cipher

Caesar Cipher Encoder and Decoder

The Caesar cipher replaces every letter with another letter a fixed number of positions away in the alphabet. It is one of the simplest substitution ciphers: the key is a number from 0 to 25, and the same key applies to every letter.

Use Encode to shift plaintext forward, Decode to reverse a known shift, or Crack to rank every possible shift by how closely its letter distribution resembles English. The alphabet wheel shows the substitution directly. Drag it, use the arrow keys, or enter the shift numerically.

How the Shift Works

Number the letters from 0 through 25, with \(A=0\), \(B=1\), and \(Z=25\). For a plaintext letter \(x\) and key \(k\), encryption is addition modulo 26:

\[ E_k(x) = (x + k) \bmod 26 \]

Modulo 26 wraps the result around the end of the alphabet. With \(k=3\), the mapping begins \(A \mapsto D\), \(B \mapsto E\), and ends \(X \mapsto A\), \(Y \mapsto B\), \(Z \mapsto C\). Therefore:

HELLOKHOOR

Decryption subtracts the same key:

\[ D_k(y) = (y - k) \bmod 26 \]

The tool preserves letter case and leaves spaces, punctuation, digits, and other characters unchanged. This makes the result readable as text while applying the substitution only to the English alphabet.

ROT13

ROT13 is the Caesar cipher with \(k=13\). Since two shifts by 13 make one complete rotation of the 26-letter alphabet, the same operation both encodes and decodes:

\[ E_{13}(E_{13}(x)) = (x + 26) \bmod 26 = x \]

ROT13 is useful for hiding spoilers or puzzle answers from immediate view, but it provides no cryptographic security.

Recovering an Unknown Shift

There are only 26 possible keys, including the unshifted alphabet. A brute-force attack can generate every candidate immediately and let a reader choose the meaningful plaintext. Frequency analysis automates that choice.

A Caesar shift changes which symbols represent letters, but it does not change how often the underlying letters occur. If a ciphertext contains enough English text, each candidate decryption can be compared with the expected English letter frequencies. The tool uses the chi-squared statistic:

\[ \chi^2 = \sum_{i=0}^{25} \frac{(O_i-E_i)^2}{E_i} \]

Here, \(O_i\) is the observed count for letter \(i\) in a candidate plaintext and \(E_i\) is its expected count. A lower score indicates a closer match. Crack mode tests all shifts, selects the lowest score, and keeps the strongest alternatives available because statistical evidence is not certainty.

Typical English letter frequency distribution

Why Short Messages Are Ambiguous

Frequency analysis depends on a representative sample. A long sentence usually contains enough letters to reveal a useful distribution; a short word may fit several shifts equally well. Names, abbreviations, source code, or text in another language can also defeat an English frequency model. In those cases, inspect the alternative shifts or try all 26 candidates.

A Minimal Implementation

The arithmetic maps each ASCII letter to the range 0 through 25, adds the normalized key, and maps the result back to its original case:

function caesar(text, shift) {
  const key = ((shift % 26) + 26) % 26;

  return text.replace(/[A-Za-z]/g, letter => {
    const code = letter.charCodeAt(0);
    const base = code >= 97 ? 97 : 65;
    return String.fromCharCode((code - base + key) % 26 + base);
  });
}

function decodeCaesar(text, shift) {
  return caesar(text, -shift);
}

This construction also handles negative and oversized keys correctly. It is suitable for teaching modular arithmetic, testing text transformations, and solving classical cipher puzzles. It must not be used to protect passwords, private messages, or any other sensitive data: the key space is tiny and the cipher has no resistance to modern cryptanalysis.