A Hill cipher decoder with a key decrypts text by multiplying each two-letter block by the key matrix’s modular inverse modulo 26.
That direct answer assumes the source uses the same alphabet, block arrangement, and padding convention as the decoder. The Hill Cipher Decoder supports this known-key workflow for A-Z text with an invertible 2 by 2 matrix. It can encrypt plaintext or decrypt ciphertext entirely in your browser, so the key and message do not need to be sent to an external service.
Use Decrypt when you already have ciphertext and the exercise provides the matrix. Use Encrypt when you need to transform a plaintext or check a published example. Before processing a long passage, align the conventions and validate the key with a short known pair.

Match the cipher conventions first
A Hill cipher result depends on more than the visible key values. Letter numbering, vector orientation, block size, and padding all affect the output. Another exercise may use row vectors, a different alphabet mapping, or another padding rule, so an apparent arithmetic error can actually be a convention mismatch.
| Setting | Convention to confirm |
|---|---|
| Alphabet | A=0, B=1, and so on through Z=25 |
| Block arrangement | Two letters form a column vector |
| Key | An invertible 2×2 matrix |
| Encryption relation | C = K × P, reduced modulo 26 |
| Odd-length plaintext | One X is appended as padding |
Check these details against the instructions that supplied the ciphertext. If the source numbers A as 1 instead of 0, places pairs in rows instead of columns, or uses a block size other than two, its published answer may not match this tool. The decoder intentionally follows one explicit convention so that the same key and input produce a reproducible result.
Decode a Hill cipher with a known key
Use the following sequence when you have both ciphertext and a 2×2 key:
- Confirm the exercise’s conventions. Verify that it uses A=0 through Z=25, two-letter column vectors, and X padding for an odd-length plaintext. Also confirm that you need decryption rather than encryption.
- Prepare the key in the required format. Enter exactly four integer or whole-number values as two rows separated by a semicolon. The default key is 3 3; 2 5. The tool also normalizes negative and large key entries into the range 0–25.
- Choose the operation. Select Decrypt for ciphertext. Select Encrypt only when the assignment supplies plaintext or asks you to reproduce a known encryption pair.
- Provide the A-Z text. Enter the ciphertext or plaintext without relying on punctuation, digits, or spaces for the cipher calculation, because those presentation characters are removed during normalization. Ciphertext must contain an even number of normalized letters because every block contains two values.
- Run the conversion. Review the uppercase output and any error message. An invalid determinant, odd-length ciphertext, empty normalized input, or oversized message produces a rejection rather than misleading text.
- Compare a known pair before continuing. With the default key, HELP becomes HIAT. To test decryption specifically, confirm that HIAT returns HELP. If the exercise supplies another key and published pair, use those instead, and only then rely on a longer result.
Keep the original text and its length alongside the decoded output. That record helps with formatting and resolves the one ambiguity in this workflow: a final X may be padding, or it may have been part of the original message.
Check whether the key can decrypt
For key rows a b and c d, and a plaintext pair x y arranged as a column vector, the encoder uses C = K × P modulo 26. The first encrypted value is ax + by, and the second is cx + dy, with both reduced to the range 0–25. Decryption applies the modular inverse of K to each ciphertext column vector.
A 2×2 key has an inverse modulo 26 only when its determinant, ad − bc, is coprime with 26. In other words, the greatest common divisor must be 1. If it is not, multiple plaintext pairs can map to the same ciphertext, so a unique inverse does not exist. The decoder rejects such a key with an invalid-key error instead of returning text that looks plausible but cannot be uniquely decrypted.
| Input condition | Tool behavior |
|---|---|
| Odd normalized plaintext in Encrypt mode | One X is appended to make a complete pair |
| Even normalized plaintext | No padding is added |
| Odd normalized ciphertext in Decrypt mode | Rejected because a two-value block is incomplete |
| Decrypted result ends in X | The X remains in the output |
| Normalized input is empty | Rejected |
| Message contains more than 100,000 letters | Rejected because it exceeds the input cap |
Verify the result with a known pair
The fastest calibration is the tool’s documented default example: the key 3 3; 2 5 maps HELP to HIAT. For the first pair, H is 7 and E is 4 under A=0 through Z=25.
[3 3; 2 5] × [7; 4] = [33; 34] ≡ [7; 8] modulo 26, so HE becomes HI.
The complete known pair therefore provides a simple check of the key format, letter mapping, matrix ordering, and block orientation at once. Run the same key in Encrypt mode and confirm HELP becomes HIAT, then switch to Decrypt mode and confirm HIAT becomes HELP. A mismatch at this stage should be resolved before processing a longer passage.
Account for padding and removed formatting
Encryption adds one X only when the normalized plaintext has an odd number of letters. That padding makes the final block complete, but it also creates an unavoidable question during decryption: the tool cannot determine whether a final X was inserted automatically or was genuinely present in the original message. It therefore leaves the decrypted X untouched. In a controlled exercise, record the original length instead of deleting the final character automatically.
Normalization also removes spaces, punctuation, and digits. The output contains only uppercase letters, so it cannot restore original capitalization, word boundaries, accents, punctuation, or numbers. If the exercise expects readable words, retain the spacing separately and add it back after decoding. For ciphertext, count the letters that remain after normalization and confirm that total is even before running the conversion.
Diagnose a mismatch
When the output disagrees with an expected answer, check the following in order:
- Count the normalized ciphertext. An odd number of letters cannot form complete two-letter blocks and is rejected.
- Confirm the alphabet mapping. A=0 through Z=25 must match the source’s numbering.
- Confirm vector orientation. This tool uses C = K × P with P and C as column vectors. Row-vector instructions can produce different ciphertext from the same visible key values.
- Confirm the key order. For key rows a b and c d, the first output value is ax + by and the second is cx + dy.
- Check block size and padding. Both this implementation and the source must use two-letter blocks and agree about X padding.
- Check the determinant and mode. The determinant must be coprime with 26 for decryption, and the selected operation must match the direction of the exercise.
- Retest a short published pair. A known pair separates a convention problem from a longer-message transcription problem.
A result that differs from another decoder usually reflects a different mapping, vector orientation, block size, or padding rule rather than a browser arithmetic problem. Change one setting at a time and rerun the known pair so you can identify the actual cause.
Keep Hill cipher work within its intended use
The Hill cipher is historically important because it applied linear algebra to polygraphic substitution, but it is not secure for modern confidentiality. Known plaintext can expose linear relationships, the alphabet and block space are small, and this educational page exposes the key directly. It is suitable for learning matrix inversion, checking an assignment, or exploring a historical cipher.
Do not use the Hill cipher for passwords, tokens, personal information, files, or production messages. For data that genuinely needs confidentiality, use a reviewed authenticated-encryption scheme such as AES-GCM encryption rather than treating this teaching cipher as a modern security system.
Related reading: HTML Entity Encoder vs Decoder: When to Use Each.