MODULAR SUBSTITUTION • INTERMEDIATE

Affine Cipher

Use multiplication and addition modulo 26 to build a complete substitution alphabet.

Family Mathematical monoalphabetic substitution
Key / parameter Two integers a and b
Text structure Word boundaries may be preserved
01 • OVERVIEW

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.

Competition note

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.

Starting from zero?

No outside reference is assumed. Work through Foundations → Complete Reference → Encryption → Decryption in order, then use the competition and cryptanalysis sections.

02 • FOUNDATIONS

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.
03 • COMPLETE REFERENCE

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.

Decryption recipe: compute x = a⁻¹(y − b) mod 26 . Reduce negative values modulo 26 before converting back to letters.
Reference rule: if a fixed table, formula, alphabet, or convention is needed to solve this cipher, it should be visible in this section or generated explicitly by the visualizer below.
04 • ENCRYPTION

How encryption works

1

Convert the letter to a number

For H, x=7.

2

Multiply by a

With a=5: 5×7=35.

3

Add b

With b=8: 35+8=43.

4

Reduce modulo 26

43 mod 26 = 17, which is R.

05 • DECRYPTION

How decryption works

1

Find a⁻¹

For a=5, the inverse is 21 because 5×21 ≡ 1 (mod 26).

2

Subtract b

If ciphertext R is 17, 17−8=9.

3

Multiply by the inverse

21×9=189.

4

Reduce modulo 26

189 mod 26 = 7 = H.

06 • COMPETITION WORKFLOW

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.
Season note: use this page to learn the cipher mechanics and solving workflow. Exact Science Olympiad event constraints can change by season, so follow the current official rules/clarifications for tournament-specific limits.
07 • CRYPTANALYSIS

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.
08 • WORKED PROBLEM

Follow one example from start to finish

Key
a=5, b=8
Encrypt H
H=7 → 5(7)+8=43 → 17 → R
Encrypt I
I=8 → 5(8)+8=48 → 22 → W
Result
HI → RW
BEGINNER SELF-CHECK

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?
09 • ERROR CHECK

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.

10 • SPEED

Competition speed strategies

1

Keep the valid-a/inverse table on your reference sheet.

2

Build a complete mapping row once the key is known.

3

Use modular reduction early to keep arithmetic small.

11 • QUICK REFERENCE

What to remember under time pressure

Encrypt E(x)=(ax+b) mod 26
Decrypt D(y)=a⁻¹(y−b) mod 26
Valid a 1,3,5,7,9,11,15,17,19,21,23,25
Letter values A=0 … Z=25
How to use this section: during timed practice, come here first for the minimum rules. If a step is unclear, jump back to Complete Reference or the worked example instead of guessing.
12 • INTERACTIVE LAB

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.