---
title: "Beaufort"
description: "Subtract each letter from a repeating key. The same operation encodes and decodes."
canonical_url: "https://ciphers.agntn.dev/ciphers/beaufort"
---
# Beaufort

> Subtract each letter from a repeating key. The same operation encodes and decodes.

<cipher-facts name="beaufort">



</cipher-facts>

Looks like a [Vigenère](https://ciphers.agntn.dev/ciphers/vigenere) turned inside out. Instead of adding the key letter to the text, it takes the text letter away from the key: `(key - input) mod 26`, with A as 0. Do that twice and you're back where you started, so `encode` and `decode` are the same call. That's the standard Beaufort. The variant one subtracts the key from the input, isn't its own inverse, and isn't what this is.

```ts
const beaufort = create("beaufort");
beaufort.encode("DCODE", { key: "KEY" }).text; // "HCKHA"
beaufort.decode("HCKHA", { key: "KEY" }).text; // "DCODE"
beaufort.encode("HCKHA", { key: "KEY" }).text; // "DCODE" too, same thing
beaufort.encode("DCODE", { key: "123" }); // throws InvalidOptionError, no letters in the key
```

`HCKHA` is the [example dCode publishes](https://www.dcode.fr/beaufort-cipher), so you can check it against something that isn't this library.

Only ASCII letters advance the key. Spaces, punctuation and emoji pass through, lowercase stays lowercase unless `preserveCase` is false, and `stripNonAlpha` throws out everything outside A to Z first. The key itself ignores case and anything that isn't a letter. No key at all is a `MissingOptionError`.

Reciprocal is handy on paper, one table for both directions. It doesn't make it harder to break. The key still repeats, so there's still a period to find, same as Vigenère.


## Sitemap

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