On this page
In June 1929, Lester S. Hill of Hunter College published "Cryptography in an Algebraic Alphabet" in The American Mathematical Monthly (Crossref record). His idea was to treat letters as numbers and blocks of letters as vectors, then encrypt by multiplying by a matrix. It is Class 12 matrix multiplication with one new step: every answer is reduced mod 26. Below is a full example you can check with a pencil.
How does the Hill cipher encrypt with a matrix?
Number the letters A = 0, B = 1, up to Z = 25. Split the message into pairs and write each pair as a column vector p. The ciphertext is c = Kp mod 26, where the key K is an invertible matrix (Hill cipher, Wikipedia).
We will use the key K = [3 2; 5 7], meaning first row (3, 2) and second row (5, 7). Our message is TEST.
TE is (19, 4). K times it is (3×19 + 2×4, 5×19 + 7×4) = (65, 123). Reduce mod 26: (13, 19), which is NT.
ST is (18, 19). K times it is (54 + 38, 90 + 133) = (92, 223). Reduce mod 26: (14, 15), which is OP.
So TEST becomes NTOP. The two Ts became N and P: a letter's ciphertext depends on its partner, so single-letter counts no longer line up with the plaintext.
Why must the key's determinant be coprime to 26?
To decrypt, you need K⁻¹ mod 26. A matrix has an inverse mod 26 if and only if its determinant has an inverse mod 26, which means it shares no factor with 26 = 2 × 13 (Hill cipher, Wikipedia). Only 12 values qualify: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25.
See what goes wrong otherwise. Take K = [2 1; 0 1], with determinant 2. AA is (0, 0) and goes to (0, 0). NA is (13, 0) and goes to (26, 0), which is also (0, 0). Two messages, one ciphertext: nobody can tell which was sent.
Our key has determinant 3×7 − 2×5 = 11, which is odd and not a multiple of 13, so it is safe.
Using the counting formula on the Wikipedia page, 157,248 of the 456,976 possible 2×2 matrices mod 26 can be used as keys.
How do you find the inverse of a matrix mod 26?
Use the usual 2×2 formula, but multiply by the determinant's inverse mod 26 instead of dividing.
Find 11⁻¹ mod 26. Since 11 × 19 = 209 = 8 × 26 + 1, the inverse is 19.
Swap the diagonal and negate the other two entries: [7 −2; −5 3], which is [7 24; 21 3] mod 26.
Multiply by 19 and reduce: [133 456; 399 57] becomes K⁻¹ = [3 14; 9 5].
Check it: K × K⁻¹ = [27 52; 78 105], which is the identity mod 26.
Now decrypt NTOP. (13, 19) goes to (39 + 266, 117 + 95) = (305, 212), which is (19, 4), or TE. (14, 15) goes to (42 + 210, 126 + 75) = (252, 201), which is (18, 19), or ST. You get TEST back.
How does a known-plaintext attack break the Hill cipher?
The Hill cipher is entirely linear. An attacker who has n² letters of plaintext and the matching ciphertext can set up a linear system and usually solve it (Hill cipher, Wikipedia). For a 2×2 key, that means four letters.
Suppose someone learns that TEST was sent as NTOP. Write the plaintext pairs as the columns of P = [19 18; 4 19] and the ciphertext pairs as the columns of C = [13 14; 19 15]. Since C = KP, the key is K = CP⁻¹.
det P = 361 − 72 = 289, which is 3 mod 26. The inverse of 3 is 9, because 27 leaves remainder 1.
P⁻¹ = 9 × [19 8; 22 19] = [171 72; 198 171], which is [15 20; 16 15] mod 26.
CP⁻¹ = [419 470; 525 605], which is [3 2; 5 7] mod 26.
That is the whole key, from four letters. Had det P shared a factor with 26, the attacker would need a few more blocks.
A short practice plan
Pick your own 2×2 key and check that its determinant is coprime to 26.
Encrypt your first name, padding it with an X if it has an odd number of letters.
Find K⁻¹ mod 26 and decrypt. No name back means recheck the inverse.
Swap ciphertexts with a friend, share the first four plaintext letters, and recover each other's keys.
If the modular inverse keeps going wrong, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer. To revise matrices, determinants and modular arithmetic as ideas, open the Fermi Concept Library.
Next, see why the one-time pad has perfect secrecy, try modular exponentiation by square and multiply, or practise with JEE Main matrices and determinants questions.



