Monoalphabetic substitution cipher solver
Break an arbitrary substitution cipher automatically by hill-climbing on quadgram statistics - no key, no guessing.
Open in ctfpalA general substitution cipher maps each letter to an arbitrary other letter. The keyspace is 26 factorial - about 4 x 10^26 - so brute force is out. It is nonetheless one of the easiest ciphers to break, because the mapping is consistent and English is highly redundant.
Hill climbing on quadgrams
Score a candidate decryption by the log-probability of its four-letter sequences against English quadgram frequencies. Start from the frequency-matched key, swap two letters at random, keep the swap if the score improves. Repeat a few thousand times, restart from a new random key a few times to escape local maxima, and the correct key emerges. Quadgrams work far better than single-letter frequencies because they capture structure - TION and THAT are enormously more likely than any permutation of the same letters.
- Needs roughly 100+ characters. Below that, several keys score similarly and the climb lands wrong.
- Word boundaries help enormously - if spaces are preserved, single-letter words are
AorI, and three-letter words are heavilyTHE. - If the climb converges to something almost readable with two letters swapped, that is a local maximum; a restart usually fixes 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
- Chi-squared, index of coincidence, and why classical ciphers fallCaesar, Vigenere, and substitution ciphers do not need guesswork - they need two statistics. How chi-squared scores a candidate plaintext, how the index of coincidence recovers a key length, and how to combine them into an attack that runs in milliseconds.
Related tools
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.
Affine cipher solver
Brute-force every valid affine key pair and rank the 312 candidate decryptions by English-likeness.
Vigenere cipher solver with automatic key recovery
Decrypt Vigenere with a known key, or recover the key from ciphertext alone using index-of-coincidence period detection and per-column chi-squared.
Playfair cipher decoder
Decrypt Playfair with a known keyword, using the 5x5 digraph square. Recognisable by its even length and total absence of doubled letters.