CacheHackCacheHack

Affine Cipher

Encode and decode using the Affine cipher. A mathematical cipher combining multiplication and addition.

Must be coprime with 26
0-25

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

  1. Convert each letter to its numerical value (A=0, B=1, ..., Z=25)
  2. Apply the formula: (a × x + b) mod 26
  3. Convert the result back to a letter

Decryption

  1. Find the modular multiplicative inverse of 'a' (called a⁻¹)
  2. Apply: a⁻¹ × (y - b) mod 26
  3. 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

aa⁻¹ (mod 26)
11
39
521
715
93
1119
157
1723
1911
215
2317
2525