Affine Cipher
Encode and decode using the Affine cipher. A mathematical cipher combining multiplication and addition.
Affine Cipher Formula
Encrypt: E(x) = (5x + 8) mod 26
Decrypt: D(x) = 21(x - 8) mod 26
Common Key Combinations
What is the Affine Cipher?
The Affine cipher is a type of monoalphabetic substitution cipher that uses a mathematical formula to encrypt each letter. It combines multiplication and addition modulo 26, making it more secure than simple substitution ciphers like Caesar.
The encryption formula is: E(x) = (ax + b) mod 26
Where 'a' and 'b' are the keys, and 'x' is the letter's position (A=0, B=1, etc.).
How the Affine Cipher Works
Encryption
- Convert each letter to its numerical value (A=0, B=1, ..., Z=25)
- Apply the formula: (a × x + b) mod 26
- Convert the result back to a letter
Decryption
- Find the modular multiplicative inverse of 'a' (called a⁻¹)
- Apply: a⁻¹ × (y - b) mod 26
- Convert the result back to a letter
Key Requirements
The key 'a' must be coprime with 26 (their greatest common divisor is 1). This ensures that every letter maps to a unique ciphertext letter and decryption is possible.
Valid values for 'a': 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25
The key 'b' can be any number from 0 to 25 (the shift amount).
Special Cases
- a=1: Reduces to Caesar cipher (just a shift)
- a=1, b=0: Identity (no change)
- a=25, b=25: Equivalent to Atbash cipher
Affine Cipher in Geocaching
The Affine cipher appears in geocaching puzzles because:
- Mathematical: Appeals to puzzle-makers who like math
- Two keys: More complex than simple shift
- Brute-forceable: Only 312 possible key combinations (12 × 26)
- Educational: Teaches modular arithmetic concepts
Breaking the Affine Cipher
Without the key, you can try:
- Brute force: Only 312 combinations to try
- Frequency analysis: Most common letters are E, T, A, O
- Known plaintext: If you know any word, solve for a and b
- Pattern analysis: Look for common words like THE, AND
Mathematical Background
The Affine cipher uses modular arithmetic. The modular multiplicative inverse a⁻¹ satisfies: (a × a⁻¹) mod 26 = 1
| a | a⁻¹ (mod 26) |
|---|---|
| 1 | 1 |
| 3 | 9 |
| 5 | 21 |
| 7 | 15 |
| 9 | 3 |
| 11 | 19 |
| 15 | 7 |
| 17 | 23 |
| 19 | 11 |
| 21 | 5 |
| 23 | 17 |
| 25 | 25 |