TermStepComputer Science Pathway · Years 7 to 13

Computer Science › Tools › Vigenère cipher

Vigenère cipher

Encrypt and decrypt with the Vigenère cipher. Type a message and a keyword, then watch each letter take its own shift from the keyword, on the wheel and on the Vigenère square. When you have a long ciphertext, the cracker below can find the keyword for you.

Outer ring: plaintextInner ring: ciphertext

The inner wheel turns to a new shift for every letter.

Letters only. Each letter is a shift: A = 0, B = 1 … Z = 25.

The Vigenère square: row = key letter, column = plaintext letter

Crack a Vigenère cipher

The cracker guesses the length of the keyword, splits the ciphertext into that many columns and treats each column as a Caesar cipher, picking the shift whose letters look most like English. It needs a long message: at least 60 letters, and 200 or more works best. Paste a ciphertext into the box above, or load a long example.

What is the Vigenère cipher?

The Vigenère cipher (often typed Vigenere cipher) is a polyalphabetic substitution cipher. Instead of one shift for the whole message, as in the Caesar cipher, it uses a keyword. Each letter of the keyword gives a different shift (A = 0, B = 1 … Z = 25), and the keyword repeats along the message. So the letter E might become G in one place and X in another, which hides the letter patterns that make the Caesar cipher easy to break.

It is named after Blaise de Vigenère, a French diplomat of the 1500s, although an earlier version was described by Giovan Battista Bellaso. For about 300 years it was thought to be unbreakable and was known as “le chiffre indéchiffrable”, the indecipherable cipher.

How to use the Vigenère square

  1. Write the keyword under the message, repeating it as many times as needed. Skip spaces and punctuation, so the keyword only lines up with letters.
  2. To encrypt a letter, find the row that starts with the key letter and the column headed by the plaintext letter. The letter where they cross is the ciphertext.
  3. To decrypt, go to the key letter’s row, find the ciphertext letter in that row and read the plaintext letter at the top of its column.

Worked example

Encrypt ATTACK AT DAWN with the keyword LEMON:

PlaintextATTACKATDAWN
KeyLEMONLEMONLE
Shift114121413114121413114
CiphertextLXFOPVEFRNHR

So the ciphertext is LXFOPV EF RNHR. Notice that the two Ts became X and F, and the three As became L, O and E.

The Vigenère cipher formula

Number the letters A = 0 … Z = 25. For the ith letter of the message Pi and the key letter lined up with it Ki:

Encrypt: Ci = (Pi + Ki) mod 26
Decrypt: Pi = (Ci − Ki) mod 26

A Caesar cipher is a Vigenère cipher whose keyword is one letter long: the keyword D is a Caesar shift of 3.

Vigenère cipher in Python

The key position i only moves on when a letter is encrypted, so spaces and punctuation do not use up key letters.

def vigenere(text, key, decrypt=False):
    result = ""
    key = key.upper()
    i = 0
    for ch in text:
        if ch.isalpha():
            shift = ord(key[i % len(key)]) - 65
            if decrypt:
                shift = -shift
            base = 65 if ch.isupper() else 97
            result += chr((ord(ch) - base + shift) % 26 + base)
            i += 1
        else:
            result += ch
    return result

print(vigenere("Attack at dawn", "LEMON"))                 # Lxfopv ef rnhr
print(vigenere("Lxfopv ef rnhr", "LEMON", decrypt=True))   # Attack at dawn

How the Vigenère cipher was broken

Where it appears in the curriculum

The Vigenère cipher is a common next step after the Caesar cipher in KS3 Computing and in GCSE and IGCSE lessons on encryption (AQA 8525, OCR J277, Cambridge IGCSE 0478 and Edexcel International GCSE). It shows why a longer key makes encryption stronger, and why modern ciphers such as AES use keys far too long to guess. It is also a good programming challenge for practising strings, loops and the modulo operator. On TermStep it follows the Caesar cipher in KS3 Year 9 Cybersecurity.

Questions and answers

What is the difference between the Caesar and Vigenère ciphers?

The Caesar cipher shifts every letter by the same amount. The Vigenère cipher uses a keyword, so the shift changes from letter to letter. That makes it a polyalphabetic cipher and much harder to break by counting letters.

How do you decrypt a Vigenère cipher without the key?

Find the keyword length first, using repeated letter groups (Kasiski examination) or the index of coincidence. Then split the ciphertext into that many columns and solve each column as a Caesar cipher with frequency analysis. The “Crack the ciphertext” button on this page does both steps.

Is the Vigenère cipher symmetric?

Yes. The same keyword is used to encrypt and decrypt: you add the key letters to encrypt and subtract them to decrypt.

Why is it called the indecipherable cipher?

For about three centuries nobody published a general way to break it, so it was called “le chiffre indéchiffrable”. Babbage and Kasiski found ways to break it in the 1800s.

Do spaces and punctuation use up letters of the keyword?

Usually not. In the common version, used by this tool, the keyword only moves on when a letter is encrypted, and spaces, digits and punctuation are copied across unchanged.

What makes a good Vigenère keyword?

A longer keyword is harder to crack, because each column of the ciphertext has fewer letters to analyse. A random keyword as long as the message that is never reused becomes a one-time pad.