Caesar cipher · 04 The maths

Caesar cipher: the maths

Modular arithmetic, encryption and decryption formulas, key space: the Caesar cipher written as formulas.

If we number the letters from 0 (A) to 25 (Z), the Caesar cipher becomes a simple operation of modular arithmetic.

Encryption
C = (P + k) mod 26
Decryption
P = (C − k) mod 26
Key
k, an integer from 1 to 25 (k = 0 would leave the text unchanged)
Key space
25 useful keys: very few

The mod 26 operation returns the remainder of division by 26: it is what lets the alphabet “wrap around”. For example, for Z with key 3: 25 + 3 = 28, and 28 mod 26 = 2, that is C.

Useful properties

  1. 01
    Two encryptions are one encryption

    Encrypting with k₁ and then with k₂ is the same as encrypting once with k₁ + k₂: adding steps does not add security.

  2. 02
    Decrypting is encrypting

    Decrypting with key k is the same as encrypting with key 26 − k.

  3. 03
    ROT13 is its own inverse

    With k = 13, encrypting twice returns the original text, because 13 + 13 = 26.

The big brother: the affine cipher

The Caesar cipher is a special case of the affine cipher, in which C = (a·P + b) mod 26. With a = 1 we are back to Caesar. For decryption to be possible, a must be coprime with 26: there are 12 valid values of a and 26 of b, hence 312 keys. Still far too few to resist a computer, and still vulnerable to frequency analysis.

Continue with
05 Weaknesses and attacks

Caesar cipher: how it is broken

Brute force, frequency analysis and known plaintext: three ways to break the Caesar cipher, each faster than the last.

↑