CipherMatrix

Affine Cipher Calculator

Encrypt or decrypt text with the key (a, b), see why a must be coprime to 26, and follow the modular inverse and each letter calculation.

What is the affine cipher?

The affine cipher is a substitution cipher that generalises the Caesar cipher. Each letter is first converted to a number (A = 0, …, Z = 25), then multiplied by a key number a, shifted by a second key number b, and reduced modulo 26: E(x) = (a·x + b) mod 26. The Caesar cipher is the special case a = 1.

The key is the pair (a, b). Since b can be any of the 26 values and a can only be one of 12 values, the cipher has 12 × 26 = 312 possible keys.

Why a must be coprime to 26

Encryption must be reversible, which means different letters have to produce different cipher letters. If a shares a factor with 26, that fails. Take a = 2: the letters A (0) and N (13) give 2·0 + b = b and 2·13 + b = 26 + b, which are both b modulo 26, so two letters collide and the receiver cannot tell which one was sent.

The condition is gcd(a, 26) = 1, so a must be odd and not a multiple of 13. The twelve valid values are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Their count is Euler's totient φ(26) = 12.

Decryption and the modular inverse

To undo the encryption, solve y = a·x + b for x: D(y) = a⁻¹·(y − b) mod 26, where a⁻¹ is the number with a·a⁻¹ ≡ 1 (mod 26). It is found with the extended Euclidean algorithm. For a = 5 the algorithm gives 5·(−5) + 26·1 = 1, so a⁻¹ = −5 mod 26 = 21; check: 5·21 = 105 = 4·26 + 1.

The inverses of the twelve valid values are: 1 → 1, 3 → 9, 5 → 21, 7 → 15, 9 → 3, 11 → 19, 15 → 7, 17 → 23, 19 → 11, 21 → 5, 23 → 17 and 25 → 25.

Special cases and security

With a = 25 and b = 25 the formula becomes 25·x + 25 ≡ 25 − x (mod 26), which reverses the alphabet: this is the Atbash cipher. The affine cipher is easy to break: there are only 312 keys, letter frequencies are preserved, and just two known plaintext letters are enough to solve for a and b.

Worked examples

These examples are computed by the same engine as the calculator above, so you can compare each step with your own working.

a = 5, b = 8

  • Mode: Encrypt
  • Text: AFFINE CIPHER
  • Key a: 5
  • Key b: 8
Ciphertext
IHHWVC SWFRCP
key (a, b) = (5, 8), a⁻¹ = 21

Step-by-step solution

Formula used
E(x) = (a·x + b) mod 26
A = 0, …, Z = 25; a = 5, b = 8
  1. NoteNumber the letters
    A = 0, B = 1, …, Z = 25. Letters keep their case; other characters are copied unchanged.
    Key: a = 5, b = 8.
  2. CheckCheck that a is allowed: gcd(a, 26) = 1
    gcd(5, 26) = 1 ✓
    Valid values of a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 (these are the φ(26) = 12 numbers coprime to 26).
  3. CalculateFind a⁻¹ mod 26 with the extended Euclidean algorithm
    a·s + 26·t = gcd(a, 26) = 1 → a⁻¹ = s mod 26
    Bézout: 5·(-5) + 26·(1) = 1
    a⁻¹ = -5 mod 26 = 21
    Check: 5 · 21 = 105 = 4·26 + 1 ≡ 1 (mod 26) ✓
    iqrst
    0—510
    1—2601
    20510
    351-51
    45026-5
  4. CalculateEncrypt every letter
    y = (a·x + b) mod 26
    #Letterxa·x + bmod 26 = yResult
    1A05·0 + 8 = 88I
    2F55·5 + 8 = 337H
    3F55·5 + 8 = 337H
    4I85·8 + 8 = 4822W
    5N135·13 + 8 = 7321V
    6E45·4 + 8 = 282C
    7C25·2 + 8 = 1818S
    8I85·8 + 8 = 4822W
    9P155·15 + 8 = 835F
    10H75·7 + 8 = 4317R
    11E45·4 + 8 = 282C
    12R175·17 + 8 = 9315P
  5. CheckVerify by reversing
    Decrypting the result with the same key gives back the original text: ✓ yes
  6. ResultCiphertext
    IHHWVC SWFRCP

Decrypt IHHWVCSWFRCP

  • Mode: Decrypt
  • Text: IHHWVCSWFRCP
  • Key a: 5
  • Key b: 8
Plaintext
AFFINECIPHER
key (a, b) = (5, 8), a⁻¹ = 21

Step-by-step solution

