Hill Cipher Tool

Encrypt and decrypt with 2x2 or 3x3 Hill cipher key matrices. Automatic modular inverse, determinant validation and step-by-step block arithmetic.

Advertisement

Hill Cipher Encoder and Decoder with Matrix Inverse Calculator

The Hill cipher is the one classical cipher that is genuinely linear algebra. Instead of substituting one letter at a time, it treats a block of letters as a vector and multiplies it by a key matrix modulo 26. That single design choice makes it the first practical cipher to encrypt more than two letters at once, and it is why the Hill cipher defeats the letter-frequency analysis that breaks Caesar, Atbash, and simple substitution outright.

This tool encrypts and decrypts with 2×2 and 3×3 key matrices, computes the modular inverse matrix for you, validates that the matrix you entered can actually be inverted, and shows the arithmetic for every block. If you are here for a homework problem, a CTF challenge, or a cryptography course, the step-by-step view is the part worth using — it shows each vector, each matrix multiplication, and each reduction mod 26. Everything runs in your browser; no text is uploaded.

How It Differs From the Other Classical Ciphers

Every other cipher in our collection substitutes or rearranges characters. The Hill cipher performs arithmetic on them. Three consequences follow, and they are the reason it is worth learning separately:

  • It is polygraphic. A 3×3 key encrypts three letters as a unit, so the same plaintext letter maps to different ciphertext letters depending on its neighbours. Single-letter frequency analysis — the technique that cracks the Caesar cipher and the substitution cipher — produces a flat distribution and tells you nothing.
  • Not every key works. The Vigènere cipher accepts any keyword; the affine cipher requires only that its multiplier be coprime with 26. The Hill cipher requires the whole matrix to be invertible mod 26, which is a much stronger condition and the thing most people get stuck on.
  • It is trivially broken by known plaintext. Because encryption is linear, an attacker with enough matching plaintext and ciphertext can solve for the key directly — no guessing involved. Linearity is what gives the cipher its strength against frequency analysis and its fatal weakness against algebra.

The Mathematics

Letters are numbered A=0 through Z=25. Split the plaintext into blocks of n letters, where n is the matrix dimension, and treat each block as a column vector p. Encryption is:

c = K · p (mod 26)

Decryption uses the modular inverse of the key matrix:

p = K⁻¹ · c (mod 26)

The inverse only exists when the determinant of K is invertible mod 26 — that is, when gcd(det(K), 26) = 1. Since 26 = 2 × 13, the determinant must be odd and not a multiple of 13. Reduced mod 26, the twelve values that qualify are:

1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25

If your determinant reduces to 0, 2, 13, or any even number, the matrix cannot be inverted and the ciphertext it produces is undecryptable — different plaintexts collide onto the same ciphertext. The tool checks this as you type and refuses to encrypt with an invalid key rather than letting you produce garbage you can never reverse.

Constructing the inverse takes two steps: find d⁻¹, the modular multiplicative inverse of the determinant mod 26, then multiply the adjugate matrix by it. For a 2×2 matrix the adjugate swaps the diagonal and negates the off-diagonal; for 3×3 the tool computes the cofactor matrix and transposes it. All arithmetic is reduced mod 26 at each stage, so negative intermediate values wrap into the 0–25 range.

Worked Example

Take the classic key K = [[3, 3], [2, 5]] and the plaintext HELP.

The determinant is (3 × 5) − (3 × 2) = 9, which is in the valid list, so the key is usable. The modular inverse of 9 mod 26 is 3, because 9 × 3 = 27 ≡ 1.

First block HE becomes the vector (7, 4):

RowArithmeticResult mod 26Letter
1(3 × 7) + (3 × 4) = 337H
2(2 × 7) + (5 × 4) = 348I

Second block LP is (11, 15): row one gives (3 × 11) + (3 × 15) = 78 ≡ 0 → A; row two gives (2 × 11) + (5 × 15) = 97 ≡ 19 → T. So HELP encrypts to HIAT. Note that the first block happened to encrypt H to H — a coincidence of this key, and a reminder that fixed points are normal rather than a bug.

