Skip to main content

Cryptography & Ciphers

Affine Cipher Encoder

Encode English letters with an affine substitution using numeric values a and b. Check the worked example and matching decoder.

Tool workspace

Your input stays in this browser.

Add the required input to continue.

Needs input

You can run the tool again after any change.

Options

Your result appears here

Encode English letters with an affine substitution using numeric values a and b. Check the worked example and matching decoder.

This tool runs open-source code from CyberChef by GCHQ (Apache License 2.0). See open-source credits.

Affine Cipher Encoder replaces each letter with y = (a × x + b) mod 26, counting A = 0 through Z = 25. The multiplier a must share no factor with 26. With a = 5 and b = 8, Hello, World! becomes Rclla, Oaplx!.

The output has the same length and punctuation as the input. Only the letters A–Z and a–z change.

  • Write an affine exercise for a lesson on modular arithmetic.
  • Show that a = 1 turns the affine cipher into a Caesar shift by b.
  • Produce ciphertext for testing your own affine decoder.

Instructions

  1. Type the plaintext into Input. Upper and lower case are kept.
  2. Enter a and b. Valid values of a within 0–25 are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.
  3. Record a and b with the ciphertext. Decode it with Affine Cipher Decoder and the same values to check the round trip.

Example

Input: Hello, World!

Settings: Affine Cipher Encoder with a = 5 and b = 8.

Result: Rclla, Oaplx!

Affine Cipher Encoder maps H (7) to 5 × 7 + 8 = 43, and 43 mod 26 = 17, which is R. The comma, space, and exclamation mark stay in place.

Limits

  • An even a, or a = 13, makes the mapping irreversible. The tool rejects those values.
  • Accented and non-Latin letters pass through unchanged.
  • With only 312 possible key pairs, anyone can try every key. The cipher gives no real secrecy.