Skip to content
Back to blog

How to Identify an Unknown Cipher From Its Statistics

4 min read
How to Identify an Unknown Cipher From Its Statistics

There is a specific kind of stuck: you have a block of text, you know it means something, and you have no idea what was done to it. Trying decryption methods at random is a bad strategy — there are dozens of classical ciphers and most of them will happily produce garbage for any key you feed them.

The way out is that a cipher hides meaning, not structure. Almost every classical cipher leaves a fingerprint in the statistics of its own output, and four cheap tests are usually enough to name the family before you spend a minute on keys.

Test 1: Look at the alphabet first

Before any arithmetic, ask what characters are actually present. This single question resolves a surprising share of real cases:

What you seeWhat it usually is
Only dots, dashes, slashesMorse code
Exactly two distinct symbols, length divisible by 5Baconian cipher
Only A, D, F, G, X (sometimes V)ADFGX / ADFGVX
Only digits 1-5, in pairsPolybius square, Nihilist or tap code
Digits with separators, none above 26A1Z26 letter numbering
Three dot-separated Base64URL chunksJWT — not a cipher at all
A-Z, a-z, 0-9, +/ with = paddingBase64 — an encoding, not encryption
Starts with H4sIBase64-wrapped Gzip
32, 40, 64 or 128 hex charactersMD5, SHA-1, SHA-256, SHA-512 hash

That last row matters more than it looks. A hash is not encryption and cannot be reversed at all — recognising one saves you from an unwinnable fight.

Test 2: Index of coincidence

When the text is plain letters, the first number to compute is the index of coincidence (IC): the probability that two letters drawn at random from the text are the same letter.

For a text of length N with counts n_i per letter:

IC = Σ n_i(n_i − 1) / (N(N − 1))

Reference values:

  • English plain text: ≈ 0.0667
  • Russian plain text: ≈ 0.0553
  • Uniformly random letters: ≈ 0.0385

The key insight is what doesn't change it. Caesar, Atbash, affine and any keyed simple substitution merely rename letters — E becomes H, T becomes W — so the distribution stays exactly as lopsided as the original. Their IC is the plaintext IC.

Vigenère, Beaufort, Gronsfeld and friends spread each plaintext letter across several different alphabets depending on its position, which evens out the distribution and drags the IC toward the random baseline. So:

  • IC near 0.065 → one alphabet: Caesar, Atbash, substitution, or a transposition (see the next test).
  • IC in 0.038-0.048 → many alphabets: Vigenère family, or a fractionating cipher such as Bifid or ADFGX.
  • IC at 0.038 with mixed case and digits → probably not a classical cipher at all: Base64, compressed data, or modern encryption.

Test 3: Chi-squared against letter frequencies

The IC tells you how uneven the distribution is, not which letters are frequent. That second question separates two things the IC cannot: substitution versus transposition.

Compare the observed letter counts to the expected frequencies of the language with a chi-squared statistic, normalised by text length:

χ²/N = Σ (observed_i − expected_i)² / (expected_i · N)
  • χ²/N ≈ 0.5 — the frequencies are still those of the language. Nothing was replaced, only reordered: rail fence, columnar transposition, scytale.
  • χ²/N of 5-20 — letters were replaced. Substitution of some kind.

This is the test that catches the confusing case: a transposition ciphertext has a perfectly normal-looking letter histogram and a perfectly normal IC, yet contains no words at all. Without the chi-squared step it is easy to misfile it as a keyed substitution and waste an afternoon.

Test 4: Kasiski repeats and periodic IC

Once you suspect a polyalphabetic cipher, the useful question becomes how long is the key.

Kasiski examination (Friedrich Kasiski, 1863; Charles Babbage got there privately around 1854) exploits a coincidence: when the same plaintext fragment happens to line up with the same section of the key, it encrypts to the same ciphertext fragment. So the distance between repeated fragments is usually a multiple of the key length. Collect the distances between all repeated 3- to 5-letter sequences, factor them, and count which factors appear most often.

Then confirm with periodic IC. If the key length is 5, then every 5th letter was encrypted with the same alphabet — so the subsequence of positions 1, 6, 11, 16… is effectively a Caesar cipher and should have a plaintext-like IC. Slice the text by each candidate period and measure. The period where the IC jumps back toward 0.065 is your key length.

