How to Break a Vigenere Cipher: Kasiski Examination Worked Example
Kasiski examination worked example on a 405-letter Vigenere ciphertext, plus success rates for Kasiski and the index of coincidence over 4,500 trials by text and key length.
On this page ⌄
A Vigenere cipher is several Caesar ciphers taken in turn, one per letter of the keyword. If you know the key length k, the ciphertext splits into k columns and each column is an ordinary Caesar shift you can solve by letter frequency. So breaking it reduces to one question: what is k? Kasiski examination and the index of coincidence both answer it. This post breaks a 405-letter ciphertext with both, then measures how often each method finds the key length on 4,500 generated ciphertexts, so you can see how much text you need. Every number below came from running the code in the script section.
To encrypt or decrypt your own text, use the Vigenere Cipher tool; it also shows the key stream alignment.
What Kasiski examination is
Friedrich Kasiski published the method in 1863 in Die Geheimschriften und die Dechiffrir-Kunst; Wikipedia's article adds that Charles Babbage seems to have discovered it independently as early as 1846. The idea: if the same plaintext string sits at two places whose distance is a multiple of the key length, the key lines up the same way both times and the same ciphertext string appears twice. The distances between repeats are therefore likely multiples of the key length, and their greatest common divisor can reveal it. Wikipedia notes the repeated strings should be at least three characters long and that some repeats are coincidence, so some distances mislead. Both caveats show up in the example below.
The ciphertext
Plaintext: the first 405 letters of Pride and Prejudice, taken from Project Gutenberg ebook 1342 (public domain), keeping A to Z only. The key is six letters, hidden until step 3. Ciphertext:
KBXZEKTCIOYEKDTYWRNTNHGBPWLSIUIMSALRVIHPRXNMBHRZPXDZWVUAXVRFHIVVSUHWGAYEGUJZXSGQCDEEVWUHAZHMWVAVXMGSMKVTTRRFYVIOIWGMAPRXUWGCMVYADMWLEPPTEEOINIIFPPXZJZTAILRKGZXUKRPMXNLSQCGOSFFBWPWKTCIOMJUWLLPCHQMLHZPBWLQZPLHVJKJMHBVIQCCKMEINPTMCKMHALRVPTPWTQVHPHVTMSHWKJMGPKYVNJSTIQXTYXPQNHVQVQVTVVFVPTYSWVPTPVUCCVOXVTABFHVCZBYFVPVTAWRKLWPWCCLNASYKUDUIUCGWHZVAWJOIRTLIOEKPMIOIIHQTSHGCZZPWCGBPAPRUBBYFVPVTAVVRTXLHKJIIOIYCLC
The index of coincidence (IC) is the chance that two letters drawn from the text without replacement are the same. William Friedman introduced it in 1922; per Wikipedia, uniformly random letters give 1/26, which is 0.0385, and English plaintext gives about 1.73 in the page's normalised form, so about 0.066 unnormalised (divide by 26). The plaintext of this example scores 0.0651. The ciphertext scores 0.0432, close to random. That is the cipher doing its job: it flattens single-letter frequencies, so ordinary frequency analysis of the whole text fails.
Step 1: Kasiski, repeated trigrams
The script finds 22 distinct trigrams that occur more than once. Several overlap (KTC, TCI, CIO are one repeated five-letter block), and trigrams that occur three times give two distances each. The result is 26 distances. Selected rows:
| Trigram | Positions (0-based) | Distance | Factors |
|---|---|---|---|
| KTC, TCI, CIO | 5, 179 | 174 | 2 x 3 x 29 |
| ALR, LRV | 33, 231 | 198 | 2 x 3^2 x 11 |
| PRX | 39, 117 | 78 | 2 x 3 x 13 |
| WPW | 176, 320 | 144 | 2^4 x 3^2 |
| BYF, YFV, VPV, PVT | 308 to 312, 380 to 384 | 72 | 2^3 x 3^2 |
| KJM | 209, 251 | 42 | 2 x 3 x 7 |
| CKM | 218, 227 | 9 | 3^2 |
| GBP | 22, 372 | 350 | 2 x 5^2 x 7 |
| APR | 116, 375 | 259 | 7 x 37 |
All 26 distances sorted: 2, 6, 9, 14, 29, 42, 42, 48, 48, 54, 72 (six times), 78, 144, 174 (three times), 198 (twice), 246, 259, 350. Twenty of them are multiples of 6. The six that are not (2, 9, 14, 29, 259 and 350) are chance repeats, and they are enough to ruin a plain GCD: the GCD of all 26 distances is 1. The GCD of the 20 multiples of 6 is 6, but you only know which to keep after the fact. The practical method is to count how many distances each candidate factor divides:
| Factor | Distances divisible (of 26) |
|---|---|
| 2 | 23 |
| 3 | 21 |
| 6 | 20 |
| 9 | 11 |
| 12 | 9 |
| 18 | 10 |
Factors 2 and 3 divide any multiple of 6, so they always score high. The rule used in the script below is: take the largest factor from 2 to 20 whose count is at least 70% of the top count. The top count is 23, the cutoff is 16.1, and the qualifying factors are 2, 3 and 6, so the answer is 6. The 70% cutoff is a choice, and the trials further down use the same rule.
Step 2: confirm with the index of coincidence
For each candidate k, split the ciphertext into k columns (every k-th letter), compute each column's IC, and average. If k is right, every column is a single Caesar shift of English and averages near 0.065. Results for this ciphertext:
| k | Average column IC |
|---|---|
| 1 | 0.0432 |
| 2 | 0.0466 |
| 3 | 0.0515 |
| 4 | 0.0467 |
| 5 | 0.0419 |
| 6 | 0.0639 |
| 7 | 0.0446 |
| 8 | 0.0483 |
| 9 | 0.0504 |
| 10 | 0.0431 |
| 11 | 0.0442 |
| 12 | 0.0641 |
k = 6 jumps out, and 12 scores high because it is a multiple of 6. Pick the smallest k that clearly clears about 0.06. The IC method needs no repeated trigrams, so it still works on short texts where Kasiski finds nothing, but each column needs enough letters (here about 67).
Step 3: solve each column
With k = 6, each column is a Caesar cipher. Try all 26 shifts and keep the one whose letter counts are closest to English (lowest chi-squared against English letter frequencies; see the note on the table in the script section):
| Column | Letters | Best shift | Key letter | chi-squared | Runner-up chi-squared |
|---|---|---|---|---|---|
| 0 | 68 | 2 | C | 21.9 | 174.0 |
| 1 | 68 | 8 | I | 18.3 | 277.8 |
| 2 | 68 | 15 | P | 25.8 | 127.4 |
| 3 | 67 | 7 | H | 20.4 | 189.7 |
| 4 | 67 | 4 | E | 14.9 | 222.8 |
| 5 | 67 | 17 | R | 13.0 | 152.6 |
The key is CIPHER. Decrypting gives ITISATRUTHUNIVERSALLYACKNOWLEDGEDTHATASINGLEMANINPOSSESSIONO... and the script asserts that the result equals the original 405 letters. The xerobit vigenere-cipher module, run in Node on the same ciphertext with key CIPHER, decrypts to the same text. With about 67 letters per column the winning shift beats the runner-up by at least 4.9 times in chi-squared here.
How much text do you need?
One example says little about the method. The script's second mode draws random windows from the same novel (541,355 letters after stripping), encrypts each with a random uppercase key, and checks whether each method recovers the key length. 300 windows per row, 4,500 ciphertexts in total, random.seed(1119). "Kasiski" uses the 70% rule above on trigram repeats. "IC" takes the smallest k up to 20 whose average column IC is at least 0.06 (the highest average if none is). "Key found" counts runs where the IC length was right and the chi-squared columns spelled the exact key.
| Letters | Key length | Kasiski length right | IC length right | Key found | No repeated trigram at all |
|---|---|---|---|---|---|
| 100 | 4 | 23% | 62% | 52% | 22% |
| 100 | 8 | 8% | 51% | 8% | 44% |
| 100 | 12 | 14% | 41% | 0% | 54% |
| 200 | 4 | 66% | 76% | 76% | 0% |
| 200 | 8 | 42% | 82% | 56% | 3% |
| 200 | 12 | 40% | 84% | 17% | 6% |
| 400 | 4 | 94% | 94% | 94% | 0% |
| 400 | 8 | 87% | 95% | 93% | 0% |
| 400 | 12 | 77% | 98% | 80% | 0% |
| 800 | 4 | 99% | 96% | 96% | 0% |
| 800 | 8 | 100% | 99% | 99% | 0% |
| 800 | 12 | 95% | 100% | 100% | 0% |
| 1600 | 4 | 99% | 96% | 96% | 0% |
| 1600 | 8 | 100% | 100% | 100% | 0% |
| 1600 | 12 | 100% | 100% | 100% | 0% |
What the table says, for this implementation and text:
- Below about 200 letters neither method is dependable. At 100 letters and key length 8, 44% of ciphertexts had no repeated trigram at all, so Kasiski had nothing to work with, and the key was recovered 8% of the time.
- The IC method beats the Kasiski method on short texts (200 letters, key length 12: 84% against 40% for the length). From 800 letters on, both find the length almost every time.
- Finding the length is not the same as finding the key. At 200 letters and key length 12 the length was right 84% of the time, but each column then has only about 17 letters, and the exact key came out 17% of the time.
- Short keys can fail on the IC side for odd reasons. In a separate 500-run check at 800 letters and key length 4, 13 runs returned the wrong length: 8 returned 2, 3 returned 12 and 2 returned 8. In two of the runs that returned 2 (keys HVHA and QERE) the key repeats a letter two places apart, which is consistent with the shorter period being picked; in one that returned 12 the average column IC at k = 4 was 0.0598, just under the 0.06 cutoff. The fixed cutoff is a weakness, not a rule of nature.
The results depend on the 70% rule, the 0.06 cutoff, the chi-squared solver and one novel's vocabulary; other thresholds will move the numbers.
The script
import re, random
from collections import Counter, defaultdict
def ic(s):
n, c = len(s), Counter(s)
return sum(v * (v - 1) for v in c.values()) / (n * (n - 1))
def kasiski(ct, n=3):
pos = defaultdict(list)
for i in range(len(ct) - n + 1):
pos[ct[i:i+n]].append(i)
return [b - a for p in pos.values() if len(p) > 1 for a, b in zip(p, p[1:])]
def votes(dists, kmax=20):
return {f: sum(d % f == 0 for d in dists) for f in range(2, kmax + 1)}
def kasiski_len(ct, kmax=20):
d = kasiski(ct)
if not d:
return None
v = votes(d, kmax)
top = max(v.values())
return max(f for f in v if v[f] >= 0.7 * top)
def col_ic(ct, k):
return sum(ic(ct[i::k]) for i in range(k)) / k
def ic_len(ct, kmax=20):
sc = {k: col_ic(ct, k) for k in range(1, kmax + 1)}
for k in range(1, kmax + 1):
if sc[k] >= 0.06:
return k
return max(sc, key=sc.get)
EN = [8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0.772,4.025,2.406,
6.749,7.507,1.929,0.095,5.987,6.327,9.056,2.758,0.978,2.360,0.150,1.974,0.074]
def solve_columns(ct, k):
key = ''
for i in range(k):
col = ct[i::k]
n, f = len(col), Counter(col)
def chi(shift):
return sum((f.get(chr(65 + (j + shift) % 26), 0) - n * EN[j] / 100) ** 2
/ (n * EN[j] / 100) for j in range(26))
key += chr(65 + min(range(26), key=chi))
return key
def encrypt(pt, key):
return ''.join(chr((ord(c) - 65 + ord(key[i % len(key)]) - 65) % 26 + 65)
for i, c in enumerate(pt))
Call kasiski_len(ct) and ic_len(ct), then solve_columns(ct, k). The EN table is the widely circulated English letter frequency list. The current Wikipedia letter frequency table, which cites Lewand's Cryptological Mathematics (MAA, 2000), is rounded and differs slightly (for example Q 0.12 against 0.095 here). Swapping in the Wikipedia values still recovered CIPHER, and exact-key recovery on 300 windows of 400 letters with a known key length of 8 was 98% with either table. Columns under about 30 letters give unreliable chi-squared; for the last letters of the key, look at the top two shifts and read the decrypted text.
Why this fails against a one-time pad
All of the above depends on the key repeating. If the key is as long as the message, never reused, and truly random, there are no key-aligned repeats to find, every column has one letter, and the ciphertext is equally consistent with every plaintext of that length. That is the one-time pad (the same idea as XOR with a random key). Anything shorter, such as a passphrase repeated, is breakable with the method above given enough text, which is why the Vigenere cipher is a teaching tool and not real protection.
A short check value
HELLOWORLD with key KEY encrypts to RIJVSUYVJN, which the tool module also returns.
Tested
2026-11-19: Python 3.11.15 for all analysis and Node v26.13.1 running the repo's vigenere-cipher module (it decrypts the 405-letter ciphertext with key CIPHER to the original text and returns RIJVSUYVJN for HELLOWORLD with key KEY). Plaintext: the first 405 letters of the Project Gutenberg text of ebook 1342 from "It is a truth universally acknowledged" onward, upper-cased with non-letters removed. The key CIPHER was recovered exactly and the script asserts equality with the plaintext. The 4,500-ciphertext trial used random.seed(1119), 300 windows per row. The 500-run key-length-4 check used random.seed(7). Not tested: other languages, other texts, Kasiski with 4-letter or longer repeats, and other cutoffs for the 70% and 0.06 rules.
Sources
- Wikipedia: Kasiski examination: Kasiski's 1863 publication, Babbage's earlier independent discovery, minimum repeat length and coincidental repeats.
- Wikipedia: Index of coincidence: Friedman, 1922; the 1/26 random value; the column-stacking key length test.
- Wikipedia: Letter frequency: the English frequency table, citing Lewand, Cryptological Mathematics (MAA, 2000).
- Project Gutenberg: Pride and Prejudice, ebook 1342: the plaintext source.
Related tools
Vigenere Cipher
Encode and decode messages using the Vigenere cipher with a custom keyword, showing the key stream alignment.
QR Code Generator
Generate QR codes for URLs, text, Wi-Fi, contact cards. Custom size, colors, error correction. Download as PNG or SVG. 100% client-side.
Barcode Generator
Generate Code 128, Code 39, EAN-13, UPC-A, EAN-8 and ITF barcodes as SVG with automatic check digit calculation.
Morse Code Translator
Translate text to Morse code and Morse back to English, live. Play it as audio at any speed, with a full alphabet chart.
Written by Mian Ali Khalid. More guides in Encoding & Crypto.