Hill cipher online – encoder/decoder
Hill cipher calculator, encoder and decoder online. Encrypt letter pairs with a 2 × 2 matrix modulo 26 and study a worked inverse example.
What is linear algebra doing in cryptography?
What is the Hill cipher?
The Hill cipher transforms whole groups of letters at once using matrix multiplication. In the version used by the tool, letters travel in pairs: each pair becomes a vector of two numbers, which is multiplied by the 2 × 2 key matrix and reduced modulo 26.
How is a pair of letters calculated?
Letters become numbers from 0 to 25, then two sums are computed: the first output letter comes from a×x + b×y and the second from c×x + d×y, where a, b, c, d are the numbers of the matrix. Both sums are taken modulo 26 and converted back into letters. An odd-length text is padded with the letter X.
Example with the matrix [3,3;2,5]
The message “REBUS” with the default matrix becomes “LC LY TV”. An odd number of letters is padded with an X, so the pair SX is encrypted and the result appears in pairs. Decryption returns “RE BU SX” — the padding X has to be dropped by hand at the end.
Numeric values of the letters
The top row is the letter and the bottom row the value used in the calculation. A equals zero, so after multiplying and reducing modulo 26 the result returns to the alphabet through the same table.
| Letter | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Value | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
How do you decrypt the text?
Decryption multiplies the letter pairs by the inverse matrix modulo 26. For the matrix [3,3;2,5] the inverse is [15,17;20,9] — the tool works it out itself once you switch to decryption. You only need to supply the same matrix that was used for encryption.
Why isn’t every matrix a valid key?
The matrix must be invertible modulo 26, which means its determinant may share no divisor with 26. The determinant of [3,3;2,5] is 9 and the greatest common divisor of 9 and 26 is 1, so the key is valid. When the determinant is divisible by 2 or 13, decryption becomes impossible and the tool returns no result.
How can the Hill cipher be broken?
The cipher is linear, so known plaintext–ciphertext pairs can be used to build a system of equations and recover the matrix. With two-letter blocks, two known pairs may suffice if their plaintext vectors form a matrix that is invertible modulo 26; otherwise more pairs are needed. Without such material, digraph frequency analysis helps. That linearity is the method’s greatest weakness.
What does Hill hide better than a simple substitution?
Because the whole pair decides the outcome, the same letter in different positions produces different ciphertext letters and single-letter frequencies stop betraying the text. Pair frequencies appear instead, so the cipher resists analysis better than Caesar while still being unsuitable for protecting confidential data.
See also: Caesar cipher · Vigenère cipher
History of the Hill cipher
Lester S. Hill published his method in 1929 in “The American Mathematical Monthly”. It was among the first polygraphic ciphers built deliberately on linear algebra and showed that cryptography could draw on the regular tools of mathematics. Hill even constructed a mechanical device to carry out the transformations.