Vigenere Cipher

A classical, poly-alphabetic cipher. Invented in 1533 by Giovan Battista Bellaso, misattributed to Blaise de Vigenère.

Each letter is shifted by an amount that depends on its position in the message, so the ciphertext letter frequencies are flattened. Also a stream cipher.

You can find an implementation of the Vigenère cipher in sahithyandev/ciphers.

Key Expansion

Repeat the key until it matches the plaintext length. Align each key letter with 1 plaintext letter.

Key Space

For a fixed keyword length nn, each position independently selects 1 of 26 shifts. The key space has 26n26^n members. Different allowed keyword lengths form separate key spaces, so the length must be specified when quoting the size.

Encryption

Assign each plaintext letter and its aligned key letter a value from 00 to 2525, where A=0A = 0 and Z=25Z = 25. Add their values modulo 2626. The result identifies the ciphertext letter.

Decryption

The key is repeated and aligned with the ciphertext the same way. Each ciphertext letter is shifted backward by the position of the key letter above it.

  • T shifted back by L (11) gives I
  • E shifted back by E (4) gives A

Attacking

Security depends significantly on keeping the key length secret. Once the key length is found, the cipher can be attacked in the same manner as a shift cipher, using statistical properties of the plaintext language for each block of ciphertext extracted from separate keyword positions.

Kasiski Test

Determines the keyword length.

Steps:

  • Locate a repeating character sequence in the ciphertext.
  • Count the distances between successive occurrences of that sequence.
  • Take the greatest common divisor of these distances as the keyword length, or a multiple of it.

Repeated short strings, often common bigrams or trigrams of the language, that align to the same keyword position produce the same ciphertext string.

Fixed Transposition

A fixed transposition after Vigenère encryption does not remove the keyword’s period. Under Kerckhoffs’s principle, the transposition is public and an attacker reverses it before attacking the substitution.

If the transposition block length bb is a multiple of the keyword length ll, each block begins at the same keyword position.

b≡0(modl)b \equiv 0 \pmod l

The period therefore remains aligned across every block. Repeated ciphertext patterns still expose multiples of ll.

Worked Example

Plaintext finishhimfinishhim under keyword RUN, with A=0A = 0 to Z=25Z = 25. The plaintext is the same short phrase said twice, so its repetition shows up directly in the ciphertext.

Solution Reveal Hide

The keyword is repeated to match the plaintext length.

Plaintext finishhimfinishhim
Keystream RUNRUNRUNRUNRUNRUN
Ciphertext WCAZMUYCZWCAZMUYCZ
Each column combines 1 plaintext letter with 1 keystream letter to produce the ciphertext letter below it.

Each position adds the keystream letter modulo 2626.

  • f(5)+R(17)=22=Wf (5) + R (17) = 22 = W
  • i(8)+U(20)=28≡2=Ci (8) + U (20) = 28 \equiv 2 = C

Ciphertext is WCAZMUYCZWCAZMUYCZ.

Decryption subtracts the keystream letter modulo 2626 at each position.

  • W(22)−R(17)=5=fW (22) - R (17) = 5 = f
  • C(2)−U(20)=−18≡8=iC (2) - U (20) = -18 \equiv 8 = i

Numbering ciphertext positions from 1, the trigram WCA appears at positions 1 to 3 and 10 to 12.

Distance between the occurrences is 10−1=910 - 1 = 9. Only 1 distance is available, so the keyword length is a divisor of 99. Divisors are 11, 33, 99. Length 11 is ruled out by trying it and getting non-language output. Length 33 already recovers language, so the keyword length is 33.

Split the ciphertext into 3 columns, column ii holding positions i,i+3,i+6,…i, i + 3, i + 6, \dots. Each column is a shift cipher under one keyword letter.

Frequency analysis on each column recovers the shifts 17,20,1317, 20, 13, which is the keyword RUN.

Decrypting with RUN gives finishhimfinishhim.

Written by October 27, 2026 3 min read
Was this helpful?