Affine Cipher Math Calculator
Encode and decode affine cipher transformations c ≡ ax + b mod 26. This educational calculator shows the formula, result, and step-by-step interpretation.
Calculator
What this calculator teaches
The affine cipher shows why modular inverses matter for reversible transformations.
Use the result as a learning aid. For classwork, still show the formula and intermediate reasoning so the final answer is not just a black-box number.
How the Affine Cipher Uses Modular Arithmetic
The affine cipher transforms each letter number x with c ≡ ax + b (mod 26). Using A=0, B=1, …, Z=25, the multiplier a stretches the residue classes and b shifts them. The reduction modulo 26 wraps every result back into the alphabet.
Decoding requires the modular inverse of a. An inverse a⁻¹ exists only when gcd(a,26)=1, so valid multipliers include 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. If a shares a factor with 26, two or more plaintext letters can collapse to the same ciphertext value, making unique decoding impossible.
| Stage | Rule | Interpretation |
|---|---|---|
| Encode | c = (ax+b) mod 26 | Map plaintext x to ciphertext c |
| Inverse | aa⁻¹ ≡ 1 (mod 26) | Undo multiplication by a |
| Decode | x = a⁻¹(c-b) mod 26 | Recover the original letter number |
This is a classical substitution cipher and is useful for learning modular arithmetic, but it is not secure for modern cryptographic protection because letter frequencies and the small key space make it easy to attack.
Verification strategy: after encoding a letter, immediately decode it with the same key. A valid affine key should return the original residue for every x from 0 through 25. You can also test the key itself by checking a·a⁻¹ mod 26=1. These two checks catch the most common modular-arithmetic mistakes.
The shift b may be any integer because modulo 26 reduces it to one of 26 equivalent shifts. For example, b=34 and b=8 are identical because 34≡8 (mod 26). The multiplier behaves differently: only its residue class modulo 26 matters, but that residue must still be coprime to 26 for decoding to be possible.
Formula & Symbols
| Concept | Formula or rule |
|---|---|
| Encode | c ≡ ax + b (mod 26) |
| Decode | x ≡ a⁻¹(c-b) (mod 26) |
Worked example
Common mistakes
Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.
Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.
Values such as a=2 or a=13 share a factor with 26, so they do not have a modular inverse and cannot produce a one-to-one alphabet substitution.
This page uses the conventional numbering A=0 through Z=25. Changing the numbering changes the arithmetic and must be done consistently.
FAQ
Related calculators
These links will work after the calculators are registered in the final Math layout update.
Formula Explorer connections
Interpretation: This formula uses integer divisibility, modular arithmetic, finite fields or coding relationships. Assumption: Inputs are usually integers with specific modulus or coprimality requirements. Cryptographic examples are educational and not a substitute for vetted security libraries.