Foundations
Modular arithmetic
Arithmetic that wraps around a finite alphabet.
Also found as clock arithmetic · modulo · mod 26
Wrap around the alphabet
Modulo n keeps the remainder after division by n. With 26 letter values, adding 26 brings you back to the same letter. This lets a Caesar shift pass Z and continue at A without a separate rule for the end of the alphabet.
x mod n ∈ {0, 1, …, n − 1}29 mod 26 = 3−3 mod 26 = 23- Y
24- Add
24 + 5 = 29- Reduce
29 mod 26 = 3- Result
D
Undo an operation
Decryption uses the operation that undoes encryption. If encryption adds k, decryption subtracts k. Multiplication is reversible only when the multiplier has an inverse modulo the alphabet size. That matters for affine ciphers; Caesar only needs addition.
C = (P + k) mod nP = (C − k) mod n