Converter Tools
Affine Cipher Encoder and Decoder
Encode and decode the affine cipher online. Set keys a and b, see the formula and modular inverse, and get an instant A-Z substitution map. No signup.
Settings
The affine cipher uses two keys. Key a is the multiplier and must be coprime with 26. Key b is the shift and can be any whole number.
The affine cipher only transforms the 26 Latin letters. Keep passes everything else through; Drop removes it.
Valid values for key a
Encode: E(x) = (5 * x + 8) mod 26
Decode: D(y) = 21 * (y - 8) mod 26
x is the letter position with A = 0 and Z = 25. The inverse of a = 5 modulo 26 is 21.
Encryption alphabet
Each plaintext letter on top maps to the cipher letter below it for the current keys.
What is the affine cipher?
The affine cipher is a monoalphabetic substitution cipher that encrypts each letter with a simple linear equation. Every letter is first turned into a number from 0 to 25 (A is 0, Z is 25). That number x is then replaced by (a times x, plus b) reduced modulo 26, where a and b are the two secret keys. The result is converted back to a letter. The shift key b works exactly like a Caesar cipher, while the multiplier key a scrambles the spacing between letters, so the affine cipher generalises and strengthens the Caesar shift. With a set to 1 the affine cipher is identical to a Caesar shift of b, which makes it a natural next step when learning classical cryptography.
Why key a must be coprime with 26
- Decryption has to undo the multiplication by a, which is only possible when a has a multiplicative inverse modulo 26. That requires the greatest common divisor of a and 26 to be 1.
- Only twelve multipliers satisfy that condition: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. Any other value of a makes two different letters encrypt to the same cipher letter, so the message can no longer be read back.
- The shift key b has no such restriction. It can be any whole number and is reduced modulo 26, giving 26 distinct shifts and a total of 312 usable affine keys.
- The affine cipher is a teaching cipher, not a secure one. With only 312 keys it is broken instantly by trying every key or by letter-frequency analysis.
How to use
- Choose Encode to turn plain text into cipher text, or Decode to turn cipher text back into plain text.
- Set key a (the multiplier) and key b (the shift). Key a must be one of 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, or 25; tap a chip to pick a valid value fast.
- Type or paste your text into the input box. The result updates instantly, and the formula panel shows the exact equation and the modular inverse used.
- Pick whether to preserve the letter case or force upper or lower case, and whether to keep or drop digits, spaces, and punctuation.
- Read the A-Z reference table to see how every letter maps for the current keys, then copy the output or use the swap button to feed it back as input. Everything runs locally in your browser.
About this tool
The Affine Cipher Encoder and Decoder converts text with the classical affine cipher, a monoalphabetic substitution that encrypts each letter using a short linear equation. Every letter is turned into a number from 0 to 25 (A is 0, Z is 25), then replaced by (a times that number, plus b) reduced modulo 26, where a and b are the two keys you choose. The multiplier a stretches the spacing between letters and the shift b slides the alphabet along, so the affine cipher generalises and strengthens the Caesar shift: setting a to 1 reproduces a plain Caesar shift of b, and the keys a equals 25 with b equals 25 reproduce the Atbash mirror. The tool encodes and decodes from a single panel. Decoding is not the same operation as encoding here, because undoing the multiplication requires the modular multiplicative inverse of a, so the tool computes that inverse for you and shows both the encryption formula E(x) = (a times x + b) mod 26 and the decryption formula D(y) = inverse of a times (y minus b) mod 26 with your live key values filled in. Key a has to be coprime with 26 for the cipher to be reversible, which leaves exactly twelve valid multipliers: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. If you enter any other value of a the tool explains why it cannot be reversed and lists the values you can use, and a row of quick-pick chips lets you set a valid multiplier in one tap. Key b can be any whole number and is reduced modulo 26 automatically. You can preserve the original letter case or force the output to upper or lower case, and you can keep digits, spaces, and punctuation verbatim or strip every non-letter. A full A-Z reference table shows exactly which cipher letter each plaintext letter maps to for the current keys, flipping to the reverse map when you switch to decode. Sample plain and cipher texts are one click away so you can see a verified round trip immediately, and the swap button feeds the output straight back as input to chain or reverse a conversion. The affine cipher is a teaching and puzzle cipher rather than a secure one: with only 312 possible keys it is broken instantly by trying every key or by letter-frequency analysis, which makes it a popular exercise in cryptography courses and a common step in capture-the-flag and escape-room puzzles. Everything runs in your browser. The text you enter is never uploaded, logged, or sent anywhere.
Free to use. Works in your browser. No signup, no login.
Related tools
You may also like
Caesar Cipher Encoder / Decoder
Encode, decode, or brute-force Caesar shift ciphers with ROT13 and frequency ranking.
Open tool
ConverterVigenere Cipher Encoder / Decoder
Encode or decode Vigenere ciphers with standard, Beaufort, and autokey variants.
Open tool
ConverterAtbash Cipher
Encode or decode Atbash cipher text in Latin or Hebrew with a live mapping table.
Open tool
ConverterA1Z26 Cipher
Bidirectional letter-to-number cipher with configurable letter and word separators.
Open tool
ConverterROT13 Encoder Decoder
ROT13, ROT5, ROT18, and ROT47 rotations with a live mapping view.
Open tool