Skip to content

↑↓ navigate ↵ open Esc close Search runs locally

Affine Cipher: Encoder, Decoder & Solver

Encrypt or decrypt with the affine cipher, E(x) = (ax + b) mod 26. Choose the keys a and b, or let the solver try all 312 keys and rank them by how English the result looks.

  • Runs in your browser
  • Nothing is uploaded
  • Free, no sign-up
0 chars
Affine
Runs in your browser. Nothing is uploaded. Tip: Decrypt tries all 312 keys

How to use the Affine cipher tool#

  1. Choose Encrypt or Decrypt.
  2. Pick a (only values that share no factor with 26 are allowed: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25) and b (0 to 25).
  3. Type or paste your text. Letters are changed; case, spaces, digits and punctuation are kept.
  4. Don't know the keys? In Decrypt mode, the list under the tool shows the 10 most likely of all 312 keys. Click one to apply it.

Examples#

Encrypt with a = 5, b = 8

Input
AFFINE CIPHER
Output
IHHWVC SWFRCP

A (0) → 5·0 + 8 = 8 = I; F (5) → 5·5 + 8 = 33 mod 26 = 7 = H.

Decrypt with the same keys

Input
IHHWVC SWFRCP
Output
AFFINE CIPHER

Decryption multiplies by 21, the inverse of 5 modulo 26: 5 × 21 = 105 = 4 × 26 + 1.

a = 1 is a Caesar cipher

Input
attack at dawn
Output
dwwdfn dw gdzq

With a = 1, the affine cipher is just a shift by b, the Caesar cipher.

What is the affine cipher?#

The affine cipher turns each letter into a number, multiplies it by a key a, adds a key b, and takes the result modulo 26. It is a monoalphabetic substitution cipher: each letter always maps to the same ciphertext letter, but the mapping is scrambled by multiplication instead of a simple shift. The Caesar cipher (a = 1) and the Atbash cipher (a = 25, b = 25) are both special cases.

How it works#

With A = 0, B = 1 … Z = 25:

plaintext
encrypt:  E(x) = (a·x + b) mod 26decrypt:  D(y) = a⁻¹ · (y − b) mod 26

a⁻¹ is the modular inverse of a: the number that gives 1 when multiplied by a modulo 26. It only exists when a and 26 share no common factor, which is why a cannot be even or 13.

a1357911151719212325
a⁻¹1921153197231151725

With a = 2, for example, both A (0) and N (13) would encrypt to the same letter, so the message could not be decrypted.

How many keys are there?#

There are 12 valid values of a and 26 values of b, so 12 × 26 = 312 keys. One of them (a = 1, b = 0) leaves the text unchanged. That is small enough to try every key, which is exactly what the solver on this page does: it decrypts the message with all 312 keys and ranks the results with a chi-squared test against normal English letter frequencies.

With a known plaintext, it is even easier: two known letter pairs give two equations, which can be solved for a and b directly.

The affine cipher offers no real security. With only 312 keys and the same letter frequencies as the plaintext, it falls to brute force or frequency analysis in seconds.

Affine cipher in code#

javascript
const inverse = (a) => [...Array(26).keys()].find((x) => (a * x) % 26 === 1); function affine(text, a, b, decrypt = false) {  const ai = inverse(a);  return text.replace(/[a-z]/gi, (c) => {    const base = c <= 'Z' ? 65 : 97;    const x = c.charCodeAt(0) - base;    const y = decrypt ? (ai * (x - b + 26)) % 26 : (a * x + b) % 26;    return String.fromCharCode(base + y);  });} affine('AFFINE CIPHER', 5, 8);        // "IHHWVC SWFRCP"affine('IHHWVC SWFRCP', 5, 8, true);  // "AFFINE CIPHER"

Common errors and fixes#

  • Why can't I choose a = 2, 4, 13 or 26?

    *a* must have no common factor with 26, otherwise two letters encrypt to the same letter and decryption is impossible. Only 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 work.

  • Decryption gives nonsense with the right a and b

    Check the letter numbering. This tool uses A = 0. Some textbooks use A = 1, which gives different results with the same keys.

  • The solver's top result is wrong

    Short messages do not have reliable letter frequencies. Look further down the list of 10 candidates for the readable one.

Frequently asked questions#

How do I decrypt an affine cipher without the key?

Choose Decrypt and paste the message. The tool tries all 312 keys and lists the 10 most English-looking results. Click one to apply its a and b.

What is the formula for the affine cipher?

Encryption is E(x) = (ax + b) mod 26, and decryption is D(y) = a⁻¹(y − b) mod 26, where a⁻¹ is the modular inverse of a.

Is the Caesar cipher an affine cipher?

Yes. With a = 1 the affine cipher is a shift by b, which is the Caesar cipher. Atbash is also affine, with a = 25 and b = 25.

How many keys does the affine cipher have?

312: 12 valid values of a times 26 values of b.

Is my text uploaded?

No. Everything runs in your browser.

Your next tool is one search away.

Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.

Browse all tools

Tip: press Ctrl D to bookmark dev247.