Affine Cipher Encoder & Decoder

Encrypt and decrypt text using this mathematical substitution cipher

?
?
Invalid coefficient value
Copied!
Affine Cipher Visualization Encryption Mode

Mathematical Formula:

E(x) = (ax + b) mod 26

The Affine cipher uses a mathematical formula where each letter is first converted to a number (A=0, B=1, ..., Z=25), then transformed using the formula, and finally converted back to a letter.

a
5
b
8
m
26

Character Mapping:

The Affine cipher is a type of monoalphabetic substitution cipher where each letter in the alphabet is mapped to its numeric equivalent, encrypted using a mathematical function, and then converted back to a letter.

What is the Affine Cipher?

The Affine cipher is a type of monoalphabetic substitution cipher where each letter in the plaintext is mapped to its numeric equivalent, transformed using a simple mathematical function, and then converted back to a letter.

How Does the Affine Cipher Work?

The Affine cipher uses a mathematical formula of the form:

E(x) = (ax + b) mod m
D(y) = a^(-1) × (y - b) mod m

Where:

  • x: is the numeric value of the plaintext letter (A=0, B=1, ..., Z=25)
  • a: is a coefficient that must be coprime with m (have no common factors with m)
  • b: is the shift value (any integer)
  • m: is the size of the alphabet (26 for English)
  • a^(-1): is the modular multiplicative inverse of a (i.e., a × a^(-1) ≡ 1 mod m)

For example, with a=5 and b=8, the letter 'C' (value 2) would be encrypted as:

  • E(2) = (5 × 2 + 8) mod 26 = 18 mod 26 = 18
  • 18 corresponds to 'S', so 'C' is encrypted as 'S'

Important Constraint

The value of 'a' must be chosen such that it is coprime with the modulus (26 for the English alphabet), which means they share no common factors other than 1. For m=26, valid values of 'a' are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.