StringMash.com

Affine cipher

Multiply the letter's position, add a shift, and take what's left after 26.

Conversion
13 characters
Updates as you type
a=5, b=8

Show the steps
  1. Number the letters from A = 0 to Z = 25.
  2. Each letter becomes 5 × letter + 8, then take the remainder after dividing by 26.
  3. A is 0: 5 × 0 + 8 = 8, remainder 8, which is I
  4. F is 5: 5 × 5 + 8 = 33, remainder 7, which is H
  5. F is 5: 5 × 5 + 8 = 33, remainder 7, which is H
  6. I is 8: 5 × 8 + 8 = 48, remainder 22, which is W
  7. N is 13: 5 × 13 + 8 = 73, remainder 21, which is V
  8. E is 4: 5 × 4 + 8 = 28, remainder 2, which is C
  9. C is 2: 5 × 2 + 8 = 18, remainder 18, which is S
  10. I is 8: 5 × 8 + 8 = 48, remainder 22, which is W
  11. The other 4 letters follow the same rule.
Printable chart

Using the affine tool

Set Multiply by and Then add, and the text on the left is enciphered as you type. Swap the boxes to decode with the same two numbers.

The multiplier is a picker rather than a number box, because only 12 of the 25 possible values work. The others are left out because they can't be undone.

Why only 12 multipliers

Number the letters A to Z as 0 to 25. Multiply by a, add b, and keep the remainder after dividing by 26. Decoding needs the multiplication undone, and that only works when a has a multiplicative inverse modulo 26.

26 factors into 2 and 13. Any multiplier sharing a factor with it collapses letters together. Multiply by 2 and both A and N land on 0, so the message can't be recovered. Multiply by 13 and the whole alphabet falls onto two letters.

That leaves 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25. Those 12 multipliers and 26 shifts give 312 keys in total. A laptop tries them all faster than you can read this sentence, so don't use the affine cipher to protect anything.

A-Z

The alphabet at a=5, b=8

AI
BN
CS
DX
EC
FH
GM
HR
IW
JB
KG
LL
MQ
NV
OA
PF
QK
RP
SU
TZ
UE
VJ
WO
XT
YY
ZD

Caesar, generalised

Set the multiplier to 1 and the affine cipher is a Caesar shift. Set it to 25 and add 25, and you get Atbash, the mirrored alphabet.

Textbooks keep it because it's the smallest cipher that needs real modular arithmetic to break, so it's where a cryptography course introduces the extended Euclidean algorithm and the idea of a multiplicative inverse. The standard break is a frequency count that pins down two letters, then the pair of equations gives up the key.

Related conversions

Caesar is this cipher with the multiplier stuck at 1, and ROT13 is Caesar with the shift stuck at 13. Each step down the chain removes a key.

Given mystery text, the cipher identifier will try the shifted alphabets first.

Questions

What is the affine cipher?

A substitution cipher where each letter's position is multiplied by one number, added to another, and reduced modulo 26. Caesar and Atbash are both special cases of it.

Why can't I choose any multiplier?

The multiplier must share no factor with 26. Even numbers and 13 map several letters onto the same output, so the message couldn't be decoded.

How many affine keys are there?

312: 12 valid multipliers times 26 shifts.

How do you break an affine cipher?

Count letter frequencies, guess that the two commonest map from E and T, and solve the two equations for the multiplier and the shift. Or try all 312 keys.

Sources

Added . Last updated and reviewed . What's new