T

Text Machine

Powerful text tools, in your browser

Kasiski Examination

Find the key length of a repeating-key cipher from the distances between repeated sequences.

Try:

Ciphertext
Settings

Sequence length

Factors up to

Three letters is the classical choice. Two finds far more repeats but most of them are coincidence, and four or more finds only the strongest evidence, which needs a longer text.

Paste a ciphertext to examine it. The samples are real Vigenere ciphertext with keys of five and seven letters.

How to use Kasiski Examination

  1. 1

    Paste the ciphertext

    Drop the encrypted text into the box. A few hundred letters is usually enough for repeats to appear; below about a hundred there is often nothing to find.

  2. 2

    Pick a sequence length

    Start at three letters. If nothing repeats, drop to two and treat the result as weak evidence. If the text is long and the tally is noisy, raise it to four.

  3. 3

    Read the factor support

    Each candidate length is scored against what chance alone would give it, and what you see suggested is the strongest member of each family of divisors and multiples. The raw distance count and the number of distinct sequences behind it are shown as well: a factor supported by several different sequences is more trustworthy than one chatty sequence repeating, and that judgement is left to you rather than folded into the score.

  4. 4

    Take the length to a solver

    Feed the suggested length to the Vigenere solver, or cross-check it against the column table on the Index of Coincidence calculator. Two methods agreeing is much stronger than either alone.

Kasiski examination, explained

The observation the method rests on

A Vigenere cipher shifts each letter by a different amount, cycling through a keyword. That defeats letter counting, because the same plaintext letter becomes different ciphertext letters depending on where it falls. But the key repeats, and repetition leaks.

Suppose the word THE appears twice in the plaintext, and by coincidence both occurrences line up with the same three letters of the key. Both encipher to the same three ciphertext letters. The distance between them, counted in letters, is therefore a whole number of key cycles: a multiple of the key length.

One such coincidence proves little. Dozens of them, factored and tallied, point straight at the length.

What this tool measures

Every sequence of the chosen length that occurs more than once is collected with its positions. For each one, every pairwise distance is computed, not just the gaps between neighbours, because three occurrences carry three pieces of evidence and taking only consecutive gaps throws one away.

Each candidate key length then gets two numbers: how many distances it divides, and how many distinct repeated sequences contributed at least one of those distances. The ranking uses the first, corrected for chance. The second sits beside it and is deliberately kept out of the score, because it is a judgement rather than an arithmetic: one chatty sequence repeating eight times inflates the distance count without adding independent evidence, whereas three different sequences agreeing on a factor is three separate coincidences pointing the same way. If the suggested length rests on a single sequence, that is the number that should make you doubt it.

The divisor trap

This is the part that catches people, and it is arithmetic rather than cryptography. If 12 divides a distance then so do 2, 3, 4 and 6. Every divisor of the true key length inherits its entire score, so a table sorted by support puts the smallest factors on top no matter what the key actually was.

The fix is arithmetic too. Charge every factor for the support chance hands it: 2 divides half of any set of distances for free, so its raw count must be halved before it can be set beside a factor that divides only a sixth of them. Correct for that and the true key length outranks its own divisors, because a divisor carries only its own share of the evidence. Correct as well for the noise a factor is entitled to, which grows with the factor, and the true length outranks its multiples too. What is left is one rule with nothing tuned in it: within any family of divisors and multiples, keep the strongest member. The full tally stays underneath so you can disagree with it: if the suggested length has only one sequence behind it, a factor with six sequences behind it deserves a look.

When it fails, and what to do instead

Kasiski examination needs repeats, and a short message may simply not have any. It also degrades against ciphers that deliberately avoid repetition, and it says nothing at all about a running-key or one-time-pad system where the key never cycles.

The Index of Coincidence approaches the same question from the opposite direction: it ignores repeats entirely and measures the letter distribution of each candidate column split. Because the two methods rely on different properties of the text, they rarely fail together, and a length that both support is usually the right one.

Frequently asked questions

What is Kasiski examination?
It is a method published by Friedrich Kasiski in 1863 for finding the key length of a repeating-key cipher. When the same plaintext sequence happens to line up with the same part of the key, it encrypts to the same ciphertext sequence. The distance between two such sequences is therefore a multiple of the key length, so factoring lots of those distances reveals the length.
Why does it suggest 6 when 2 and 3 have the same support?
Because every divisor of the real key length divides exactly the same distances that the real length does. If the key is 6 letters, then 2 and 3 score at least as highly as 6 in the raw tally, and a list sorted purely by support would recommend 2 for almost every text. The suggestion keeps the largest well-supported factor and drops its own divisors, which is what a cryptanalyst does by eye.
How much ciphertext does it need?
Repeats are what the method runs on, and they get rarer as the text gets shorter. A few hundred letters usually produces several trigram repeats. Under about a hundred letters you will often find nothing, and what you do find is as likely to be coincidence as evidence.
Why are the positions different from where I see the text?
Positions and distances are counted in letters only. Spaces, digits, punctuation and line breaks are removed before anything is measured, because the cipher never saw them either. A tool that counts them will report different numbers for the same text.
Should I use two-letter sequences?
Only as a last resort. In a few hundred letters, pairs repeat by chance constantly, and the factor tally turns into noise that will happily support any length you like. Three letters is the classical choice because a chance trigram repeat is rare enough to mean something.
What if Kasiski and the Index of Coincidence disagree?
Trust the one with more evidence behind it, and prefer a length they both allow. The two methods fail differently: Kasiski struggles when the text is short or has few repeats, while the Index of Coincidence struggles when the key is long relative to the message. If they disagree, a common multiple or a common divisor of the two answers is often the real length.
Does my ciphertext leave my browser?
No. The whole examination runs in the page. Nothing is uploaded, logged or stored anywhere.

Related tools

Keep going with these handy tools

Index of Coincidence Calculator

Vigenère Solver

Vigenère Cipher

Cipher Identifier

Frequency Analysis

Substitution Cipher Solver