Affine Cipher
Use multiplication and addition modulo 26 to build a complete substitution alphabet.
What is the Affine Cipher?
The Affine cipher converts each letter to a number from 0 to 25, applies a linear formula modulo 26, and converts the result back to a letter.
The multiplier a must have a multiplicative inverse modulo 26. That is why only values relatively prime to 26 are valid.
The cipher mechanics on this page are self-contained. Science Olympiad event formats, allowed variants, and tournament constraints can change by season; the current official Rules Manual and official clarifications take precedence.
No outside reference is assumed. Work through Foundations → Complete Reference → Encryption → Decryption in order, then use the competition and cryptanalysis sections.
What you need to know
- Use A=0, B=1, …, Z=25.
- Encryption: E(x) = (ax + b) mod 26 .
- Decryption: D(y) = a⁻¹(y − b) mod 26 .
- Only multipliers a with gcd(a,26)=1 are valid.
Beginner glossary
| Term | Meaning |
|---|---|
| Letter value | The number assigned to a letter; this guide uses A=0 through Z=25. |
| Multiplier a | The value multiplying the plaintext number. |
| Offset b | The value added after multiplication. |
| Modular inverse | A number a⁻¹ such that a·a⁻¹ ≡ 1 (mod 26). It is required for decryption. |
| Coprime | Two numbers whose greatest common divisor is 1. Valid a values must be coprime to 26. |
Keep this beside you while solving
Valid multipliers and inverses
| 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 |
Alphabet numbers
| Letter | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
Why only certain multipliers work
If
a
shares a factor with 26, two different plaintext values can collapse to the same ciphertext value, so decryption is not unique. The valid multipliers are exactly 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.
x = a⁻¹(y − b) mod 26
. Reduce negative values modulo 26 before converting back to letters.
How encryption works
Convert the letter to a number
For H, x=7.
Multiply by a
With a=5: 5×7=35.
Add b
With b=8: 35+8=43.
Reduce modulo 26
43 mod 26 = 17, which is R.
How decryption works
Find a⁻¹
For a=5, the inverse is 21 because 5×21 ≡ 1 (mod 26).
Subtract b
If ciphertext R is 17, 17−8=9.
Multiply by the inverse
21×9=189.
Reduce modulo 26
189 mod 26 = 7 = H.
How to approach it in Codebusters practice
- Write the formula and inverse table before starting calculations.
- A supplied a value that has no inverse mod 26 cannot define a valid one-to-one Affine substitution.
- Once a and b are known, build the full substitution alphabet once instead of recalculating every repeated letter.
What the problem gives you vs. what you produce
| Part | What to expect |
|---|---|
| You may be given | Ciphertext plus a and b, or enough known plaintext/ciphertext information to infer them. |
| You must find | Plaintext and sometimes the key. |
| Fastest first move | Write A=0…Z=25 and locate a⁻¹ before decoding any letters. |
How to attack an unknown or partially known key
- Two reliable plaintext↔ciphertext letter correspondences can form two modular equations in a and b.
- Frequency and word-pattern analysis still apply because the cipher is monoalphabetic.
- After finding a candidate key, verify it against multiple letters before decoding the whole message.
Follow one example from start to finish
a=5, b=8
HI → RW
Before moving on, make sure you can answer these without another site:
- Can you explain why a=2 is invalid modulo 26?
- For a=5, b=8, can you encrypt A and then decrypt the result?
- Can you find the inverse of 5 mod 26 from the reference table?
Common mistakes
Using A=1 instead of A=0 with the stated formula.
Choosing an a that is not coprime with 26.
Forgetting the modular inverse during decryption.
Reducing negative values incorrectly; keep adding 26 until the value is in 0–25.
Competition speed strategies
Keep the valid-a/inverse table on your reference sheet.
Build a complete mapping row once the key is known.
Use modular reduction early to keep arithmetic small.
What to remember under time pressure
See every transformation
Use the lab to change inputs and keys, keep the relevant reference material visible, inspect each intermediate transformation, and then read the “How to reverse it” panel so encryption and decryption connect.