Hill Cipher
Encode and decode the Hill cipher, the classical polygraphic cipher built on linear algebra. Letters are grouped into blocks, turned into number vectors, and multiplied by a key matrix modulo 26: encryption is C = K·P, decryption is P = K⁻¹·C. Choose a 2×2 or 3×3 key, watch the live matrix, its determinant, and its modular inverse, and follow the block-by-block working. Everything runs in your browser.
Key (letters)
The key is 4 letters (A=0 … Z=25) filling the matrix row by row. A 2×2 key enciphers letters in pairs (digraphs).
Enter text above to see the result here.
Key matrix
Each block of letters becomes a vector (A=0 … Z=25) and is multiplied by the key matrix modulo 26. A key works only when its determinant is invertible mod 26.
C = K · P (mod 26)
Enter exactly 4 letters (A–Z) to fill the key matrix.
How to use Hill Cipher
- 1
Choose encode or decode and a matrix size
Select Encode to encrypt or Decode to decrypt, then pick a 2×2 key (letters in pairs) or a 3×3 key (letters in triples).
- 2
Enter the key as letters
Type the key letters that fill the matrix row by row — four letters for a 2×2 key, nine for a 3×3. The live matrix shows the determinant and tells you whether the key is invertible mod 26.
- 3
Type or paste your text
Enter your message and it is converted as you type. The working panel shows each block as a vector, the matrix multiplication, and the resulting block.
- 4
Check the key matrix and inverse
Open the key-matrix panel to see the key, its determinant, and — when the key is valid — the modular inverse matrix used for decoding.
- 5
Copy, download, or share
Copy the result, download it as a text file, or share a link that reopens the tool with your exact text, key, size, and direction ready to go.
Understanding the Hill Cipher
What is the Hill cipher?
The Hill cipher is a classical polygraphic substitution cipher invented by the American mathematician Lester S. Hill in 1929. Instead of replacing one letter at a time, it enciphers a whole block of letters together by treating the block as a vector of numbers and multiplying it by a secret key matrix, modulo 26. It was the first practical cipher able to operate on more than three symbols at once, and it brought linear algebra squarely into cryptography.
Because each output letter depends on every input letter in its block, the Hill cipher hides the single-letter frequencies that betray simpler ciphers. A 2×2 key mixes letters in pairs, a 3×3 key mixes them in triples, and larger matrices mix even bigger blocks. That diffusion is the same idea that underlies modern block ciphers, which makes the Hill cipher a favourite teaching example — even though, as a linear cipher, it is easy to break with a little known plaintext.
How the Hill cipher works
First, turn letters into numbers with A=0, B=1, up to Z=25. The key is an n×n matrix of such numbers; here you type it as a run of letters that fills the matrix row by row, so a 2×2 key needs four letters and a 3×3 key needs nine. The plaintext is split into blocks of n letters, each written as a column vector.
To encrypt a block P, compute C = K · P (mod 26): multiply the key matrix by the vector and reduce every entry modulo 26, then read the numbers back as letters. To decrypt, you need the modular inverse of the key matrix, K⁻¹, and compute P = K⁻¹ · C (mod 26). The inverse exists only when the matrix determinant is invertible modulo 26 — that is, when it shares no common factor with 26 — which is the single most important rule when choosing a key.
Choosing a valid key matrix
Not every matrix can be a Hill key. For decryption to work, the determinant of the key, taken modulo 26, must be coprime with 26. Since 26 = 2 × 13, the determinant must be odd and not a multiple of 13; the allowed values are the twelve numbers 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. If the determinant is even or equals 13, no modular inverse exists and the ciphertext cannot be uniquely decoded.
The tool computes the determinant for you and shows a green badge when the key is invertible or a red one when it is not, alongside the inverse matrix it will use for decoding. If a key is rejected, change a letter or two and watch the determinant update until it lands on a valid value. This live feedback turns the otherwise fiddly job of hand-picking a Hill key into a quick, visual exercise.
A worked 2×2 example
Take the key DDCF, which fills the 2×2 matrix with the numbers 3, 3 on the top row and 2, 5 on the bottom row. Its determinant is 3×5 − 3×2 = 9, and because 9 is coprime with 26 the key is valid. Now encrypt HELP. The first block HE is the vector (7, 4): multiplying gives (3×7 + 3×4, 2×7 + 5×4) = (33, 34), which reduces mod 26 to (7, 8) = HI.
The second block LP is (11, 15), giving (3×11 + 3×15, 2×11 + 5×15) = (78, 97) = (0, 19) = AT. So HELP encrypts to HIAT. To decrypt, the tool inverts the key to (15, 17 / 20, 9) and multiplies each cipher block by it, recovering HELP. Type DDCF as the key above to watch every step appear in the live working panel.
A worked 3×3 example
The classic 3×3 example uses the key GYBNQKURP, which fills the matrix with 6, 24, 1 on the first row, 13, 16, 10 on the second, and 20, 17, 15 on the third. Encrypting the trigraph ACT — the vector (0, 2, 19) — gives (67, 222, 319) before reduction, which becomes (15, 14, 7) modulo 26, or POH.
The determinant of this matrix is 25 modulo 26, which is coprime with 26, so it is a valid key, and its modular inverse is the matrix (8, 5, 10 / 21, 8, 21 / 21, 12, 8). Multiplying the cipher block POH by that inverse returns ACT. Switch the size selector to 3×3 and enter GYBNQKURP to reproduce this textbook example and see the inverse matrix the tool derives.
Padding, letters, and formatting
The Hill cipher only knows the 26 letters A–Z, so spaces, digits, and punctuation are stripped before encryption and do not return when you decrypt. Because the message is processed in fixed blocks, a plaintext whose length is not a multiple of the block size is padded with the letter X to complete the final block; a decrypted message may therefore end in one or two extra letters.
Letter case is not preserved — everything is treated as uppercase. These limitations are inherent to the classical cipher rather than to this tool, and they are part of why the Hill cipher, like its contemporaries, was used for short tactical messages rather than free-flowing text. The live breakdown shows exactly how your text is grouped and padded into blocks.
Security and cryptanalysis
The Hill cipher's strength is also its fatal weakness: it is linear. Each cipher block is a fixed linear function of the plaintext block, so an attacker who learns enough plaintext–ciphertext block pairs can set up and solve a system of linear equations to recover the key matrix directly. For an n×n key, roughly n known blocks are usually enough, which makes the cipher fall quickly to a known-plaintext attack.
It also offers no diffusion between blocks and no confusion beyond the linear mix, so identical plaintext blocks always encrypt to identical cipher blocks. By modern standards it is not secure, and you should never use it to protect real information — reach for a vetted algorithm such as AES instead. As a clear, hands-on illustration of how matrices, modular arithmetic, and block encryption fit together, though, the Hill cipher remains one of the best classical ciphers to learn.
Frequently asked questions
What is the Hill cipher?
How does the Hill cipher work?
How do I choose a valid Hill cipher key?
Can you show a worked Hill cipher example?
How do you decode a Hill cipher?
What is the difference between a 2×2 and a 3×3 Hill cipher?
Why does my key say it is not invertible?
What happens to spaces, numbers, and punctuation?
Is the Hill cipher secure?
Who invented the Hill cipher?
What is the modular inverse of a matrix?
Is my text uploaded to a server?
Related tools
Keep going with these handy tools