Skip to main content
Substitution ciphers100% FreeInstant

Affine Cipher: Encrypt and Decrypt with Keys a and b

This free affine cipher tool encrypts and decrypts text with two numbers, a and b, using the formula (a × x + b) mod 26. It checks your key and shows the inverse used for decryption.

By Debmedia Technologies LLP · Kolkata, India · Updated

What Is It and When Do You Need It?

The affine cipher is a Caesar cipher with a multiplication added. Multiplying by a spreads the letters out, and adding b shifts them. It gives 312 possible keys, still far too few to be secure, but a good step up for teaching.

Enter text, the two keys and the direction.

How to Use This Tool

  1. Type or paste your text.
  2. Enter a valid key a and a key b.
  3. Choose encrypt or decrypt.
  4. Press Run.

How the Calculation Works

Letters are numbered 0 to 25. Encryption computes (a × x + b) mod 26. Decryption computes a⁻¹ × (y − b) mod 26, where a⁻¹ is the number that gives 1 when multiplied by a modulo 26. The tool tests that a and 26 share no factor.

Worked Example

With a = 5 and b = 8, AFFINECIPHER becomes IHHWVCSWFRCP, the standard example.

Common Uses

  • Teaching modular arithmetic.
  • Puzzles.
  • Studying cipher families.
  • Homework checks.

Tips and Common Mistakes

  • Pick a from the list of 12 allowed values.
  • a = 25 and b = 25 is the Atbash cipher.
  • Try brute force over all 312 keys by hand only for short texts.

Next, you may need the Caesar cipher, Atbash cipher, Vigenère cipher, A1Z26 cipher.

Everything runs in your browser. Nothing you type is uploaded, stored or sent to any outside service.

Frequently Asked Questions

A substitution cipher that maps each letter x to (a × x + b) mod 26, where a and b are the key numbers.
Only numbers with no common factor with 26: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Otherwise two letters would map to the same one.
With x = a⁻¹ × (y − b) mod 26, where a⁻¹ is the modular inverse of a. The tool finds it for you.
Then it is a Caesar cipher with shift b.

Disclaimer: results are calculated in your browser from the numbers you enter. Check important results before you rely on them for money, health, safety or legal decisions.