Hill Cipher

Encrypt and decrypt text with the Hill cipher using an invertible numeric key matrix. Learn how classical matrix-based polygraphic substitution works modulo the alphabet size.

Key matrix
Matrix status
Input
0 chars · 0 bytes
Try:
Result
✓ Matrix-based polygraphic cipher ✓ Validates invertible keys modulo the alphabet size ✓ Multiple alphabets supported ✓ Processed on our server
Examples
Encrypt with a 2x2 matrix
Key
3 3 2 5
Input HELP
Output HIAT

Classic Hill cipher example with an invertible 2x2 key matrix. Text is encrypted in pairs of letters.

Decrypt with the same matrix
Key
3 3 2 5
Input HIAT
Output HELP

The same key matrix is inverted modulo 26 to restore the plaintext.

Encrypt a longer message
Key
3 3 2 5
Input MATRIX
Output KYETPB

A 2x2 key matrix processes the message as letter pairs: MA, TR, and IX.

Encrypt with a 3x3 matrix
Key
6 24 1 13 16 10 20 17 15
Input ACT
Output POH

Classic 3x3 Hill cipher example. The plaintext block ACT is multiplied by the key matrix modulo 26.

How the Hill cipher works

The Hill cipher is a classical polygraphic substitution cipher based on linear algebra. Instead of encrypting one letter at a time, it groups letters into fixed-size blocks and treats each block as a vector of numbers.

The key is a square numeric matrix. During encryption, each plaintext vector is multiplied by the key matrix, and the result is reduced modulo the alphabet size. For the English alphabet, this usually means modulo 26.

For example, a 2x2 key matrix encrypts text in pairs of letters, while a 3x3 matrix encrypts text in groups of three. Decryption uses the inverse of the key matrix modulo the same alphabet size.

Key matrix example

The key matrix can be entered directly in the matrix editor. The matrix size determines how many letters are processed together during encryption.

  • 2×2 matrix → encrypts pairs of letters
  • 3×3 matrix → encrypts groups of three letters
  • 4×4 matrix → encrypts groups of four letters
  • 5×5 matrix → encrypts groups of five letters

The tool automatically validates whether the matrix is invertible modulo the selected alphabet size and shows the determinant status before encryption.

Why the matrix must be invertible

The Hill cipher depends on reversible matrix multiplication. To decrypt a message, the tool must calculate the inverse of the key matrix modulo the alphabet size.

A matrix is usable only when its determinant is coprime with the alphabet size. For modulo 26, this means the determinant must not share a common factor with 26. If this condition fails, different plaintext blocks may produce the same ciphertext block, making reliable decryption impossible.

Hill cipher and modern security

The Hill cipher was an important step in classical cryptography because it introduced matrix operations and encrypted multiple letters at once. This made it more resistant to simple frequency analysis than many monoalphabetic substitution ciphers.

However, the Hill cipher is not secure by modern standards. If enough plaintext and ciphertext pairs are known, the key matrix can often be recovered. Today it is mainly used for education, mathematics, puzzles, and demonstrations of linear algebra in cryptography.

FAQ

Decryption requires the inverse of the key matrix modulo the alphabet size. If the determinant is not coprime with that size, the inverse does not exist and the ciphertext cannot be reliably decrypted.

Enter rows of numbers separated by semicolons, for example 3 3; 2 5. A flat list such as 3, 3, 2, 5 is also accepted when it forms a square matrix.

The most common Hill cipher examples use 2x2 or 3x3 matrices. A 2x2 matrix encrypts pairs of letters, while a 3x3 matrix encrypts groups of three letters.

Modulo 26 means that all matrix calculation results are wrapped into the range of the 26-letter English alphabet. This keeps every encrypted value mapped to a letter.

Hill cipher implementations usually add padding characters so the text can be split into complete blocks. For example, a 2x2 matrix requires pairs of letters.

No. The Hill cipher is useful for learning classical cryptography and matrix operations, but it can be broken with modern methods, especially when plaintext and ciphertext samples are known.

Yes. It is a polygraphic substitution cipher because it substitutes groups of letters rather than single letters.
Related tools

Caesar Cipher

Classic letter-shift cipher with custom shift values.

Playfair Cipher

Classic digraph substitution cipher with keyword matrix encryption.

Vigenere Cipher

Keyword-based polyalphabetic encryption and decryption.

Vernam Cipher

XOR-based Vernam encryption with Base64 output.