Skip to content
All tools
Classical cryptoRuns locallyNo account

Caesar cipher decoder with automatic shift detection

Break a Caesar shift without guessing. ctfpal scores all 26 rotations by chi-squared letter frequency and puts the English one first.

Open in ctfpal

A Caesar cipher shifts every letter by a fixed amount. There are only 25 useful keys, so it is trivially breakable by trying all of them - the only real question is which of the 25 candidates is the right one, and that is a question you should never answer by reading them yourself.

Why chi-squared beats eyeballing

English letter frequencies are stable: E around 12.7%, T around 9.1%, Z under 0.1%. Score each of the 26 rotations against those expected frequencies with a chi-squared statistic and the correct shift usually wins by a wide margin. The advantage over scanning by eye grows with the length of the text and, crucially, it does not degrade when the plaintext is unusual - a shifted flag full of digits and underscores still scores correctly on its letters alone.

EXPECTED = {'a': 0.0817, 'b': 0.0150, ...}  # English unigram frequencies

def chi_squared(text):
    letters = [c for c in text.lower() if c.isalpha()]
    n = len(letters)
    score = 0.0
    for ch, expected_freq in EXPECTED.items():
        expected = expected_freq * n
        observed = letters.count(ch)
        score += (observed - expected) ** 2 / expected
    return score          # lower is more English-like

best = min(range(26), key=lambda k: chi_squared(rotate(ct, k)))
The scoring ctfpal runs for each rotation

When the shift is not 13

Short ciphertexts - under about 30 letters - are where statistics get unreliable, because a single unusual word skews the distribution. ctfpal still ranks all 26 and shows them, so a short text becomes a list to skim rather than a set of 26 separate round trips to a website.

Worked example

A shifted flag

Input

cvpbPGS{arire_gehfg_n_pnrfne}

Result

picoCTF{never_trust_a_caesar}

Shift 13. Note that digits and punctuation pass through untouched, which is why the braces still look like braces.

Load this example in the workspace

Common questions

What if the alphabet includes digits?
Some challenges rotate over a 36- or 62-character alphabet so digits shift too. If a plain 26-letter rotation leaves the letters right but the digits wrong, that is what happened; the ctfpal cipher tab exposes the alphabet so you can widen it.

Part of a module

2. Classical ciphers and frequency analysis

Break Caesar, Vigenere, and arbitrary substitution using letter statistics - and learn why statistics beat guessing.

Practise on real challenges

Go deeper

Related tools