Leandro Junes

Frequency Analysis

Analyze letter patterns, n-grams, and coincidence statistics.

Explore statistical patterns in plaintext and ciphertext.

Text to analyze

0 letters analyzed

The standard analysis uses the letters A–Z, in both views. Case is ignored; spaces, digits, punctuation and accented or non-Latin letters are left out, so “The end!” is counted as THEEND.

Example
Export

Frequency distribution

—

Enter text to see frequencies.

Index of coincidence
Open the Coincidence view
Shannon entropy
—

Advanced analysis

Chi-square comparison with English

χ² = —

χ² = Σ (Oi − Ei)² / Ei, where Oi is the count of a letter and Ei = N × its typical English frequency. A lower value means the letter counts are closer to the reference table, not that the text is English. The value grows with the length of the text, so compare it only between texts of similar length.

N-gram boundaries

Unchecked, spaces and punctuation are removed first, so THE END also gives the digram EE. Checked, an n-gram must lie inside one unbroken run of letters, and the percentages are taken of the windows that do. The letter counts are the same either way.

Understanding the results

What is counted

Letter frequency measures how often each letter appears. An n-gram frequency counts overlapping runs of n neighbouring letters (digrams, trigrams, and so on up to 6): the window moves one letter at a time. Each percentage is a count divided by the number of windows of that size, and a text of N letters has max(0, N − n + 1) of them.

ABABA  →  letters A = 3, B = 2  ·  digrams AB = 2, BA = 2  ·  trigrams ABA = 2, BAB = 1  ·  4-grams ABAB = 1, BABA = 1

What n-grams add

AABB and ABAB have the same letter counts, and therefore the same index of coincidence and the same entropy. Their digrams differ: AABB has AA, AB, BB and ABAB has AB, BA, AB. Single-letter statistics cannot tell the order of the letters; n-grams can, which is why a transposition changes them while leaving every letter count alone.

Shannon entropy

H = −Σ pi log2(pi)

Entropy measures the uncertainty in the letter distribution, with pi = ni/N; it is always taken over single letters, whichever n-gram is charted. A text made of a single letter has entropy 0. Twenty-six equally frequent letters give the maximum, log2(26) ≈ 4.70 bits per letter. It does not measure cryptographic security: a Caesar ciphertext has exactly the entropy of its plaintext.

The English reference

“Compare with English” and the chi-square statistic use one table of 26 letter frequencies: the conventional table reproduced in most cryptography texts, with the values given in Lewand, Cryptological Mathematics (2000), rounded to two decimals (E 12.70%, T 9.06%, A 8.17%, …). It is a typical profile, not an exact law: frequencies vary with the corpus, the genre, the length of the sample and the language. N-grams are shown without a reference.

Limitations

Frequency analysis is statistical evidence, not a guaranteed decryption method. It is weakened by:

  • very short messages, unusual vocabulary and names;
  • text that is not English;
  • letter distributions that were manipulated on purpose;
  • polyalphabetic encryption and block transformations.

What each cipher does to the statistics

CipherLetter countsOverall IC
CaesarShifted to other letters.Preserved.
AffinePermuted.Preserved.
SubstitutionRelabelled; the counts themselves are kept.Preserved.
Rail FenceKept exactly; the order changes.Preserved.
Columnar TranspositionKept exactly; the order changes.Preserved.
VigenèreSeveral substitutions take turns, which can flatten them.May decrease.
PlayfairPairs are replaced, so they can change.Can change.
HillBlocks are mixed, so they can change.Can change.

“Preserved” assumes that only letters are encrypted and none are added: padding a transposition with extra letters changes the counts. A transposition keeps the IC and the entropy but generally changes the n-grams. Encryption does not have to lower the IC: the first five ciphers leave it exactly as it was.

The IC analysis suggests possible key lengths; the Vigenère Cipher calculator can attempt complete decryption.

Caesar · Affine · Substitution · Playfair · Hill · Rail Fence · Columnar Transposition