Affine Cipher
Encrypt and decrypt text using the Affine cipher — a substitution cipher using modular arithmetic with keys a and b.
What Does This Tool Do?
The Affine cipher encrypts each letter using E(x) = (ax + b) mod 26, where a must be coprime with 26. It is a generalisation of the Caesar cipher (a=1) and Atbash cipher (a=25, b=25).
Key Features
Two Keys
Keys a (multiplicative) and b (shift).
Modular Arithmetic
E(x) = (ax+b) mod 26 formula shown.
Both Directions
Encrypt and decrypt.
Valid Key Check
Warns if a is not coprime with 26.
How to Use
- Enter your message.
- Select key a (must be coprime with 26) and enter key b (0–25).
- Click Encrypt or Decrypt.
Frequently Asked Questions
Why must key a be coprime with 26?▾
For decryption to work, the modular inverse of a must exist. A modular inverse of a mod 26 exists only when gcd(a, 26) = 1 — i.e. a is coprime with 26.
What is the formula?▾
Encryption: E(x) = (a×x + b) mod 26. Decryption: D(y) = a⁻¹×(y − b) mod 26, where a⁻¹ is the modular inverse of a.
What are the special cases?▾
a=1 gives a Caesar cipher (shift by b). a=25, b=25 gives an Atbash cipher. a=1, b=0 gives no encryption at all.