---
title: "Autokey"
description: "A primer starts the key, then the plaintext takes over."
canonical_url: "https://ciphers.agntn.dev/ciphers/autokey"
---
# Autokey

> A primer starts the key, then the plaintext takes over.

<cipher-facts name="autokey">



</cipher-facts>

Vigenère's weak spot is the keyword going round and round. Autokey stops it from repeating. The primer covers the first few letters, then the plaintext itself becomes the key: `QUEENLY` followed by `ATTACK...`. This is the plaintext variant, not the one that feeds the ciphertext back.

```ts
const autokey = create("autokey");
autokey.encode("ATTACKATDAWN", { key: "QUEENLY" }).text; // "QNXEPVYTWTWP"
autokey.decode("QNXEPVYTWTWP", { key: "QUEENLY" }).text; // "ATTACKATDAWN"
autokey.encode("ATTACK AT DAWN", { key: "QUEENLY" }).text; // "QNXEPV YT WTWP"
```

Same numbers as the [example on Wikipedia](https://en.wikipedia.org/wiki/Autokey_cipher#Method). The effective key is `QUEENLYATTACK`, and decoding has to rebuild it one recovered letter at a time. Repeat `QUEENLY` instead and you get garbage after the seventh letter, which is a nice way to tell Autokey from Vigenère.

Only ASCII letters advance the key, so the spaces in the last line cost nothing. Lowercase stays lowercase unless `preserveCase` is false, `stripNonAlpha` drops everything outside A to Z. The primer ignores case and anything that isn't a letter, a missing one is a `MissingOptionError`, one with no letters at all an `InvalidOptionError`. Every call starts from the primer again, there's no state between them.

No period to hunt for, which is progress. But the key is the plaintext, so a good guess at one word of it unlocks the letters after it. Fun for a puzzle. Not for anything else.


## Sitemap

See the full [sitemap](https://ciphers.agntn.dev/sitemap.md) for all pages.
