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 and a primer , the key stream is for and for . Encryption gives with . Decryption uses . Each recovered plaintext letter becomes a later key letter.
Key Formula
Where:
- = Number of the i-th plaintext letter (A = 0, ..., Z = 25)
- = Number of the i-th key letter: a primer letter first, then earlier plaintext letters
- = Number of the i-th ciphertext letter, from 0 to 25
- = 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.
Step 2: Add each plaintext number to its key number and reduce mod 26. Two sums pass 25, so they wrap around the alphabet.
Step 3: Convert the numbers back to letters. To check, decrypt the first letter: subtract the primer value and reduce mod 26.
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.
