DEV Community

Picute
Picute

Posted on • Originally published at textmachine.org

How to Crack a Cipher Without the Key

You've figured out which cipher you're staring at — say a monoalphabetic cryptogram, or a Vigenère — but you don't have the key. No keyword, no shift, no crib. Manually, this is where people grind for hours. Automatically, a good solver recovers it in about a second. Here's how that actually works, so the tool isn't a black box.

The whole game rests on one idea: you don't search for the key, you search for English. A wrong key produces gibberish; the right key produces text that looks like a real language. So if you can score how English-like a candidate decryption is, breaking the cipher becomes an optimization problem — find the key that maximizes the score. Everything below is a variation on that theme.

The scoring function is the secret, and single letters aren't enough

The naive score is letter frequency: real English is ~12.7% E, ~9% T, and so on, so reward decryptions whose letter distribution matches. This is too weak. A decryption that's 95% correct can score as well as or better than the true plaintext on single-letter counts alone, because shuffling a few letters barely moves the histogram. The search then happily settles on a near-miss garble and calls it done.

The fix is n-grams — scoring sequences of letters, not single ones. English is far richer in some letter-pairs and triples (TH, HE, IN, ER; THE, AND, ING) than in others (QZ, JX, VKZ). Any decoding error injects rare, low-probability pairs and triples, which a bigram or trigram score punishes hard. So the fitness function is the sum of log-probabilities of every trigram in the candidate plaintext, using a frequency table built from a large English corpus. Truth scores strictly higher than any near-miss, which is exactly what you need to climb toward.

A useful diagnostic if you ever build one of these: if your solver lands on garbage, check whether score(true plaintext) > score(found). If truth scores higher, your fitness function is fine and your search is stuck — don't tune the scorer, fix the optimizer (next section). If truth scores lower, the scorer itself is too weak (you're probably on single letters — go to trigrams).

Cracking a monoalphabetic substitution (cryptogram)

A simple substitution maps each letter to another, fixed for the whole message. There are 26! ≈ 4×10²⁶ possible alphabets — brute force is hopeless. But the scoring trick makes it tractable:

  1. Seed with frequency analysis. Count letters in the ciphertext; map the most common cipher letter to E, the next to T, and so on. This is usually 30–60% correct — a decent starting point, not the answer. (You can do this step by hand with a frequency analysis tool.)
  2. Improve by local search. Swap two letters in the key, re-score, keep the swap if the score went up. Repeat. This is hill-climbing — and on its own it gets stuck in local optima: a key that's better than all its neighbors but still wrong.
  3. Escape local optima with simulated annealing. The fix is to sometimes accept a worse swap, with a probability that starts high and "cools" toward zero. Early on the search roams freely and jumps out of bad valleys; late on it behaves like pure hill-climbing and locks onto the peak. Run a few random restarts and keep the best result. This reliably recovers normal English prose.

That's precisely what the substitution cipher solver does — frequency-seeded, then simulated annealing on trigram fitness — and it recovers both the message and the full cipher alphabet with no key or crib. Paste a cryptogram and it solves in well under a second.

Cracking a Vigenère without the keyword

Vigenère uses a repeating keyword, so it's polyalphabetic — letter frequencies are smeared flat and the substitution trick above doesn't directly apply. You break it in two stages:

  1. Find the key length. Two classic methods: Kasiski examination looks for repeated sequences in the ciphertext and measures the distances between them — those distances tend to be multiples of the key length. The Index of Coincidence approach tries each candidate length and watches for the one where the slices look like natural (peaky) English.
  2. Solve each column independently. Once you know the key length L, every L-th letter was enciphered with the same shift — so the ciphertext splits into L columns, and each column is just a Caesar cipher. Solve each one by frequency / chi-squared against English (only 26 shifts per column), and you've recovered the keyword letter by letter.

The robust way to drive this — and what the Vigenère solver does — is to solve at every plausible key length, then rank the resulting decryptions by English fitness and present the best, rather than committing to a single length guessed from a threshold (which fails on repetitive plaintext). A monoalphabetic message naturally collapses to a one-letter key, so the same tool degrades gracefully.

When automatic solving struggles

Auto-solvers are statistical, so they need enough text to be confident:

  • Too short. Under ~40–50 letters there often isn't enough signal; the trigram statistics are noisy. Get more ciphertext if you can.
  • Not English. The fitness table is language-specific. A French or German plaintext needs a French/German n-gram model.
  • Homophones, nulls, or padding. Homophonic substitution (several cipher symbols per plaintext letter) and inserted null characters break the one-to-one assumption — identify and strip those first.
  • It's not actually a simple substitution/Vigenère. If the solver can't find anything English-like at any setting, re-check the cipher type — start again with the cipher identifier.

The two-minute version

  1. Identify the cipher (character set, IoC, structure) — or confirm it's a cryptogram / Vigenère.
  2. Paste it into the matching auto-solversubstitution for cryptograms, Vigenère for keyword ciphers.
  3. Read off the plaintext and the recovered key. If it stalls, check the message length and language, and re-confirm the cipher type.

No key, no problem — the statistics of English do the work for you. All of these run entirely in your browser; nothing you paste is uploaded.

Top comments (0)