Get in touch

Contact Form Demo

Affine Cipher

Linear Substitution Cipher Calculator

Status: Ready
Encrypt
Decrypt
Message (A-Z)
Slope (a)
Intercept (b)
1
2
3
4
5
6
7
8
9
0
NEXT
Result Output
Cipher Mapping (x → y)
Process Details
👨‍🏫
By Prof. David Anderson
Cryptography Specialist | 20+ Years Exp.
"Welcome to the elegant world of **Classical Cryptography**. The Affine Cipher is not just a historical curiosity; it is the perfect sandbox for understanding **Linear Congruence** and **Modular Inverses**. Unlike the Caesar Cipher which only 'shifts', the Affine Cipher 'scales' and 'shifts'. Today, we will dissect the math behind $(ax + b) \pmod{26}$, learn why choosing the wrong key breaks the cipher, and see how easily it can be cracked using brute force."

Affine Cipher Calculator & Guide

The Math of Encryption, Decryption & Linear Congruence

1. The Mathematical Engine: Linear Congruence

The Affine Cipher is a type of Monoalphabetic Substitution Cipher. It maps each letter of the alphabet to a numeric value ($A=0, B=1, \dots, Z=25$) and transforms it using a linear equation.

The Encryption Formula

$$ E(x) = (ax + b) \pmod{26} $$
  • $x$: The numeric value of the plaintext letter.
  • $a$: The multiplicative key (Slope). Must be coprime to 26.
  • $b$: The additive key (Intercept). Any integer 0-25.
  • $m$: The size of the alphabet (26).

The Decryption Formula

Decryption is harder. You cannot simply divide. In modular arithmetic, we multiply by the Modular Multiplicative Inverse ($a^{-1}$).

$$ D(x) = a^{-1}(x - b) \pmod{26} $$

2. The "Coprime" Trap: Why Key A Matters

Many students ask: "Why can't I use 4 as Key A?"

For the cipher to be reversible (i.e., for decryption to work), the function must be a Bijection (one-to-one mapping). This only happens if $gcd(a, 26) = 1$.

Valid vs Invalid Keys for A:

Since $26 = 2 \times 13$, any even number (2, 4, 6...) or multiple of 13 is forbidden.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
19
21
23
25

3. Manual Calculation Example

Let's encrypt the letter "H" using Key $a=5, b=8$.

Step 1: Convert to Number

"H" is the 8th letter, but since we start at 0 (A=0), $x = 7$.

Step 2: Apply Formula

$$ E(7) = (5 \times 7 + 8) \pmod{26} $$ $$ E(7) = (35 + 8) \pmod{26} $$ $$ E(7) = 43 \pmod{26} $$ $$ 43 = 26 \times 1 + 17 \implies \text{Result is } 17 $$

Step 3: Convert Back

Number 17 corresponds to the letter "R". So, H becomes R.

Step 4: Verification (Decryption)

To decrypt, we first find $a^{-1}$ where $5 \times a^{-1} \equiv 1 \pmod{26}$.
The inverse of 5 is 21 (since $5 \times 21 = 105 = 4 \times 26 + 1$).

$$ D(17) = 21(17 - 8) \pmod{26} $$ $$ D(17) = 21(9) \pmod{26} $$ $$ D(17) = 189 \pmod{26} $$ $$ 189 = 26 \times 7 + 7 \implies \text{Result is } 7 \text{ ("H")} $$

4. Cracking the Code: Brute Force & Frequency Analysis

Is the Affine Cipher secure? Absolutely not. It is vulnerable to two major attacks.

A. Brute Force Attack

The key space is incredibly small.
Possible values for $a$: 12
Possible values for $b$: 26
Total Keys: $12 \times 26 = 312$.
A human can check 312 keys in an hour; a computer does it in microseconds.

B. Frequency Analysis

Since it is a monoalphabetic substitution, the frequency of letters is preserved.
If the most common letter in the ciphertext is 'X', and we know 'E' is the most common letter in English, we can assume:
$E(x_E) = x_X$
We can build a system of linear equations to solve for $a$ and $b$ instantly.

5. Developer's Corner: Python Implementation

Want to build your own solver? Here is the core logic for the Modular Inverse in Python 3.

def mod_inverse(a, m):
  # Extended Euclidean Algorithm
  for x in range(1, m):
    if ((a % m) * (x % m)) % m == 1:
      return x
  return -1

# Example usage for decryption
key_a = 5
key_b = 8
inv_a = mod_inverse(key_a, 26) # Returns 21

6. Professor's FAQ Corner

Q: Is Affine Cipher the same as Caesar Cipher?
The Caesar Cipher is a special case of the Affine Cipher where $a=1$. The formula becomes $1x + b$, which is just a shift. Affine is more complex because of the scaling factor $a$.
Q: Can I use negative numbers for B?
Yes! In modular arithmetic, negative numbers wrap around. $-1 \equiv 25 \pmod{26}$. So a shift of $b=-1$ is mathematically identical to $b=25$.
Q: How do I find the inverse of 'a' without a computer?
Use the Extended Euclidean Algorithm. It allows you to write $1 = ax + 26y$. The coefficient $x$ (modulo 26) is your inverse.
Q: What happens if b = 0?
If $b=0$, the function becomes $E(x) = ax \pmod{26}$. This is called a Linear Cipher. It maps 'A' to 'A' (0 to 0) always, which is a significant weakness.
Q: How do I handle negative numbers in modulo?
Standard modulo operators in programming (like `%` in JS or C) often return negative remainders. The correct formula is: `((n % m) + m) % m`. For example, $-3 \pmod{26} = 23$.
Q: Is the Affine Cipher used in modern security?
No. It is insecure. However, the concept of **Linear Congruential Generators (LCG)**, which uses the formula $X_{n+1} = (aX_n + b) \pmod m$, is widely used for generating pseudo-random numbers.

References

  • Stinson, D. R. (2005). Cryptography: Theory and Practice. CRC Press.
  • Singh, S. (1999). The Code Book: The Science of Secrecy. Fourth Estate.
  • Rosen, K. H. (2011). Elementary Number Theory and Its Applications. Pearson.

Ready to Compute?

Start calculating your own cipher shifts now.

Calculate