Mathwords logoMathwords

Autokey Cipher — Definition, Formula & Example

An autokey cipher is a secret code that shifts each letter of your message by an amount set by a key. The key starts with a short word called the primer and then continues with the letters of the message itself, so it never cycles back through a fixed keyword.

Number the letters A = 0, B = 1, ..., Z = 25. Given a plaintext P1P2…PnP_1 P_2 \dots P_n and a primer k1k2…kmk_1 k_2 \dots k_m, the key stream is Ki=kiK_i = k_i for 1≤i≤m1 \le i \le m and Ki=Pi−mK_i = P_{i-m} for i>mi > m. Encryption gives Ci≡Pi+Ki(mod26)C_i \equiv P_i + K_i \pmod{26} with 0≤Ci≤250 \le C_i \le 25. Decryption uses Pi≡Ci−Ki(mod26)P_i \equiv C_i - K_i \pmod{26}. Each recovered plaintext letter becomes a later key letter.

Key Formula

Ci=(Pi+Ki) mod 26,Pi=(Ci−Ki) mod 26C_i = (P_i + K_i) \bmod 26, \qquad P_i = (C_i - K_i) \bmod 26
Where:
  • PiP_i = Number of the i-th plaintext letter (A = 0, ..., Z = 25)
  • KiK_i = Number of the i-th key letter: a primer letter first, then earlier plaintext letters
  • CiC_i = Number of the i-th ciphertext letter, from 0 to 25
  • 2626 = The modulus, one for each letter of the alphabet

Worked Example

Problem: Encrypt the word MATH with an autokey cipher using the one-letter primer X.
Step 1: Build the key. Start with the primer X, then append the plaintext letters until the key is 4 letters long. Then convert every letter to a number.
Key=X M A T→23,12,0,19MATH→12,0,19,7\text{Key} = \text{X M A T} \to 23, 12, 0, 19 \qquad \text{MATH} \to 12, 0, 19, 7
Step 2: Add each plaintext number to its key number and reduce mod 26. Two sums pass 25, so they wrap around the alphabet.
12+23=35≡90+12=1219+0=197+19=26≡0(mod26)\begin{aligned} 12 + 23 &= 35 \equiv 9 \\ 0 + 12 &= 12 \\ 19 + 0 &= 19 \\ 7 + 19 &= 26 \equiv 0 \end{aligned} \pmod{26}
Step 3: Convert the numbers back to letters. To check, decrypt the first letter: subtract the primer value and reduce mod 26.
9,12,19,0→J M T A,(9−23) mod 26=12=M9, 12, 19, 0 \to \text{J M T A}, \qquad (9 - 23) \bmod 26 = 12 = \text{M}
Answer: The ciphertext is JMTA. The receiver decrypts J with the primer X to get M, then uses M as the key for the next letter, and so on.

Why It Matters

An autokey system was described by Blaise de Vigenère in 1586 as a fix for the main weakness of a repeating keyword: the repeat gives the pattern away. Studying it shows how modular addition and subtraction undo each other. It also shows why the cipher is still breakable, because part of the key is the message itself, so a correct guess at one word reveals more of the key.

Common Mistakes

Mistake: Repeating the primer over and over, as in a Vigenère cipher (for example, using key XXXX for MATH).
Correction: In an autokey cipher the primer is used only once. After it runs out, the key continues with the plaintext letters, so the key for MATH with primer X is XMAT.

Related Terms