Introduction
The chi-squared statistic measures how closely a ciphertext's letter distribution matches the expected distribution for a given language. A low chi-squared value means the text is close to English (or whatever language you are testing against). A high value means it is far from natural language. This tool computes the chi-squared statistic for any text, shows a per-letter breakdown of observed vs expected frequencies, and includes a bar chart comparing the two distributions. It also supports custom frequency distributions for non-English languages.
What this tool does
- Compute the chi-squared statistic: sum of (observed - expected)^2 / expected for each letter A-Z, using standard English letter frequencies as the reference
- Strip non-letter characters automatically and convert to uppercase before counting, so punctuation and spaces do not affect the analysis
- Display a per-letter table showing observed count, observed percentage, expected percentage, and individual chi-squared contribution
- Render a bar chart comparing observed frequency (primary color) vs expected frequency (muted color) for each letter
- Support custom frequency distributions for languages other than English, entered in a simple `A:8.167, B:1.492, ...` format
- Show the standard English letter frequency reference table (ETAOIN SHRDLU ordering) for comparison
How this tool works
The tool strips all non-A-Z characters from the input and converts to uppercase. It counts the occurrences of each letter and divides by the total to get observed percentages. For each letter, it computes the expected count as `(expectedPct / 100) * totalLetters`, then calculates the chi-squared contribution as `(observed - expected)^2 / expected`. The total chi-squared value is the sum of all 26 contributions.
The standard English letter frequencies used as the reference are the well-known values from cryptographic literature: E at 12.702%, T at 9.056%, A at 8.167%, and so on, following the ETAOIN SHRDLU ordering. These values come from frequency analysis of large English text corpora.
For custom frequency distributions, the tool parses a comma-separated list of `letter:value` pairs. Missing letters are filled with 0. This lets you test against French, German, Spanish, or any other language by providing its letter frequency distribution.
The bar chart scales both observed and expected bars to the same maximum value, so you can visually compare the shape of the distribution. A ciphertext encrypted with a simple substitution cipher will show a chi-squared value similar to English (because the frequency distribution is preserved, just with different letters mapped), while random text will show a much higher value.
How chi-squared cryptanalysis works
The chi-squared test was adapted for cryptanalysis by William F. Friedman, who introduced the Index of Coincidence in his 1922 paper 'The Index of Coincidence and Its Applications in Cryptanalysis' (Riverbank Publication No. 22). Friedman's work at Riverbank Laboratories during World War I laid the foundation for modern statistical cryptanalysis.
The chi-squared statistic itself was developed by Karl Pearson in 1900. In cryptanalysis, it is used to compare the letter frequency distribution of a candidate decryption against the expected distribution of the target language. The formula is: chi-squared = sum over all letters of (observed_i - expected_i)^2 / expected_i.
For monoalphabetic substitution ciphers, the chi-squared value of the ciphertext (using English frequencies) will be high because the letters are permuted. But when you try each of the 26 possible Caesar shifts, the correct shift will produce the lowest chi-squared value, because the decrypted text's frequency distribution will match English most closely.
For polyalphabetic ciphers like Vigenere, chi-squared is applied per column (per key position). Each column is shifted through all 26 values, and the shift producing the lowest chi-squared for that column is the most likely key letter. This is the method used by the Vigenere Auto-Solver.
Helen Fouche Gaines documented these techniques in her 1939 book 'Cryptanalysis: A Study of Ciphers and Their Solution', which remains a standard reference for classical cryptanalysis. For related analysis tools, see the Frequency Analysis, Index of Coincidence, Kasiski Examination, and N-gram Frequency Analyzer.
How to use this tool
- Paste your ciphertext into the input field. Non-letter characters are automatically stripped
- The tool computes the chi-squared statistic against standard English letter frequencies and displays the total value
- Review the per-letter table to see which letters contribute most to the chi-squared value (large contributions indicate frequency mismatches)
- Check the bar chart to visually compare observed vs expected frequencies
- To test against a non-English language, enable 'Use custom frequency distribution' and enter the letter frequencies in `A:8.167, B:1.492, ...` format
- Use the chi-squared value to compare candidate decryptions: the lowest value indicates the most likely correct decryption
Real-world examples
Identifying the correct Caesar shift
Take a Caesar-encrypted ciphertext and try all 26 shifts. For each shift, paste the result into this tool. The shift producing the lowest chi-squared value is the correct decryption. For example, if shift 7 gives chi-squared 28.3 while all others give values above 100, shift 7 is almost certainly the correct key.
Distinguishing English from random text
Paste 500 characters of English text. The chi-squared value will typically be between 15 and 50. Now paste 500 random letters. The chi-squared value will be much higher (often above 200), because the uniform distribution of random text deviates strongly from the expected English distribution.
Analyzing a Vigenere ciphertext column
Extract one column from a Vigenere ciphertext (every Nth character, where N is the key length). Paste it into the tool. The chi-squared value will be high because the column is Caesar-shifted. Try shifting the column by each of 26 values and pasting each result. The lowest chi-squared identifies the key letter for that position.
Testing against French letter frequencies
Enable custom frequency distribution and enter French frequencies: `A:8.13, B:0.91, C:3.15, D:3.55, E:17.52, F:1.07, G:0.87, H:0.74, I:7.55, J:0.31, K:0.05, L:5.27, M:3.08, N:7.12, O:5.39, P:3.04, Q:1.36, R:6.63, S:8.13, T:7.11, U:5.92, V:1.38, W:0.04, X:0.38, Y:0.27, Z:0.14`. Paste French text and the chi-squared value will be low, confirming the text is French.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| Chi-squared (this tool) | Sum of (O-E)^2/E over 26 letters | Caesar key finding, Vigenere column analysis |
| Index of Coincidence | Sum of n_i(n_i-1) / N(N-1) | Key length detection for polyalphabetic ciphers |
| N-gram scoring | Sum of log-probabilities of bigrams/trigrams | Hill climbing and simulated annealing solvers |
| Kasiski examination | Find repeated sequences, factor distances | Vigenere key length estimation |
| Raw frequency analysis | Compare letter counts visually | Manual substitution cipher solving |
Limitations or considerations
The chi-squared statistic is most reliable with longer texts. For ciphertexts shorter than 100 characters, statistical fluctuations can produce misleading results. A short text may have a chi-squared value that does not clearly distinguish the correct decryption from incorrect ones.
The tool uses standard English letter frequencies as the default reference. These are averages across many text types. Specialized texts (medical, legal, technical) may have different frequency distributions, which can affect the chi-squared value.
The chi-squared test only considers single-letter frequencies. It does not account for bigram or trigram frequencies, which carry more information about whether text is natural language. For more accurate scoring, use N-gram Frequency Analyzer or the Hill Climbing Cipher Solver, which uses bigram log-probabilities.
The tool does not automatically try all 26 Caesar shifts. You must paste each candidate decryption manually. For automated Caesar cracking, use the Caesar Brute Force tool.
Frequently asked questions
What chi-squared value indicates English text?
Typical English text produces a chi-squared value between 15 and 50 when compared against the standard English frequency distribution, assuming a few hundred characters. Random text produces values above 200. The exact threshold depends on text length: shorter texts have higher variance.
How is chi-squared used in Vigenere cracking?
After determining the key length, the ciphertext is split into columns. Each column is a Caesar cipher. For each column, all 26 shifts are tried, and the shift producing the lowest chi-squared value is selected as the key letter for that position. This is the method used by the Vigenere Auto-Solver tool.
Can I use this tool for languages other than English?
Yes. Enable 'Use custom frequency distribution' and enter the letter frequencies for your target language in the format A:8.167, B:1.492, and so on. The tool will compute chi-squared against your custom distribution instead of the default English frequencies.
Why does my short ciphertext give unreliable results?
The chi-squared test relies on the law of large numbers. With only 20-30 characters, the observed frequencies have high variance, and the chi-squared value can fluctuate significantly. For reliable results, use at least 100 characters of ciphertext.
What is the relationship between chi-squared and the Index of Coincidence?
Both measure how far a text's letter distribution is from uniform, but they serve different purposes. The Index of Coincidence (IoC) measures self-similarity within the text and is used to detect key length. Chi-squared compares the text against a known reference distribution and is used to find the correct decryption key. Both were developed by Friedman and his colleagues at Riverbank Laboratories.
Conclusion
The chi-squared statistic is a fundamental tool in classical cryptanalysis, used to score candidate decryptions by how closely their letter frequency distribution matches the target language. This tool computes it for any text with a visual comparison of observed vs expected frequencies. For related cryptanalysis tools, see the Frequency Analysis, Index of Coincidence, Kasiski Examination, and N-gram Frequency Analyzer. For automated Vigenere cracking using chi-squared per column, use the Vigenere Auto-Solver.