Hill cipher encryption is the transformation C = KP mod 26, where K is a 2 by 2 key matrix, P is a two-letter column vector, and C is the resulting encrypted block.

This is a polygraphic substitution method: it encrypts letters in pairs rather than substituting each letter independently. For an A-Z exercise, the important details are the alphabet mapping, vector direction, block size, padding rule, and matrix key. The Hill Cipher Decoder implements one explicit version of those choices and performs the conversion entirely in your browser.

hill cipher encryption
Hill Cipher Encryption: A Practical A-Z Workflow

The convention behind Hill cipher encryption

The documented convention is A = 0, B = 1, continuing through Z = 25. After removing spaces, punctuation, and digits, the remaining letters are divided into two-letter blocks. Each block is represented as a column vector and multiplied by the key matrix.

  • Alphabet: A is 0 and Z is 25.
  • Block size: Each matrix operation processes two letters.
  • Vector direction: Each letter pair is a column, not a row.
  • Encryption rule: C = K times P, reduced modulo 26.
  • Padding: One X is appended when the normalized plaintext has an odd number of letters.

These choices affect the result, not just the presentation. A source that uses row vectors, a different alphabet mapping, another block size, or another padding rule can produce different ciphertext from the same visible key and message.

Choose a key that can be reversed

Write the key as K = [[a, b], [c, d]]. Its determinant is det(K) = ad - bc. For decryption, the determinant must have a greatest common divisor of 1 with 26, meaning that det(K) and 26 share no factor other than 1. Only then does a unique modular inverse exist. The tool rejects a non-invertible key rather than returning text that cannot be decrypted uniquely.

The matrix control accepts exactly four whole-number values arranged as two rows separated by a semicolon, such as 3 3; 2 5. Negative and large entries are normalized into the range 0 through 25. The displayed default key is 3 3; 2 5, which converts HELP to HIAT under the documented convention.

Encrypt A-Z text in the browser

  1. Confirm the exercise convention. Check that it uses A = 0 through Z = 25, two-letter column vectors, and X padding for odd-length plaintext. Do not continue if the source uses a different convention.
  2. Enter the key and select the operation. Supply the two matrix rows, choose encryption or decryption, and enter the A-Z message or ciphertext. For example, the default key can be entered as 3 3; 2 5.
  3. Run the conversion. The browser applies the key to each two-letter block and reduces the resulting values modulo 26.
  4. Compare a known pair. With the default key, test the plaintext HELP. Its expected ciphertext is HIAT. If that test does not match, check the mapping, matrix orientation, key order, and padding rule before processing a longer message.
  5. Use the same convention for decryption. Select decryption, enter the corresponding matrix, and provide ciphertext with an even number of normalized letters. The tool computes the modular inverse and multiplies each ciphertext block by it.
  6. Preserve presentation details separately. Keep word spacing in a separate field or record if the exercise requires it. Also record the original plaintext length when a final X could be meaningful, because decryption cannot identify whether the last X was padding.

The output contains uppercase letters only. Removed spaces, punctuation, digits, and other presentation characters are not restored automatically, so the normalized cipher output should be compared with the exercise's expected A-Z sequence rather than with its original layout.

The matrix calculation in one example

For a key K = [[a, b], [c, d]] and a plaintext pair represented by the column vector P = [x, y], the encrypted values are calculated as c1 = (ax + by) mod 26 and c2 = (cx + dy) mod 26. The two numerical results then map back to letters.

Using the default key 3 3; 2 5, HELP forms HE = [7, 4] and LP = [11, 15]. For HE, the transformed vector is [(3 × 7 + 3 × 4) mod 26, (2 × 7 + 5 × 4) mod 26] = [7, 8], which maps to HI. For LP, it is [(3 × 11 + 3 × 15) mod 26, (2 × 11 + 5 × 15) mod 26] = [0, 19], which maps to AT. Together, the two blocks produce HIAT.

For decryption, the implementation computes det(K)^-1 multiplied by the adjugate of K, with all arithmetic reduced modulo 26. It performs that calculation only after confirming that the determinant is coprime with 26.

Padding, formatting, and input limits

Encryption accepts odd-length plaintext because it adds one X to make the normalized message even. Decryption deliberately leaves a trailing X untouched, since that character may belong to the original message. These rules make the output reliable for controlled exercises without pretending that the tool can recover formatting or infer the original message length.

Operation or caseDocumented ruleResult for the user
Encrypt plaintextA-Z input is allowed; odd normalized plaintext receives one X.The ciphertext has uppercase letters and an even number of letters.
Decrypt ciphertextThe normalized ciphertext must contain an even number of letters.Odd ciphertext is rejected because it cannot form complete two-value blocks.
Enter a keyProvide exactly four whole numbers in two semicolon-separated rows.Negative and large values are normalized to 0-25; a non-invertible determinant is rejected.
Normalize textSpaces, punctuation, and digits are removed.Original case, spacing, punctuation, accents, and digits cannot be reconstructed automatically.
Handle input sizeInput is capped at 100,000 letters.Empty normalized input and oversized messages are rejected.

If the real plaintext ends in X, the decrypted text will also end in X, exactly like plaintext whose odd length triggered automatic padding. Record the original length in any controlled exercise rather than deleting the final character automatically.

Diagnose a result that does not match

A mismatch usually indicates a convention difference, not an arithmetic problem in the browser. Run the known HELP to HIAT check and then verify these points:

  • Whether the source maps A to 0 or uses a different alphabet numbering.
  • Whether letter pairs are written as rows or columns.
  • Whether the source uses two-letter blocks or another block size.
  • Whether it pads odd plaintext with X or follows a different rule.
  • Whether the same four key values appear in the same matrix positions.

Use the Hill cipher only for education

The Hill cipher was historically important because it applied linear algebra to polygraphic substitution. It is not secure for modern confidentiality. Known plaintext can reveal linear relationships, the alphabet and block size are small, and this page exposes the key directly.

Never use Hill cipher encryption for passwords, tokens, personal information, files, or production messages. Use a reviewed authenticated-encryption scheme such as AES-GCM when real security is required.

Related reading: HMAC Generator Online: Hashing, Not Encryption.

Related reading: How Do I Avoid Mistakes Using a Rail Fence Cipher Decoder?.