The Autokey cipher is a polyalphabetic substitution cipher devised by Blaise de Vigenère, and it is a notable improvement on the better-known Vigenère cipher that bears his name. Its defining trick is that the encryption key extends itself with the plaintext, so the running key is exactly as long as the message and never falls into a repeating pattern.
How it works
Every letter becomes a number from 0 (A) to 25 (Z). The cipher builds a key stream: it starts with your keyword (called the primer) and then, as each plaintext letter is enciphered, appends that plaintext letter to the stream. Encryption uses the Vigenère addition rule:
C = (P + K) mod 26
where K is the current key-stream value. Because the primer is short but the plaintext continues the key, there is no short repeat for an attacker to exploit.
Decryption must run progressively. The first letters are decrypted with the keyword primer using P = (C - K) mod 26. Each recovered plaintext letter is then appended to the key stream and used to decrypt the following letter. This chaining is automatic in the tool, so you only supply the original keyword.
Only letters are enciphered and advance the key; spaces, digits and punctuation pass through untouched and keep the alignment intact.
Example
With keyword KEY and plaintext HELLO, the key stream is KEYHE — the primer KEY followed by the first two plaintext letters HE. Adding letter by letter:
P: H E L L O
K: K E Y H E
C: R I J S S
Decrypting RIJSS with keyword KEY recovers H, appends it to the key, recovers E, and so on, rebuilding HELLO.
Notes
- The Autokey cipher resists Kasiski examination because the key has no period, but error propagation means a single wrong letter garbles the rest of the message.
- Pick a primer that is not a common word to avoid easy guessing of the opening letters.
- For a reciprocal classical cipher where encryption equals decryption, see the Beaufort cipher.