Tools · Classical ciphers
Vigenère cipher decoder, encoder and breaker
Encrypt and decrypt with a key, or paste a ciphertext and let the tool find the key length and then the key.
Nothing you type here leaves your browser.
This Vigenère cipher decoder works both ways. Give it a key and it encrypts or decrypts. Don't give it one and it breaks the cipher, finding the key length and then the key from the ciphertext alone. It all runs in your browser. For the story and the theory behind it, see the Vigenère cipher page.
Using it
With a key you know, type or paste the text, enter the key (letters only) and choose the direction. Spaces and punctuation are kept and don't use up key letters.
Without the key, paste the ciphertext into the text box and press "Crack it". You need roughly 20 letters of ciphertext per letter of key, so a 7-letter key wants about 150 letters, and the tool refuses anything under 40. It prints its best guess for the key and the decryption, plus a row of numbers showing how it picked the key length. If you know or suspect the length, enter it under "Key length" and the tool skips the guess. "Load a demonstration ciphertext" fills the box with a 217-letter passage from A Tale of Two Cities, encrypted with a seven-letter key, which the breaker finds.
How the breaker works
A Vigenère cipher with a key of k letters is k Caesar ciphers taking turns. The 1st, (k+1)th and (2k+1)th letters share a shift, and so on. The tool does two jobs.
- It finds k. For each length from 1 to 20 it cuts the ciphertext into that many columns and averages the index of coincidence of the columns, which is the chance that two letters drawn from a column match. English scores about 0.067 and random letters score 1/26, about 0.038. At the true key length each column is a shifted English text and scores high. At a wrong length the columns mix several shifts and score low. Multiples of the true length also score high, so the tool takes the shortest length whose score is within 8% of the best.
- It solves each column. For every column it tries all 26 shifts and keeps the one whose letter counts are closest to English by a chi-squared test, exactly as the Caesar decoder does. The seven winning shifts are the seven key letters.
The method and its history, with a worked example, are on the cipher page. The result is tested too. The project's test suite breaks a Dickens passage with the key DICKENS and checks that the key comes back exactly.
When it fails
- Too little text. With fewer than about 10 letters per key letter, the column statistics are noise and the key comes out wrong, though often close. A nearly right key gives almost readable text, so try changing a letter or two.
- A key as long as the text. If the key never repeats, there are no columns to cut. That's the one-time pad, and this method can't break it.
- Autokey and other variants. The autokey cipher has no repeating key. Beaufort ciphertexts need the Atbash step first, and the Beaufort page shows how.
- Not English. The scoring uses English letter frequencies.
Common questions
Is the Vigenère cipher secure?
No. It was the strongest practical cipher of its time, but it's been breakable by hand since the 1850s, and a computer solves it in a blink. Use it for puzzles and learning.
What if my message contains numbers and symbols?
They pass through unchanged and don't use up key letters. If the puzzle you're solving counts them, remove them first.
Why does the guessed key sometimes repeat, like IREVIIREVI?
If the true key is RIVER (5 letters), a length of 10 fits the data equally well, because 10 is a multiple of 5. The tool prefers the shortest length that scores close to the best, and when it picks a multiple, the key it prints is the real key written twice.
Is the text I paste sent anywhere?
No. The cipher and the breaker are JavaScript in this page, and the page makes no network requests.