Logo

MonoCalc

/

Affine Cipher

Encode/Decode
Key a=7 is valid — gcd(7,26)=1 — a⁻¹ mod 26 = 15

Quick-select valid key a values:

Cipher Alphabet — a=7, b=3

PlainABCDEFGHIJKLMNOPQRSTUVWXYZ
CipherDKRYFMTAHOVCJQXELSZGNUBIPW
Enter some text above
Type or paste text in the input field to encrypt or decrypt it with the Affine cipher.

About This Tool

🔐 Affine Cipher – Classical Encryption with Modular Mathematics

The Affine cipher is a classical monoalphabetic substitution cipher that encrypts each letter in a message by applying a mathematical formula drawn from modular arithmetic. Unlike the simple Caesar cipher, which only shifts letters, the Affine cipher also scales them — producing a more complex substitution that is nonetheless fully reversible with the right keys.

The Encryption Formula

Each letter is first converted to its zero-indexed numeric position in the alphabet (A=0, B=1, …, Z=25). The encryption formula is then applied:

E(x) = (a · x + b) mod 26

where:
  x = numeric value of the plaintext letter (0–25)
  a = multiplicative key (must be coprime with 26)
  b = additive shift key (0–25)

For example, with a=7 and b=3, the letter H (x=7) encrypts as: (7×7 + 3) mod 26 = 52 mod 26 = 0 A.

Decryption and the Modular Inverse

Decryption reverses the transformation using the formula:

D(x) = a⁻¹ · (x − b) mod 26

where a⁻¹ is the modular multiplicative inverse of a modulo 26 — the unique value such that (a · a⁻¹) mod 26 = 1.

The tool automatically computes the modular inverse for you. For example, 7⁻¹ mod 26 = 15 because (7 × 15) mod 26 = 105 mod 26 = 1.

Valid Values for Key a

The multiplicative key a must satisfy gcd(a, 26) = 1 — it must be coprime with 26. This is required for the modular inverse to exist, which is necessary for decryption. There are exactly 12 valid values:

Key aa⁻¹ mod 26Key aa⁻¹ mod 26
11157
391723
5211911
715215
932317
11192525

Key Space and Security

The Affine cipher has 312 distinct key pairs (12 valid a values × 26 b values). Key pair (a=1, b=0) is the identity transformation (no encryption), and (a=1, b=n) degenerates to a Caesar cipher with shift n. The cipher is not secure by modern standards — it can be broken in milliseconds by brute force or letter frequency analysis. Use it for learning, puzzles, and educational demonstrations only.

Worked Example

Encrypting "HELLO" with a=7, b=3:

H (x=7):  (7×7 + 3) mod 26 = 52 mod 26 =  0  → A
E (x=4):  (7×4 + 3) mod 26 = 31 mod 26 =  5  → F
L (x=11): (7×11 + 3) mod 26 = 80 mod 26 =  2  → C
L (x=11): (7×11 + 3) mod 26 = 80 mod 26 =  2  → C
O (x=14): (7×14 + 3) mod 26 = 101 mod 26 = 23 → X

Result: HELLO → AFCCX

How This Tool Works

Enter your text, choose a valid key a (use the quick-select buttons for convenience), and set key b (0–25). Switch between Encrypt and Decrypt modes using the dropdown. The tool validates your key a in real time — a green indicator confirms it is coprime with 26. Enable Cipher Alphabet Table to see the full A–Z substitution mapping, or turn on Step-by-Step Breakdown to trace the arithmetic for each character. Non-alphabetic characters (spaces, digits, punctuation) are passed through unchanged.

Educational Applications

The Affine cipher is widely used in cryptography courses to introduce modular arithmetic, modular inverses, and the concept of a key space. Its structure makes it ideal for demonstrating why certain mathematical properties (like coprimality) are essential for reversible encryption, laying the groundwork for understanding modern ciphers such as RSA.

Frequently Asked Questions

Is the Affine Cipher free?

Yes, Affine Cipher is totally free :)

Can I use the Affine Cipher offline?

Yes, you can install the webapp as PWA.

Is it safe to use Affine Cipher?

Yes, any data related to Affine Cipher only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

What is the Affine cipher and how does it work?

The Affine cipher is a classical monoalphabetic substitution cipher that transforms each letter using the formula E(x) = (a·x + b) mod 26, where x is the letter's zero-indexed position (A=0…Z=25), a is a multiplicative key, and b is an additive shift. Every letter maps to exactly one other letter, making the output length identical to the input.

Why must key 'a' be coprime with 26?

For decryption to work, the modular inverse of a (denoted a⁻¹ mod 26) must exist. A modular inverse exists only when gcd(a, 26) = 1 — i.e., a and 26 share no common factors other than 1. The 12 valid values are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. Using any other value makes the cipher irreversible.

How is decryption performed?

Decryption uses the formula D(x) = a⁻¹ · (x − b) mod 26, where a⁻¹ is the modular multiplicative inverse of a modulo 26. For example, when a=7, a⁻¹=15 because (7·15) mod 26 = 1. This tool automatically computes the modular inverse so you only need to enter the original keys.

Is the Affine cipher secure for protecting real data?

No. The Affine cipher is a classical cipher with only 312 distinct key pairs (12 valid a values × 26 b values), making it trivially breakable by brute force or frequency analysis. It is intended for educational purposes, puzzles, games, and cryptography demonstrations — never for protecting sensitive information.

What happens to spaces, numbers, and punctuation?

Non-alphabetic characters (spaces, digits, punctuation) are passed through unchanged. Only the 26 letters A–Z are transformed. The tool also supports a 'Preserve Case' option that maintains the original casing of each letter in the output.

What is the Step-by-Step Breakdown feature?

The Step-by-Step Breakdown shows the full arithmetic for each letter: its zero-indexed value (x), the formula applied, the intermediate calculation, the result mod 26, and the output letter. This is especially useful for students learning modular arithmetic and classical cryptography concepts.