Introduction
Polyalphabetic ciphers like Vigenere encrypt the same plaintext letter with different key letters, which flattens single-letter frequency analysis. But the underlying plaintext structure creates a detectable pattern: characters separated by a multiple of the key length are encrypted with the same key letter, causing autocorrelation to spike at those intervals. This tool computes autocorrelation for shifts 1 through 50, displays the results as a bar chart, and identifies the top 5 candidate key lengths. It also computes the Index of Coincidence for each candidate to confirm the result.
What this tool does
- Compute autocorrelation for shifts 1 through 50 by counting character matches between the text and a shifted version of itself
- Display autocorrelation values as a bar chart, with the top 5 peaks highlighted to indicate likely key lengths
- Compute the Index of Coincidence (IoC) for each of the top 5 candidate key lengths by splitting the ciphertext into columns
- Show the match rate (matches / comparisons) for each shift, which normalizes for the reduced overlap at larger shifts
- Strip non-letter characters and convert to uppercase before analysis, so spaces and punctuation do not affect the results
- Display reference values: English IoC is approximately 0.0667, random text IoC is approximately 0.0385
How this tool works
The tool cleans the input by stripping non-A-Z characters and converting to uppercase. For each shift d from 1 to 50, it compares each character at position i with the character at position i+d. The number of matches and the total number of comparisons are recorded. The match rate is matches divided by comparisons.
In a Vigenere-encrypted text, characters separated by the key length (or a multiple of it) are encrypted with the same key letter. Since the underlying plaintext has non-uniform letter frequencies, these positions will have a higher-than-random match rate. This causes autocorrelation to spike at shifts equal to the key length and its multiples.
The tool identifies the top 5 shifts by match rate and highlights them in the bar chart. For each candidate, it also computes the Index of Coincidence by splitting the ciphertext into d columns (where d is the candidate key length) and computing the average IoC across all columns.
The IoC for a column is computed as: sum over all letters of count(letter) * (count(letter) - 1) / (n * (n - 1)), where n is the column length. If the key length is correct, each column is a monoalphabetic substitution of English, and the IoC should be close to 0.0667. If the key length is wrong, the columns mix different key letters, and the IoC will be closer to 0.0385 (random).
How autocorrelation cipher detection works
Autocorrelation analysis for cipher period detection builds on the work of 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 insight was that the statistical properties of the underlying plaintext survive polyalphabetic encryption and can be detected by examining the text at regular intervals.
The autocorrelation method is closely related to the Kasiski examination, which was published by Friedrich Kasiski in 1863 in 'Die Geheimschriften und die Dechiffrir-Kunst'. Kasiski's method looks for repeated sequences in the ciphertext and factors the distances between them to find the key length. Autocorrelation is a more general approach that does not require finding repeated sequences: it simply measures the overall match rate at each shift.
Henry Beker and Fred Piper describe both methods in 'Cipher Systems: The Protection of Communications' (1982), along with detailed worked examples. They note that autocorrelation is particularly effective when the ciphertext is long enough for statistical patterns to emerge above noise.
The relationship between autocorrelation and IoC is straightforward: autocorrelation at shift d measures the match rate when comparing the text to itself shifted by d positions, while IoC measures the probability that two randomly chosen letters from a column are the same. Both metrics spike when the shift or column count equals the key length, because the underlying English letter frequency distribution is preserved within each column.
For Kasiski examination, see the Kasiski Examination tool. For IoC calculations, see the Index of Coincidence. For automated Vigenere cracking that uses IoC for key length detection, see the Vigenere Auto-Solver and Vigenere Cracker.
How to use this tool
- Paste your Vigenere ciphertext into the input field. Non-letter characters are stripped automatically
- Review the autocorrelation bar chart. Peaks (highlighted bars) at regular intervals indicate the likely key length
- Check the top 5 candidate key lengths table, which shows the autocorrelation value and IoC for each candidate
- Look for candidates where the IoC is close to 0.0667 (English). IoC values near 0.0385 indicate the key length is wrong
- If multiple candidates have high IoC, the smallest one is likely the true key length (larger ones are multiples)
- Take the detected key length to the Vigenere Auto-Solver or Vigenere Cracker to recover the actual key
Real-world examples
Detecting a key length of 6 in a Vigenere ciphertext
Paste a 500-character Vigenere ciphertext encrypted with a 6-letter key. The autocorrelation chart shows peaks at shifts 6, 12, 18, 24, and 30. The top candidate table shows key length 6 with IoC 0.0652 (close to English), while other lengths show IoC near 0.039. This confirms the key length is 6.
Distinguishing Vigenere from monoalphabetic ciphertext
Paste a monoalphabetic substitution ciphertext. Autocorrelation will be high at shift 1 and relatively flat at all other shifts, because every position uses the same substitution. There will be no periodic peaks. This tells you the cipher is not polyalphabetic, and you should use frequency analysis instead of key length detection.
Handling a short ciphertext with noisy autocorrelation
With only 100 characters, the autocorrelation chart will be noisy. Peaks may not be clearly visible. In this case, rely on the IoC column in the candidate table: the key length with the highest IoC is still the best guess, but the confidence is lower. For reliable results, use at least 300 characters.
Confirming key length with IoC
Autocorrelation suggests key length 8, but the IoC for 8 is 0.041 (close to random). Key length 4 has IoC 0.063 (close to English). This means the true key length is 4, and the autocorrelation peak at 8 is a harmonic (a multiple of the true key length). Always cross-check autocorrelation peaks with IoC values.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| Autocorrelation (this tool) | O(n * max_shift) character comparisons | Key length detection for polyalphabetic ciphers |
| Kasiski examination | Find repeated sequences, factor distances | Key length detection, works best with long ciphertexts |
| Index of Coincidence | O(n) per candidate key length | Confirming key length, comparing candidates |
| Friedman test | Single IoC computation, formula-based estimate | Quick key length estimate |
| Chi-squared per column | O(26 * n) per key position | Recovering key letters after length is known |
Limitations or considerations
Autocorrelation is most effective with ciphertexts of 300 or more characters. Shorter texts produce noisy autocorrelation values where peaks are difficult to distinguish from random fluctuations. For texts under 100 characters, the results may be unreliable.
The tool tests shifts 1 through 50. If the key is longer than 50 characters, the peak will not be detected. In practice, Vigenere keys longer than 20 characters are rare, but some CTF challenges use long keys to resist this analysis.
Autocorrelation detects the key length but does not recover the key itself. After identifying the key length, use the Vigenere Auto-Solver to recover the key letters via chi-squared analysis.
The tool assumes a simple Vigenere cipher. More complex polyalphabetic ciphers (autokey, running key) do not produce clean autocorrelation peaks because the key does not repeat at a fixed period.
Harmonics (multiples of the true key length) will also produce peaks. The IoC values help distinguish the true key length from harmonics: the true key length will have the highest IoC, while harmonics will have slightly lower IoC values.
Frequently asked questions
What is autocorrelation and how does it detect cipher key length?
Autocorrelation measures how often characters at position i match characters at position i+d, for each shift d. In a Vigenere cipher, characters separated by the key length are encrypted with the same key letter, so they share the underlying plaintext's frequency distribution. This causes the match rate to spike at shifts equal to the key length and its multiples.
What IoC value indicates the correct key length?
English text has an IoC of approximately 0.0667. Random text has an IoC of approximately 0.0385. When you split a Vigenere ciphertext into columns by the correct key length, each column is a monoalphabetic substitution of English, so the IoC should be close to 0.0667. Wrong key lengths produce IoC values closer to 0.0385.
How is autocorrelation different from the Kasiski examination?
Kasiski examination looks for repeated sequences of 3 or more characters in the ciphertext and factors the distances between repetitions. Autocorrelation is more general: it measures the overall match rate at every shift, without needing to find specific repeated sequences. Both methods detect the same underlying phenomenon (periodic key reuse) but autocorrelation works with noisier data.
Why does my autocorrelation chart show peaks at multiples of the key length?
If the key length is 5, peaks will appear at shifts 5, 10, 15, 20, and so on. These are harmonics. The true key length is the smallest shift with a significant peak. The IoC values help confirm this: the true key length will have the highest IoC, while harmonics will have slightly lower values.
Can autocorrelation detect the key length for autokey ciphers?
No. Autokey ciphers use the plaintext itself as part of the key, so the key does not repeat at a fixed period. Autocorrelation relies on periodic key reuse, which does not occur in autokey ciphers. For autokey analysis, different techniques are needed.
Conclusion
Autocorrelation is a reliable method for detecting the key length of polyalphabetic ciphers, especially when combined with Index of Coincidence confirmation. This tool computes autocorrelation for shifts 1 through 50 and highlights the top candidates. For the next step in Vigenere cryptanalysis, use the Kasiski Examination and Index of Coincidence tools for confirmation, then the Vigenere Auto-Solver or Vigenere Cracker to recover the key.