The guide
A cipher made of matrices: why invertible mod 26 needs more than a non-zero determinant
How the Hill cipher mixes letters, why a key must be coprime to 26 and not merely non-zero, how many keys exist, and why its linearity lets a few known blocks give the key away.
- 25 mod 26 determinant of the key GYB NQK URP, odd and not a multiple of 13
- 157,248 usable 2 by 2 keys out of 456,976 matrices
- 40.57 bits usable 3 by 3 keys, 1,634,038,189,056 of them
- QRT ACT under row vectors, where column vectors give POH
- 2 blocks known plaintext blocks that give a 2 by 2 key
Read the full guide Fold the guide
How a matrix mixes letters
Most classical ciphers swap one letter for another. The Hill cipher, described by Lester S. Hill in 1929, swaps a whole block of letters at once. It was the first polygraphic cipher in which operating on more than three symbols at a time was practical, even if only barely.
Number the letters A to Z as 0 to 25. Take a block of n letters as a column of n numbers, multiply it by an n by n key matrix, and reduce every result modulo 26. The reduced column, turned back into letters, is the ciphertext block. A message longer than one block is cut into blocks and each is handled the same way, with the last block filled up with a padding letter if it is short.
The standard worked example uses the key GYB NQK URP, which is the matrix with rows 6 24 1, 13 16 10 and 20 17 15. The block ACT is the column 0, 2, 19. The product is 67, 222, 319, and reducing each mod 26 gives 15, 14, 7, which spells POH. The block CAT, which uses the same three letters, becomes FIN.
Every letter changed, and each output letter depends on all three input letters. That spreading of one input letter across several output letters is the property Claude Shannon called diffusion, and it is the cipher’s genuine strength. A single-letter substitution cannot do it.
A small trap lies in how the block is written. Treat it as a row and multiply from the other side, and the same key letters turn ACT into QRT, not POH. Neither is wrong, but a message encrypted one way will not decrypt the other way, so any description of the cipher has to say which it uses.
Why invertible needs more than non-zero
To read the message you need the inverse key, the matrix that undoes the multiplication. Over the real numbers a square matrix has an inverse whenever its determinant is not zero. Modulo 26 the condition is stricter: the determinant must have an inverse modulo 26, which happens exactly when it shares no factor with 26.
Since 26 is 2 times 13, the determinant must be odd and must not be a multiple of 13. The determinant of GYB NQK URP is 441, which is 25 mod 26. The number 25 is 5 squared, so it shares nothing with 26, and the key works. The key with rows 2 4 and 1 3 has determinant 2, which is not zero, yet it has no inverse. The key with rows 13 13 and 13 14 has determinant 13 and fails for the same reason.
That is the usual chatbot mistake, and a second one sits beside it. Inverting the key over the real numbers and rounding gives a matrix that looks plausible and is wrong. For the example key the rounded real inverse reduces to AZB AAA ABA, which decrypts nothing.
The right inverse comes from two pieces. The adjugate, the transpose of the cofactor matrix, is the integer matrix that multiplies the key to the determinant times the identity. The inverse of the determinant modulo 26 comes from the extended Euclidean algorithm. Multiply the two, reduce mod 26, and you have the inverse key.
For the example the inverse of 25 is 25, since 25 times 25 is 625, which is 1 mod 26. The result is the matrix with rows 8 5 10, 21 8 21 and 21 12 8, or IFK VIV VMI in letters. Applied to POH it returns ACT. Some authors prefer to change the alphabet instead of the key. Add three extra symbols, such as a space, a period and a question mark, and the modulus becomes 29, a prime, so every non-zero determinant has an inverse and the trap disappears. The cost is a larger alphabet to agree on, and the cipher is no less linear.
The 2 by 2 key with rows 3 3 and 2 5 shows the same steps on a smaller scale. Its determinant is 9, the inverse of 9 mod 26 is 3, and the inverse key has rows 15 17 and 20 9. The word HELP encrypts to HIAT and decrypts back.
Counting the keys
A key must be invertible modulo 26, which by the Chinese remainder theorem means invertible modulo 2 and modulo 13 separately. The number of invertible matrices over each is the order of a general linear group, the product of p to the n minus p to the k, for k from 0 to n minus 1. Multiply the two orders and you have the number of usable keys.
For 2 by 2 matrices that is 6 times 26,208, which is 157,248 out of 456,976 matrices. About 34.41 percent of 2 by 2 matrices can serve as keys, and the count is 17.26 bits. Counting every one of the 456,976 matrices by machine gives the same figure.
For 3 by 3 matrices the order mod 2 is 168 and the order mod 13 is 9,726,417,792. Their product is 1,634,038,189,056 keys, or 40.57 bits. The 4 by 4 case has more than 12 sextillion usable keys and 73.38 bits.
These sound like large numbers, and a naive reader takes them as a measure of strength. Remember the point of Kerckhoffs’s principle: a cipher should stay secure even when everything except the key is public. A large key count helps only if no shortcut around the key exists.
Why linearity is the fatal flaw
The Hill cipher has a shortcut, and it is the same property that makes it easy to use. Every ciphertext block is the key times a plaintext block. That is a linear equation, and linear equations can be solved.
Suppose an opponent knows a few plaintext blocks and their ciphertext, which the attack model calls a known-plaintext attack. Write n of the plaintext blocks as the columns of a matrix P and the matching ciphertext blocks as the columns of C. Then C is the key times P, so the key is C times the inverse of P, mod 26, whenever P has an inverse mod 26.
Take the 2 by 2 key 3 3 2 5 and the pairs HE to HI and LP to AT. The plaintext columns are 7, 4 and 11, 15, with determinant 61, which is 9 mod 26 and so invertible. One matrix multiplication returns 3 3 2 5. Searching all 456,976 matrices confirms that exactly one usable key fits those two pairs. A 2 by 2 key falls to 2 blocks, with no search at all.
The solve can stall. If the plaintext columns are 0, 1 and 2, 3, the determinant is 24, which is even and so not invertible, and two usable keys fit the pairs. The cure is a third pair. In general n squared letters of known plaintext are usually enough, and a few more pairs rescue an indeterminate system.
The same trap appears in the larger example. ACT and CAT look different, but their letters have the same odd and even pattern, so a matrix built from both is never invertible. The solve skips that choice and finds a working set of pairs.
What this tool does and does not do
The calculator encrypts and decrypts, checks whether a key is usable, shows the inverse, counts the keys and recovers a key from known blocks. It also lists six wrong versions beside the right values, computed from the key and message you type. A good habit is to test a key before trusting it: take the determinant, reduce it mod 26, then take its gcd with 26. Anything other than 1 means the key can encrypt but its ciphertext can never be read back, which is the worst failure, because nothing looks wrong until someone tries to decrypt.
It is a study tool. It protects nothing. A Hill key offers no confusion, only diffusion, so it is fine as one step inside a larger design: the MixColumns step of AES is itself a matrix multiplication. Used alone it falls to a few known blocks, and the page says so wherever a result appears.
It does not cover modern ciphers, real keys, variants with 27 or 29 symbols, or any attack beyond the linear algebra shown. Use it to see how the arithmetic works, and to see why a clever structure and a big key count are not the same as security.
Sources
Every source consulted for this guide. Links checked November 22, 2026.
- Hill cipher Wikipedia (CC BY-SA)
- Known-plaintext attack Wikipedia (CC BY-SA)
- Modular multiplicative inverse Wikipedia (CC BY-SA)
- Lester S. Hill Wikipedia (CC BY-SA)
- Modular arithmetic Wikipedia (CC BY-SA)
- Determinant Wikipedia (CC BY-SA)
- Invertible matrix Wikipedia (CC BY-SA)
- Adjugate matrix Wikipedia (CC BY-SA)
- Extended Euclidean algorithm Wikipedia (CC BY-SA)
- Block cipher Wikipedia (CC BY-SA)
- Confusion and diffusion Wikipedia (CC BY-SA)
- Chinese remainder theorem Wikipedia (CC BY-SA)
- General linear group Wikipedia (CC BY-SA)
- Kerckhoffs's principle Wikipedia (CC BY-SA)
- BigInt MDN Web Docs (CC BY-SA)