One caveat that trips people up: short slices inflate the IC by chance. A period of 12 on a 100-letter text leaves 8 letters per slice, and 8 letters can look "plaintext-like" purely at random. Only trust periods that leave at least 15-20 letters per slice.

Putting the tests in order

The four tests form a decision procedure, and the order matters — each one is cheaper and more decisive than the next:

  1. Charset. Resolves Morse, Baconian, ADFGX, Polybius, A1Z26, JWT, Base64, hex and hashes outright.
  2. Try the keyless ciphers. Caesar has 25 keys, Atbash has none, affine has 312. Just run them: a readable decryption beats any statistic and ends the investigation immediately.
  3. IC. Splits one-alphabet from many-alphabet.
  4. Chi-squared. Within one-alphabet, splits substitution from transposition.
  5. Kasiski + periodic IC. Within many-alphabet, estimates the key length.

Everything after step 2 is evidence, not proof. A confidence figure attached to "Vigenère, 82%" should be read as "the statistics point here", never as certainty — short samples, mixed languages and hybrid ciphers all bend the numbers.

What identification does not give you

Identifying the cipher is not breaking it. The split is clean:

  • Keyless families — Caesar, ROT-N, Atbash, affine, rail fence, keyboard shift, single-byte XOR — collapse the moment they are identified, because the whole key space fits in a brute-force pass.
  • Keyed families — Vigenère, Playfair, Bifid, ADFGX, columnar — still need the key, a crib, or a dedicated attack such as key recovery from per-column frequency analysis.
  • Modern encryption — AES-GCM, ChaCha20 — identification only tells you to stop. High entropy with no structure is the correct, final answer, and no amount of statistics will move it.

The most valuable outcome of a cipher identifier is often negative: knowing that a string is a hash, a random key or an AES payload saves far more time than any successful decryption of a toy cipher.

Frequently asked questions

What is the index of coincidence used for?

It measures how uneven a text's letter distribution is — formally, the probability that two randomly chosen letters are identical. English plain text sits near 0.0667 and Russian near 0.0553; uniformly random text sits at 0.0385. Monoalphabetic ciphers such as Caesar, Atbash or keyed substitution leave the value unchanged because they only rename letters, while polyalphabetic ciphers such as Vigenere flatten it toward random. It is the fastest single test for telling those two families apart.

How do you spot a transposition cipher?

Compare the letter frequencies to the language's expected distribution with a chi-squared test. Transposition reorders letters without replacing any of them, so the frequency profile stays perfectly normal for the language — a low chi-squared — while the text still contains no readable words. Substitution ciphers change which letters appear and score far worse on the same test. Normal frequencies plus unreadable text is the signature of rail fence, columnar or scytale.

What does the Kasiski examination reveal?

It reveals the probable key length of a polyalphabetic cipher. Repeated fragments in the ciphertext usually appear when the same plaintext meets the same section of the key, so the distances between repeats tend to be multiples of the key length. Factoring those distances and counting the most common factors gives a shortlist of candidate lengths, which you then confirm by slicing the text by that period and checking whether the index of coincidence returns to a plaintext-like value.

Can statistics identify a cipher from a short sample?

Not reliably. The index of coincidence, chi-squared and Kasiski all need enough letters to be stable — below roughly 60 characters the numbers fluctuate enough to point at the wrong family, and below 20 they are meaningless. Charset-based signals still work on short samples: dots and dashes, two-symbol blocks of five, an A-D-F-G-X alphabet or three dot-separated Base64URL segments identify themselves regardless of length.

Does identifying a cipher mean it can be broken?

No. Identification narrows what to try; it does not supply a key. Keyless families such as Caesar, affine, rail fence and single-byte XOR fall to brute force immediately once identified. Keyed ciphers such as Vigenere, Playfair or ADFGX still need the key, a crib or a dedicated attack — and identifying AES-GCM ciphertext tells you only that you should stop trying.

Author

Fullstack Engineer

In commercial development since 2011 — over 15 years. Author of two SaaS products, TeleGo.io and LingvoHabit.com. The articles here come out of real client and product work.