---
title: "Encode and decode"
description: "The two methods every cipher has, the two options they share, and what happens to case and punctuation on the way through."
canonical_url: "https://ciphers.agntn.dev/guide/transform"
---
# Encode and decode

> The two methods every cipher has, the two options they share, and what happens to case and punctuation on the way through.

## Two methods

```ts
import { create, resolveCipher } from "@agntn/ciphers";

const caesar = create("caesar");
caesar.encode("Hello, World!", { shift: 7 }).text; // "Olssv, Dvysk!"
caesar.decode("Olssv, Dvysk!", { shift: 7 }).text; // "Hello, World!"

resolveCipher("Rail Fence").encode("WEAREDISCOVEREDRUNATONCE", { rails: 3 }).text;
// "WECRUOERDSOEERNTNEAIVDAC"
```

`create` wants the exact registry name. `resolveCipher` lowercases and turns runs of whitespace into a hyphen, so `"Rail Fence"` and `"rail-fence"` are the same thing, and then it also wants an exact match. Both cache one instance per name.

## Options every classical cipher shares

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Effect
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        preserveCase
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Lowercase in, lowercase out. <code>
        false
      </code>
      
       uppercases the output.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        stripNonAlpha
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      Drop everything that isn't an ASCII letter before the cipher runs.
    </td>
  </tr>
</tbody>
</table>

The `block` and `stream` ciphers are the exception. They encrypt the UTF-8 bytes of the text as they are and ignore both. With `bytes: "hex"` they read hex in place of text and decode back to hex, for plaintext that isn't text at all.

With the defaults a Caesar leaves spaces, digits and punctuation exactly where they were and shifts the letters around them. A puzzle that wants one solid block gets `stripNonAlpha: true`:

```ts
create("vigenere").encode("ATTACK AT DAWN", { key: "LEMON", stripNonAlpha: true }).text;
// "LXFOPVEFRNHR"
```

`options` on the result echoes what was applied, both flags included when you set them. One log line and you can rerun the call.

## Ciphers that reshape the input

Some ciphers can't leave the input alone, so the round trip comes back reshaped:

- [Playfair](https://ciphers.agntn.dev/ciphers/playfair) works on letter pairs. It strips non-letters, folds J into I, splits doubled letters with X and pads an odd length with X.
- [Four-square](https://ciphers.agntn.dev/ciphers/four-square) works on letter pairs too. It strips non-letters, folds J into I (or drops Q with `omit: "q"`) and pads an odd length with X, but splits nothing, so doubled letters stay doubled.
- [Two-square](https://ciphers.agntn.dev/ciphers/two-square) does exactly the same to the text. Its ciphertext is the odd one: about a pair in five comes out as it went in.
- [Hill](https://ciphers.agntn.dev/ciphers/hill) works on blocks of 2 or 3 symbols of its `alphabet`. That's A-Z unless you pick another. Everything else goes. X pads the last block, or the last symbol when there's no X. Decoding refuses strangers, whitespace aside.
- [ADFGVX](https://ciphers.agntn.dev/ciphers/adfgvx) keeps letters and digits and drops the rest.
- [Bifid](https://ciphers.agntn.dev/ciphers/bifid) and [Polybius](https://ciphers.agntn.dev/ciphers/polybius) fold J into I by default, and Bifid drops non-letters. With `alphabet`, nothing is folded and symbols outside it are dropped. A 36 symbol alphabet gives both a 6×6 square with digits.
- [Trifid](https://ciphers.agntn.dev/ciphers/trifid) has room for all 26 letters plus its `symbol`, so nothing folds. Everything else is dropped.
- [Nihilist](https://ciphers.agntn.dev/ciphers/nihilist) folds J into I too, drops non-letters and hands back numbers with single spaces.
- [Morse](https://ciphers.agntn.dev/ciphers/morse), [Bacon](https://ciphers.agntn.dev/ciphers/bacon), [tap code](https://ciphers.agntn.dev/ciphers/tap-code) and [A1Z26](https://ciphers.agntn.dev/ciphers/a1z26) change the alphabet entirely. You get uppercase back and, for Morse, single spaces. Digits sent through A1Z26 come back as letters.
- The [book cipher](https://ciphers.agntn.dev/ciphers/book) gives back words as the book spells them. Punctuation is gone. With `pick: "letter"` it's uppercase letters, glued. And `options` holds counts, not the book. Keep the book next to that log line.

For everything else `decode(encode(x)) === x` holds for any input. Emoji, Polish letters, whatever - they pass through untouched.

## Self-inverse ciphers

ROT-13, ROT-47, Atbash and Enigma are their own inverse. `encode` and `decode` do the same thing and `info().selfInverse` says so. Both methods still exist on every cipher, so you can write `decode` and mean it.

```ts
const rot13 = create("rot13");
rot13.decode(rot13.encode("HELLO").text).text === "HELLO"; // true
rot13.encode("URYYB").text; // "HELLO", same thing
```

## Options per cipher

Each cipher validates its own options and lists them in `info().options` with a type, whether it is required, a default and a description. The [Playground](https://ciphers.agntn.dev/playground) form and `ciphers info` are built from that list, nothing is typed twice:

```ts
create("affine").info().options;
// [
//   { name: "a", type: "number", required: false, default: 5, description: "Multiplier (must be coprime with 26: ...)" },
//   { name: "b", type: "number", required: false, default: 8, description: "Additive shift (0-25)" },
// ]
```

In your own cipher read one with `getOpt(options, "shift", 3)`. Fallback when the key is absent, the value as typed otherwise.

## What a wrong option does

```ts
create("caesar").encode("X", { shift: 0 }); // InvalidOptionError, 0 is the identity
create("affine").encode("X", { a: 13 }); // InvalidOptionError, gcd(13, 26) is 13
create("vigenere").encode("X"); // MissingOptionError: key
create("enigma").encode("X", { plugboard: "AB AC" }); // InvalidOptionError, a letter in two pairs
```

All of them thrown before the first character is touched. A cipher that quietly accepts shift 0 and hands your text back unchanged is worse than one that throws.


## Sitemap

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