Skip to main content

Affine Cipher

New

Encrypt and decrypt text with the Affine cipher, a mathematical substitution cipher using the formula E(x) = (a·x + b) mod 26, with validation that key A is coprime with 26.

affineciphercryptographymodular arithmeticclassical cipher
Read the guide
Shortcuts:⌘ KSearch

The Affine cipher applies the formula E(x) = (a·x + b) mod 26 to each letter's alphabet position, generalizing the Caesar cipher (which is the special case a=1). Key "a" must share no common factor with 26 (i.e. be coprime with it) for the cipher to be reversible — valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.

Continue with:

Frequently Asked Questions

How does the Affine cipher generalize the Caesar cipher?

The Caesar cipher is just a special case of the Affine cipher with key A fixed at 1 — the Affine cipher adds a second key (A) that multiplies each letter's alphabet position before adding the shift (B), producing a mathematically richer substitution than a simple fixed shift.

Why does key A have to be coprime with 26?

Decryption requires computing the modular multiplicative inverse of A modulo 26, which only exists if A shares no common factor with 26 — if A isn't coprime with 26 (for example, using an even number, or 13), multiple different letters would encrypt to the same ciphertext letter, making decryption ambiguous or impossible.

What are the valid values for key A?

Since 26 = 2 × 13, the valid coprime values for A are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25 — this tool automatically warns you if you enter a value outside this list.

What does key B control?

Key B is simply an additional shift amount (0-25) applied after the multiplication, similar to the single shift key in a Caesar cipher — together, A and B define the complete two-parameter transformation for every letter.

Is the Affine cipher secure for real-world use?

No — like all classical single-letter substitution ciphers, it's vulnerable to frequency analysis (since each plaintext letter always maps to exactly one ciphertext letter) and has at most 12 × 26 = 312 possible key combinations, making it trivial to brute-force with modern computing.

Related Tools