Affine Cipher
Encrypt and decrypt text using the Affine monoalphabetic substitution cipher with configurable key parameters.
What is the Affine Cipher?
The Affine Cipher is a classic monoalphabetic substitution cipher that uses a mathematical formula to transform each letter of the plaintext into a corresponding ciphertext letter. Unlike the simple Caesar cipher, which only shifts letters, the Affine cipher applies both a multiplication and an addition to the letter's numeric position, making it a more complex classical encryption scheme.
Despite its added complexity over simpler substitution ciphers, the Affine cipher is still vulnerable to frequency analysis and is not suitable for modern cryptographic use. It is primarily studied for its historical and educational value.
How Does the Affine Cipher Work?
Each letter in the alphabet is assigned a numeric value: A = 0, B = 1, C = 2, …, Z = 25. The cipher then applies two keys:
- Key a — a multiplicative key that must be coprime with 26 (i.e., GCD(a, 26) = 1). Valid values are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
- Key b — an additive shift key, ranging from 0 to 25.
Encryption Formula
To encrypt a letter with numeric position \(x\):
Decryption Formula
To decrypt a ciphertext letter with numeric position \(y\):
Where \(a^{-1}\) is the modular multiplicative inverse of \(a\) modulo 26 — the unique number such that \(a \cdot a^{-1} \equiv 1 \pmod{26}\).
Step-by-Step Example (a=5, b=8)
Let's encrypt the word "HELLO" using key a=5 and key b=8:
H(7) → (5·7 + 8) mod 26 = 43 mod 26 = 17 = RE(4) → (5·4 + 8) mod 26 = 28 mod 26 = 2 = CL(11) → (5·11 + 8) mod 26 = 63 mod 26 = 11 = LL(11) → (5·11 + 8) mod 26 = 63 mod 26 = 11 = LO(14) → (5·14 + 8) mod 26 = 78 mod 26 = 0 = A
The encrypted result is "RCLLA".
Why Must Key 'a' Be Coprime with 26?
The requirement that GCD(a, 26) = 1 ensures that each letter maps to a unique ciphertext letter and the mapping is a bijection (one-to-one correspondence). If 'a' shared a common factor with 26 (the alphabet size), multiple plaintext letters would map to the same ciphertext letter, making decryption impossible because the mapping would not be reversible.
Mathematically, for the decryption formula to work, 'a' must have a modular multiplicative inverse modulo 26. This inverse only exists when GCD(a, 26) = 1.
Historical Context
The Affine cipher is a generalization of both the Caesar cipher (where a=1) and the Atbash cipher (where a=25, b=25). By varying both keys, there are 12 × 26 = 312 unique Affine cipher configurations possible over the 26-letter English alphabet. While this seems like a significant key space compared to the Caesar cipher's 26 variants, a ciphertext-only attack using frequency analysis can still break it quickly.
Frequently Asked Questions
Frequently Asked Questions
What values are valid for key 'a' in the Affine cipher?
Key 'a' must be coprime with 26, meaning GCD(a, 26) = 1. The valid values for the standard 26-letter English alphabet are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25 — giving 12 possible values. Note that a=1 reduces the Affine cipher to a simple Caesar cipher.
How do I decrypt an Affine cipher message?
To decrypt, you use the formula D(y) = a⁻¹ · (y − b) mod 26, where a⁻¹ is the modular multiplicative inverse of key 'a' modulo 26. Our tool handles this automatically — simply switch the operation mode to "Decrypt (Decode)" and enter the same 'a' and 'b' key values that were used to encrypt the message.
Is the Affine cipher secure for protecting sensitive information?
No. The Affine cipher is not secure for modern cryptography. Its key space of 312 combinations is trivially small, and the cipher is easily broken using frequency analysis — by comparing the frequency of letters in the ciphertext to known letter frequency distributions in natural language. It should only be used for educational purposes or simple puzzles.
What is the difference between the Affine cipher and the Caesar cipher?
The Caesar cipher is a special case of the Affine cipher where key 'a' is always 1. This means the Caesar cipher only applies an additive shift (b), while the Affine cipher also applies a multiplicative transformation (a). The Affine cipher is therefore more complex, offering more possible key combinations.
Does the Affine cipher work with spaces, numbers, and punctuation?
By default, the Affine cipher only transforms alphabetic characters (A–Z). Spaces, numbers, and punctuation marks are passed through unchanged. You can use the "Strict" case strategy option in our tool to strip non-alphabetic characters entirely from the output.
How many unique Affine cipher keys are there for the English alphabet?
There are 12 valid values for key 'a' (those coprime with 26) and 26 possible values for key 'b' (0 through 25). This yields 12 × 26 = 312 unique Affine cipher key combinations for the standard 26-letter English alphabet.
Related tools
Your recent visits