Ciphers
Affine
E(x) = (a·x + b) mod 26. The multiplier must be coprime with 26, which leaves twelve of them and 312 keys.
Cipher / Multiplicative
Affine Cipher
a·x + b mod 26.
- Keyspace
- 12 × 26 = 312
- Decode
- same options back
- Works on
- A-Z, the rest passes
- Family
- 1 in multiplicative
required 0 / 2
Options
Access
- Create
create("affine") - CLI
ciphers encode affine 'AFFINE CIPHER' --a 5 --b 8 - Tryplayground with the sample above
A letter is its index x, 0 to 25. Encoding is (a·x + b) mod 26, decoding multiplies by the inverse of a. That inverse only exists when gcd(a, 26) = 1, so a is one of 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 or 25. Anything else is an InvalidOptionError before the first letter, because a cipher that maps two letters onto one can't be decoded and shouldn't pretend. b is a shift 0 to 25. Defaults are a = 5, b = 8.
const affine = create("affine");
affine.encode("AFFINE CIPHER", { a: 5, b: 8 }).text; // "IHHWVC SWFRCP"
affine.decode("IHHWVC SWFRCP", { a: 5, b: 8 }).text; // "AFFINE CIPHER"
affine.encode("X", { a: 13, b: 0 }); // throws, gcd(13, 26) = 13
a = 1 is a Caesar with shift b. a = 25, b = 25 is Atbash. Twelve multipliers times 26 shifts is 312 keys - small enough to try every one.