The Affine cipher changes each letter with two numbers: a multiplies its alphabet position, and b adds a shift. With A = 0 and Z = 25, the rule is C = (aP + b) mod 26. Choose values for a and b, then select a letter to see the calculation.
Choose a reversible key
Choose a from 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, or 25. These values share no factor with 26 other than 1, so each encoded letter can be traced back to one original letter. Other values make letters overlap and cannot be decoded reliably.
Multiply by 5, then add 8
HELLOa = 5; b = 8; A = 0RCLLA| Input | P | 5P + 8 | mod 26 | Output |
|---|---|---|---|---|
| H | 7 | 43 | 17 | R |
| E | 4 | 28 | 2 | C |
| L | 11 | 63 | 11 | L |
| O | 14 | 78 | 0 | A |
Reverse the calculation
To decode, subtract b and multiply by the inverse of a. For a = 5, that inverse is 21: 5 × 21 = 105, which leaves a remainder of 1 when divided by 26. The rule becomes P = 21(C − 8) mod 26. For R, index 17, this gives 21 × 9 = 189, or 7 mod 26: H.
Try one yourself
Encode CAT using a = 5 and b = 8. Then test a = 13 with the same message.
Spaces and punctuation stay in place by default. Shifts that differ by 26 have the same effect: b = −18 gives the same result as b = 8. Check the normalized key above the result to see the values used.