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.

CryptographyDiscrete Math

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.

StageRuleInterpretation
Encodec = (ax+b) mod 26Map plaintext x to ciphertext c
Inverseaa⁻¹ ≡ 1 (mod 26)Undo multiplication by a
Decodex = a⁻¹(c-b) mod 26Recover 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

ConceptFormula or rule
Encodec ≡ ax + b (mod 26)
Decodex ≡ a⁻¹(c-b) (mod 26)

Worked example

Example: Use a value coprime to 26 so the cipher can be decoded.
Example 2: With a=5 and b=8, A has x=0, so c=(5·0+8) mod 26=8, which is I.
Example 3: With a=5 and b=8, H has x=7. Then c=(35+8) mod 26=17, giving R.
Example 4: For a=5, the inverse modulo 26 is 21 because 5·21=105≡1 (mod 26).
Example 5: To decode I (c=8) with a=5,b=8: x=21(8−8) mod 26=0, so the plaintext is A.

Common mistakes

⚠️
Using the wrong input format

Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.

⚠️
Ignoring assumptions

Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.

⚠️
Choosing a noninvertible multiplier

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.

⚠️
Using A=1 instead of A=0

This page uses the conventional numbering A=0 through Z=25. Changing the numbering changes the arithmetic and must be done consistently.

FAQ

Can I use decimals?
Most numerical calculators allow decimals, but modular arithmetic and coding-theory tools usually require integers or binary strings.
Is this for homework checking?
Yes. The page is designed to show both the answer and the reasoning pattern.
Why does the result sometimes say approximate?
Some probability, floating-point, and numerical methods naturally produce approximations.
Why must gcd(a,26)=1?
Because decoding needs a modular inverse of a. That inverse exists modulo 26 exactly when a and 26 are coprime.
What happens to spaces and punctuation?
The calculator transforms A–Z letters and leaves nonletter characters unchanged.
Is the affine cipher secure today?
No. It is educational classical cryptography, not a modern security method.
How many valid multipliers are there modulo 26?
There are φ(26)=12 multipliers coprime to 26, and each can be paired with any of 26 shifts.

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.

Caesar Cipher Math Calculator →Checksum Calculator →Chinese Remainder Theorem Calculator →Math Formula Explorer →