📐 Affine Cipher
Encrypt and decrypt text using the Affine cipher with custom a and b keys. E(x) = (a*x + b) mod 26.
About the Affine Cipher
The Affine cipher is a monoalphabetic substitution cipher that combines multiplication and addition. Each letter is mapped through the function E(x) = (a*x + b) mod 26, where x is the letter's position (A=0, B=1, ..., Z=25). Decryption uses the modular inverse of a: D(x) = a⁻¹ * (x - b) mod 26. This tool runs entirely in your browser — nothing is uploaded.
How it works
- Encrypt — Convert each letter to a number (A=0...Z=25), compute (a*x + b) mod 26, convert back to a letter.
- Decrypt — Compute a⁻¹ * (x - b) mod 26, where a⁻¹ is the modular inverse of a modulo 26.
- The key a must be coprime with 26 (i.e., not divisible by 2 or 13). Valid values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
- The key b can be any integer from 0 to 25.
- Letter case is preserved; non-letter characters are left unchanged.
Use cases
- Teaching modular arithmetic and modular inverses in cryptography.
- Demonstrating how monoalphabetic ciphers depend on bijective mappings.
- Creating math-flavored puzzles and educational challenges.
- Exploring why affine ciphers are still vulnerable to frequency analysis.
Frequently asked questions
Why must a be coprime with 26? If a shares a factor with 26, the mapping is no longer one-to-one and decryption becomes impossible. The valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.
Is the Affine cipher secure? No. With only 12 × 26 = 312 possible keys, it is easily brute-forced and remains vulnerable to frequency analysis.
What is a modular inverse? The modular inverse of a modulo 26 is the number a⁻¹ such that (a × a⁻¹) mod 26 = 1. For example, the inverse of 5 mod 26 is 21, because 5 × 21 = 105 = 4 × 26 + 1.
Does this tool upload my text? No. All encryption and decryption happens locally in your browser.