To decrypt, the inverse matrix is 3 × adj(K) mod 26 = [[15, 17], [20, 9]]. Applying it to (7, 8) returns (7, 4) — HE.

How to Use the Tool

  1. Choose 2×2 or 3×3. Each size keeps its own matrix, so you can switch back and forth without losing your key.
  2. Enter the key matrix. The determinant and its validity are shown live. A red indicator means the matrix is not invertible mod 26 — change one entry and watch the determinant move.
  3. Pick encode or decode. In decode mode the tool computes and applies the inverse matrix automatically; you never have to invert it by hand.
  4. Type or paste your text. Non-letters are stripped and everything is uppercased, because the cipher only operates on the 26-letter alphabet. If the length is not a multiple of the block size, the text is padded with X.
  5. Open the Matrix Math tab to see the determinant, the modular inverse of the determinant, the adjugate, and the final inverse matrix.
  6. Turn on step-by-step to see each block as a vector, the products, and the reduction — the view you want if you are checking coursework.

The Analysis tab shows the letter distribution of the result, which is instructive: encrypt a long English passage and the distribution flattens noticeably compared with a monoalphabetic cipher, though a 2×2 key leaves more structure than a 3×3.

Breaking It

The Hill cipher is not secure, and the reason is worth understanding because it recurs in modern cryptanalysis. Encryption is a linear map, so with n known plaintext blocks and their ciphertexts you can assemble two n×n matrices P and C such that C = K · P, and recover the key as K = C · P⁻¹ mod 26 — provided P is invertible. For a 2×2 key that means four known plaintext letters are enough. Guessing a probable crib such as a header or a common word often supplies them.

Ciphertext-only attacks are harder but still feasible for 2×2 keys, since the keyspace is small enough to search with digraph frequency scoring. This is why Hill’s cipher, published by Lester S. Hill in 1929 and briefly implemented in a mechanical device, never saw serious operational use. Treat it as a teaching cipher and a puzzle. If you are unsure what cipher you are even looking at, start with the cipher identifier.

Frequently Asked Questions

Why does my key matrix get rejected?

Its determinant is not coprime with 26. Compute the determinant, reduce it mod 26, and check it against the list of valid values: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. Any even determinant or any multiple of 13 makes the matrix non-invertible. Changing a single entry usually fixes it.

What does the X at the end of my decrypted text mean?

It is padding. The Hill cipher works on fixed-size blocks, so plaintext whose length is not a multiple of the block size is padded before encryption. The padding letters are recovered on decryption and are safe to discard.

Can it handle spaces, punctuation, and numbers?

No — and that is inherent to the cipher, not a limitation of this implementation. The arithmetic is defined mod 26 over the letters A–Z, so everything else is stripped before encryption and cannot be restored afterwards.

Is a 3×3 key meaningfully stronger than 2×2?

Against frequency analysis, yes — trigraph statistics are far sparser than digraph statistics, so ciphertext-only attacks become impractical for short messages. Against known-plaintext attacks, no: it just means the attacker needs nine matching letters instead of four.

How do you invert a matrix modulo 26?

Compute the determinant, reduce it mod 26, find its modular multiplicative inverse (the value d⁻¹ with d × d⁻¹ ≡ 1 mod 26), then multiply the adjugate matrix by that inverse and reduce every entry mod 26. The Matrix Math tab shows each of these stages for the key you entered.

Is any text sent to a server?

No. All matrix arithmetic and encryption happens in JavaScript in your browser. Nothing is transmitted or stored.

Is the Hill cipher secure?

No. It resists letter-frequency analysis, which was genuinely novel in 1929, but its linearity means a known-plaintext attack recovers the key by solving a system of equations. Use it for teaching and puzzles only.

Which cipher should I look at next?

If you want the other polygraphic classical cipher, the Playfair cipher encrypts pairs of letters using a 5×5 key square instead of matrix arithmetic. For a keyword-driven polyalphabetic approach, see the Vigènere cipher.

Not sure which cipher you have? Use the Cipher Identifier to auto-detect cipher types from unknown ciphertext using frequency analysis and Index of Coincidence.

What Is the Hill Cipher?

