BYTETOOLS

Affine Cipher

Encrypt and decrypt with the affine cipher E(x) = (ax + b) mod 26, see the substitution alphabet, and brute-force all 312 keys to crack a message.

5
a
8
b
21
a⁻¹ mod 26
0
Letters

Ciphertext

Formula in use

E(x) = (5·x + 8) mod 26

Letters are numbered A = 0 through Z = 25. Decryption needs a⁻¹, the number that multiplies with a to give 1 modulo 26 — here 5 × 21 = 105, and 105 mod 26 = 1.

Substitution alphabet

AI
BN
CS
DX
EC
FH
GM
HR
IW
JB
KG
LL
MQ
NV
OA
PF
QK
RP
SU
TZ
UE
VJ
WO
XT
YY
ZD

Top row plaintext, bottom row ciphertext, for the current key.

What is the Affine Cipher?

The ByteTools Affine Cipher tool applies the classic formula E(x) = (a·x + b) mod 26, where letters are numbered A = 0 through Z = 25.

  • Encrypt and decrypt with any of the 312 valid affine keys
  • Only coprime multipliers offered, so the cipher is always reversible
  • Shows a⁻¹ and the exact formula being applied
  • Full substitution alphabet rendered for the current key
  • Optional preservation of letter case, spaces and punctuation
  • Brute-force panel listing every possible decryption, computed offline

How to use the Affine Cipher

  1. 1

    Choose whether you are encrypting or decrypting.

  2. 2

    Pick the multiplier a from the twelve legal values, and type the shift b.

  3. 3

    Decide whether to keep the original letter case and the spaces and punctuation.

  4. 4

    Type or paste your text and read the result, the formula in use and the substitution alphabet.

  5. 5

    Do not know the key? Tick the brute-force box to list all 312 decryptions and find the readable one.

About the Affine Cipher

The ByteTools Affine Cipher tool applies the classic formula E(x) = (a·x + b) mod 26, where letters are numbered A = 0 through Z = 25. Decryption reverses it with D(y) = a⁻¹·(y − b) mod 26, using the modular inverse of a. Because the multiplier must be coprime with 26, only twelve values of a are legal — 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 — and the tool refuses anything else rather than producing a cipher you could never undo.

The affine cipher generalises several ciphers you already know: a = 1 makes it a plain Caesar shift, a = 1 with b = 13 gives ROT13, and a = 25 with b = 25 reproduces Atbash. Alongside the result you get the full substitution alphabet for the current key, so you can see exactly how each letter maps.

With only 312 possible keys the cipher is trivially breakable, and the built-in brute-force panel does exactly that: it lists every legal decryption of your text so you can spot the one that reads like English. Everything runs 100% locally in your browser and nothing you type is uploaded.

Frequently asked questions

How does the affine cipher work?

Each letter is turned into a number from 0 to 25, multiplied by a, shifted by b, and reduced modulo 26 back into a letter. Decryption multiplies by the modular inverse of a after subtracting b, which undoes the operation exactly.

Why can't a be any number?

The multiplier has to be coprime with 26, otherwise two different letters map to the same ciphertext letter and the message cannot be decrypted. That leaves exactly twelve legal values, which is why this tool only offers those.

How many affine cipher keys are there?

312 — twelve valid multipliers times twenty-six shifts. That is small enough to try every one instantly, which is what the brute-force panel on this page does.

Is the affine cipher the same as the Caesar cipher?

The Caesar cipher is the special case where a equals 1, so only the shift applies. The affine cipher adds a multiplication step, which scrambles the alphabet more thoroughly but still leaves letter frequencies intact.

How do you break an affine cipher without the key?

The simplest way is to try all 312 keys and read whichever output makes sense, which is what the brute-force panel on this page does. Frequency analysis also works: map the two commonest ciphertext letters onto E and T, and solving the resulting pair of simultaneous equations gives you a and b directly.

Related tools