Formula used
D(y) = a⁻¹·(y − b) mod 26
a⁻¹ is the inverse of a modulo 26: a·a⁻¹ ≡ 1 (mod 26)
A = 0, …, Z = 25; a = 5, b = 8, a⁻¹ = 21
  1. NoteNumber the letters
    A = 0, B = 1, …, Z = 25. Letters keep their case; other characters are copied unchanged.
    Key: a = 5, b = 8.
  2. CheckCheck that a is allowed: gcd(a, 26) = 1
    gcd(5, 26) = 1 ✓
    Valid values of a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 (these are the φ(26) = 12 numbers coprime to 26).
  3. CalculateFind a⁻¹ mod 26 with the extended Euclidean algorithm
    a·s + 26·t = gcd(a, 26) = 1 → a⁻¹ = s mod 26
    Bézout: 5·(-5) + 26·(1) = 1
    a⁻¹ = -5 mod 26 = 21
    Check: 5 · 21 = 105 = 4·26 + 1 ≡ 1 (mod 26) ✓
    iqrst
    0—510
    1—2601
    20510
    351-51
    45026-5
  4. CalculateDecrypt every letter
    x = a⁻¹·(y − b) mod 26
    #Letteryy − ba⁻¹·(y − b)mod 26 = xResult
    1I88 − 8 = 021·(0) = 00A
    2H77 − 8 = -121·(-1) = -215F
    3H77 − 8 = -121·(-1) = -215F
    4W2222 − 8 = 1421·(14) = 2948I
    5V2121 − 8 = 1321·(13) = 27313N
    6C22 − 8 = -621·(-6) = -1264E
    7S1818 − 8 = 1021·(10) = 2102C
    8W2222 − 8 = 1421·(14) = 2948I
    9F55 − 8 = -321·(-3) = -6315P
    10R1717 − 8 = 921·(9) = 1897H
    11C22 − 8 = -621·(-6) = -1264E
    12P1515 − 8 = 721·(7) = 14717R
  5. CheckVerify by reversing
    Encrypting the result with the same key gives back the original text: ✓ yes
  6. ResultPlaintext
    AFFINECIPHER

Atbash (a = b = 25)

  • Mode: Encrypt
  • Text: WIZARD
  • Key a: 25
  • Key b: 25
Ciphertext
DRAZIW
key (a, b) = (25, 25), a⁻¹ = 25
a = 25, b = 25 is the Atbash cipher: the alphabet is reversed (A ↔ Z, B ↔ Y, …).

Step-by-step solution

Formula used
E(x) = (a·x + b) mod 26
A = 0, …, Z = 25; a = 25, b = 25
  1. NoteNumber the letters
    A = 0, B = 1, …, Z = 25. Letters keep their case; other characters are copied unchanged.
    Key: a = 25, b = 25.
  2. CheckCheck that a is allowed: gcd(a, 26) = 1
    gcd(25, 26) = 1 ✓
    Valid values of a: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25 (these are the φ(26) = 12 numbers coprime to 26).
  3. CalculateFind a⁻¹ mod 26 with the extended Euclidean algorithm
    a·s + 26·t = gcd(a, 26) = 1 → a⁻¹ = s mod 26
    Bézout: 25·(-1) + 26·(1) = 1
    a⁻¹ = -1 mod 26 = 25
    Check: 25 · 25 = 625 = 24·26 + 1 ≡ 1 (mod 26) ✓
    iqrst
    0—2510
    1—2601
    202510
    311-11
    425026-25
  4. CalculateEncrypt every letter
    y = (a·x + b) mod 26
    #Letterxa·x + bmod 26 = yResult
    1W2225·22 + 25 = 5753D
    2I825·8 + 25 = 22517R
    3Z2525·25 + 25 = 6500A
    4A025·0 + 25 = 2525Z
    5R1725·17 + 25 = 4508I
    6D325·3 + 25 = 10022W
  5. CheckVerify by reversing
    Decrypting the result with the same key gives back the original text: ✓ yes
  6. ResultCiphertext
    DRAZIW

Frequently asked questions

How many keys does the affine cipher have?

There are 12 valid choices for a and 26 for b, so 312 keys in total. The identity key a = 1, b = 0 leaves the text unchanged.

Why does the calculator reject even numbers and 13 for a?

Those values share a factor with 26, so the encryption would map two letters to the same cipher letter and could not be decrypted.

What is the Atbash cipher?

Atbash maps A to Z, B to Y and so on. It is the affine cipher with a = 25 and b = 25.

How do I find the modular inverse of a by hand?

Either use the extended Euclidean algorithm on a and 26, as shown in the step table, or look for the number between 1 and 25 whose product with a leaves remainder 1 when divided by 26.

Can the affine cipher be broken with two known letters?

Yes. Two plaintext and ciphertext pairs give two linear congruences in a and b, which can be solved modulo 26.

Related calculators