Mathwords logoMathwords

Polyalphabetic Cipher — Definition, Formula & Example

A polyalphabetic cipher is a secret code that switches between several different letter substitutions as it moves through a message. The same plaintext letter can therefore turn into different ciphertext letters in different positions.

A polyalphabetic substitution cipher uses more than one substitution alphabet, choosing which one to apply from the letter's position (or from a key stream). In the most common (periodic) form, a cycle of mm alphabets is used, with alphabet (i mod m)(i \bmod m) on the letter in position ii; non-periodic forms such as the autokey cipher also exist. In its best-known form, the Vigenère cipher, each letter is identified with an integer in {0,1,…,25}\{0, 1, \dots, 25\}. A keyword k0k1…km−1k_0 k_1 \dots k_{m-1} then defines the encryption Ci=(Pi+ki mod m) mod 26C_i = (P_i + k_{i \bmod m}) \bmod 26.

Key Formula

Ci=(Pi+Ki mod m) mod 26,Pi=(Ci−Ki mod m) mod 26C_i = (P_i + K_{i \bmod m}) \bmod 26, \qquad P_i = (C_i - K_{i \bmod m}) \bmod 26
Where:
  • PiP_i = Number of the plaintext letter in position i (A = 0, B = 1, ..., Z = 25), counting positions from i = 0
  • CiC_i = Number of the ciphertext letter in position i
  • Ki mod mK_{i \bmod m} = Number of the keyword letter used at position i. The keyword repeats.
  • mm = Length of the keyword (the number of different shifts in the cycle)

Worked Example

Problem: Encrypt the word MOON with a Vigenère cipher using the keyword KEY. Use A = 0, B = 1, ..., Z = 25.
Step 1: Convert the letters to numbers. Repeat the keyword under the plaintext until it covers every letter, so KEY becomes KEYK.
PlaintextMOONPi12141413KeyKEYKKi1042410\begin{array}{c|cccc} \text{Plaintext} & M & O & O & N \\ P_i & 12 & 14 & 14 & 13 \\ \hline \text{Key} & K & E & Y & K \\ K_i & 10 & 4 & 24 & 10 \end{array}
Step 2: Add each pair and reduce mod 26. Only the third sum is 26 or more, so only it wraps around.
12+10=22,14+4=18,14+24=38≡12(mod26),13+10=2312+10=22, \quad 14+4=18, \quad 14+24=38 \equiv 12 \pmod{26}, \quad 13+10=23
Step 3: Convert the results back to letters: 22 = W, 18 = S, 12 = M, 23 = X.
22, 18, 12, 23  ⟶  WSMX22,\ 18,\ 12,\ 23 \;\longrightarrow\; \text{WSMX}
Answer: MOON encrypts to WSMX. The two O's became S and M because they used different key letters. A single-shift (Caesar) cipher could never do that.

Why It Matters

A Caesar cipher sends every E to the same letter, so you can break it by counting which letter shows up most often. A polyalphabetic cipher spreads each letter across several ciphertext letters, and this defeated simple frequency analysis for about 300 years, until Friedrich Kasiski published a general attack in 1863. The same idea, taken to a random key as long as the message, gives the one-time pad, which is studied in intro cryptography and computer security courses.

Common Mistakes

Mistake: Stopping at a sum like 38 and treating it as the answer, or subtracting 25 instead of 26 to wrap around.
Correction: The alphabet has 26 positions (0 through 25), so every sum must be reduced mod 26. Since 38 − 26 = 12, the letter is M. Any result of 26 or more is not yet a valid letter.

Related Terms

  • Modular Arithmetic — Supplies the wrap-around addition used for every letter shift
  • Modulo n — Defines the mod 26 reduction that keeps results as letters
  • Modular Equivalence — Explains why 38 and 12 name the same letter
  • Modular Equivalence Rules — Justify why subtracting the key reverses the encryption
  • Frequency — Counting letter frequencies is the basis of frequency analysis
  • Autokey Cipher — Polyalphabetic cipher whose key continues with the message itself