UTILS.
100% in-browser
✖️

Affine Cipher Encoder / Decoder

Monoalphabetic cipher using E(x)=(a·x+b) mod 26, with key validation (gcd(a,26)=1) and modular-inverse decoding. Encode or decode letters instantly.

Key a must be coprime with 26: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, or 25.

— output appears here —

About this tool

The Affine Cipher Encoder / Decoder is a free, in-browser tool for the monoalphabetic cipher built on modular arithmetic. Letters A–Z are numbered 0–25 and each is transformed by the linear function E(x) = (a·x + b) mod 26, where a and b are the two key values. Decoding inverts this with D(y) = a⁻¹·(y − b) mod 26, where a⁻¹ is the modular multiplicative inverse of a modulo 26.

For the cipher to be reversible, the multiplier a must be coprime with 26 — that is, gcd(a, 26) = 1. The twelve valid choices are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25; any other value would map two different letters to the same output and cannot be decoded, so the tool rejects it with a clear message. The additive key b may be any value 0–25. Everything is computed locally in your browser, so nothing you type leaves your device.

Case is preserved and non-letters pass through unchanged. The Affine cipher generalises simpler classical ciphers: setting a = 1 reduces it to a Caesar shift of b, and a = 25, b = 25 reproduces the Atbash cipher. It is a staple of discrete-mathematics and number-theory courses because it makes the ideas of coprimality and modular inverses concrete, and it shows up regularly in CTF puzzles and tools such as CyberChef.

Frequently asked questions

What is the affine cipher formula?
Encoding is E(x) = (a·x + b) mod 26 and decoding is D(y) = a⁻¹·(y − b) mod 26, with letters numbered 0–25. Here a⁻¹ is the modular multiplicative inverse of a modulo 26.
Which values of a are allowed?
Only values coprime with 26, i.e. gcd(a, 26) = 1: that is 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, or 25. Other values make the cipher non-invertible, so the tool rejects them.
How is decoding done?
It needs the modular inverse a⁻¹ of a modulo 26, the number where a·a⁻¹ ≡ 1 (mod 26). The tool finds it automatically, then applies D(y) = a⁻¹·(y − b) mod 26 to each letter.
How does it relate to Caesar and Atbash?
It generalises both. With a = 1 the affine cipher becomes a Caesar shift of b, and with a = 25, b = 25 it reproduces the Atbash cipher. It is the linear form of a monoalphabetic substitution.

More tools