The Hill cipher is a polygraphic substitution cipher that encrypts blocks of letters using matrix multiplication modulo 26. It is the standard textbook example of linear algebra in cryptography. Because each ciphertext block depends on every plaintext letter in that block, Hill diffuses information across positions in a way simpler substitution ciphers cannot.

How the Hill Cipher Works

Number the alphabet A=0, B=1, ..., Z=25. Pick a square key matrix K of size n × n (commonly 2×2 or 3×3) with entries mod 26. Group the plaintext into blocks of n letters and treat each block as a column vector:

C = K · P (mod 26)

Decryption multiplies by the matrix inverse of K modulo 26:

P = K⁻¹ · C (mod 26)

The key matrix must be invertible mod 26, which requires gcd(det(K), 26) = 1. Since 26 = 2 × 13, determinants that are even or multiples of 13 have no inverse and break the cipher.

Worked Example (2×2)

Encrypt "HELP" with key K = [[3, 3], [2, 5]].

  • det(K) = 3·5 − 3·2 = 9. gcd(9, 26) = 1, so K is invertible.
  • Blocks: "HE" = [7, 4], "LP" = [11, 15].
  • K · [7, 4] mod 26: row 1 = 3·7 + 3·4 = 33 mod 26 = 7 → H; row 2 = 2·7 + 5·4 = 34 mod 26 = 8 → I.
  • K · [11, 15] mod 26: row 1 = 3·11 + 3·15 = 78 mod 26 = 0 → A; row 2 = 2·11 + 5·15 = 97 mod 26 = 19 → T.

Ciphertext: "HIAT".

To decrypt, compute K⁻¹ mod 26. The inverse of det(K) = 9 mod 26 is 3 (since 9·3 ≡ 1). The 2×2 inverse formula yields K⁻¹ = [[15, 17], [20, 9]]. Multiplying K⁻¹ by each ciphertext block recovers HE and LP. Plaintexts whose length is not a multiple of n are padded with X.

History

Lester S. Hill, an American mathematician, invented the cipher and published it in 1929 in The American Mathematical Monthly in "Cryptography in an Algebraic Alphabet." It was the first cipher to use linear algebra and the first practical polygraphic cipher operating on more than three letters at a time. Hill also designed a mechanical device for the matrix multiplication, but it saw little operational use.

When to Use the Hill Cipher

Educational use only. It is the canonical example for teaching matrix arithmetic mod a composite number, modular matrix inverses via the adjugate method, invertibility constraints on cipher keys, and the diffusion concept central to modern block ciphers like DES and AES. It also appears in CTF challenges. Not suitable for real-world secrecy.

Security and Cryptanalysis

The Hill cipher is completely broken by a known-plaintext attack. With n plaintext-ciphertext block pairs, an attacker assembles n×n matrices P and C such that C = K · P mod 26. If P is invertible mod 26, they solve directly:

K = C · P⁻¹ (mod 26)

For 2×2 Hill, just 4 known plaintext letters often suffice to recover the entire key.

  • Ciphertext-only attacks are harder than for monoalphabetic ciphers since Hill flattens single-letter frequencies, but n-gram frequency analysis still applies — digraphs (2×2) or trigrams (3×3) have well-known English distributions that leak structure.
  • Brute force is feasible for small matrices: about 157,000 invertible 2×2 matrices mod 26 — small enough to enumerate with English-language scoring.
  • The cipher is linear, so it provides no security against chosen-plaintext attacks at all.

Related Ciphers

  • Playfair cipher — another polygraphic cipher (digraphs), using grid lookup instead of matrix multiplication.
  • Affine cipher — the 1×1 case of Hill, where the "matrix" is a single scalar a and the cipher reduces to E(x) = a·x + b mod 26.
  • Vigenère cipher — a polyalphabetic substitution closer in spirit, but using a repeating keyword rather than matrix arithmetic.

If unknown ciphertext does not yield to single-letter or digraph frequency analysis, Hill is one candidate — start with the Cipher Identifier to narrow the field.

This tool is provided for informational and educational purposes only. All processing happens in your browser — no data is sent to or stored on our servers. While we strive for accuracy, we make no warranties about the completeness or reliability of results.