Hill Cipher Decoder

Decrypt Hill cipher with known key or perform known-plaintext attack to recover the key matrix.

Ciphertext
0 letters
Plaintext
Matrix Size
Key Matrix (K)
det = 9 (mod 26)
Inverse (K⁻¹)
Click calculate to show

Matrix values: 0-25 (A=0, Z=25)

Determinant must be coprime with 26

Hill Cipher Decryption Overview

The Hill cipher decoder provides two powerful decryption methods for recovering plaintext from ciphertext: standard decryption using a known key matrix, and known-plaintext attack for recovering unknown keys. Hill cipher decryption uses the mathematical inverse of the encryption key matrix, applying the formula P = (K⁻¹ × C) mod 26, where P represents the plaintext vector, K⁻¹ is the inverse matrix, and C is the ciphertext vector. This tool automates complex inverse matrix calculations, making decryption accessible for both educational study and practical cryptanalysis demonstrations.

How to Decrypt Hill Cipher with Known Key

When you possess the encryption key matrix, Hill cipher decryption follows a straightforward mathematical process that reverses the encryption transformation.

Decryption Steps

Step 1: Input your ciphertext

Enter the encrypted message into the ciphertext area. The hill cipher decoder automatically filters to letters A-Z, removing spaces, numbers, and punctuation. The ciphertext length should be divisible by your matrix size (2 for 2x2, 3 for 3x3) for optimal decryption.

Step 2: Select matrix size

Choose 2x2 or 3x3 to match the matrix size used during encryption. This selection is critical—using the wrong matrix size will produce incorrect results. If you're unsure, start with 2x2 as it's more common in educational settings.

Step 3: Input the key matrix

Provide your encryption key using one of two methods. Enter a keyword that automatically converts to a matrix (convenient for keys generated from memorable words), or directly input numeric matrix values (precise control for specific mathematical keys). The tool accepts values from 0 to 25, corresponding to letters A through Z.

Step 4: Automatic inverse matrix calculation

Click "Calculate Inverse" and our hill cipher decoder automatically computes K⁻¹ through these operations: calculating the determinant modulo 26, finding the modular inverse of the determinant using the extended Euclidean algorithm, computing the adjugate matrix (transpose of the cofactor matrix), and multiplying the determinant inverse by the adjugate, all mod 26. The tool displays both the original key matrix and the calculated inverse matrix for verification.

Step 5: Decrypt and view results

Click "Decrypt" to transform ciphertext back to plaintext. The decryption applies the inverse matrix to each ciphertext block, performs modulo 26 operations, and converts numbers back to letters. Enable "Show Steps" to view detailed calculations including ciphertext-to-number conversion, matrix multiplication for each block, modulo operations, and final letter conversion. The "Remove Padding" option automatically eliminates common padding characters (X, Z) that may have been added during encryption.

Need to encrypt instead? Try our Hill Cipher Encoder for the reverse operation.

Calculating the Inverse Matrix Mod 26

The inverse matrix is the mathematical key to Hill cipher decryption, reversing the encryption transformation through precise modular arithmetic operations.

What is an Inverse Matrix?

An inverse matrix K⁻¹ satisfies the equation K × K⁻¹ = I (mod 26), where I is the identity matrix [[1,0],[0,1]] for 2x2 or [[1,0,0],[0,1,0],[0,0,1]] for 3x3. The inverse matrix "undoes" the original matrix transformation, making decryption possible. Not all matrices have inverses—only those with determinants coprime to 26 are invertible modulo 26.

2x2 Inverse Matrix Calculation

For a 2x2 matrix, follow these four steps to compute the inverse matrix modulo 26.

Step 1: Calculate the determinant

For matrix K = [[a,b],[c,d]]:

det(K) = (a × d) - (b × c)

Example with K = [[3,3],[2,5]]:

det(K) = (3 × 5) - (3 × 2) = 15 - 6 = 9

Step 2: Find the modular inverse of the determinant

Find x such that (det × x) ≡ 1 (mod 26). For det = 9, we need 9x ≡ 1 (mod 26).

Testing systematically:

  • 9 × 3 = 27 = 26 + 1 ≡ 1 (mod 26) ✓

Therefore, det⁻¹ = 3

Step 3: Calculate the adjugate matrix

For [[a,b],[c,d]], the adjugate is:

adj(K) = [[d, -b],
          [-c, a]]

Example: adj([[3,3],[2,5]]) = [[5,-3],[-2,3]]

Apply mod 26 to handle negative values:

adj(K) = [[5, 23],
          [24, 3]]  (since -3 ≡ 23 and -2 ≡ 24 mod 26)

Step 4: Multiply and apply modulo 26

K⁻¹ = det⁻¹ × adj(K) mod 26
K⁻¹ = 3 × [[5, 23],     [[15, 69],     [[15, 17],
            [24, 3]]  =   [72, 9]]   ≡   [20, 9]] (mod 26)

Verification: Multiply K × K⁻¹ to confirm it equals the identity matrix mod 26.

3x3 Inverse Matrix Calculation

Computing a 3x3 inverse matrix involves significantly more complex calculations requiring cofactor expansion, determinant calculation using the rule of Sarrus or cofactor expansion, computing nine cofactors (one for each element), forming the cofactor matrix and transposing it to get the adjugate, and finally multiplying by the determinant inverse modulo 26.

Our hill cipher decoder automates these calculations, handling all the mathematical complexity behind the scenes. For manual calculation practice, start with 2x2 matrices before attempting 3x3 inverses. Use our Matrix Calculator to verify complex inverse matrix calculations and explore step-by-step computation methods.

Known-Plaintext Attack on Hill Cipher

The Hill cipher's complete linearity makes it vulnerable to cryptanalysis when attackers possess matching plaintext-ciphertext pairs, demonstrating a fundamental weakness in this classical cipher system.

Understanding the Vulnerability

The Hill cipher weakness stems from its purely linear transformation. Because encryption is simply matrix multiplication followed by modulo operation, the relationship between plaintext and ciphertext forms a system of linear equations. With sufficient known plaintext-ciphertext pairs, an attacker can set up and solve these equations to recover the secret key matrix completely, even without any knowledge of the original key.

Required Data for Attack

For 2x2 matrices: Minimum 4 characters of known plaintext with their corresponding ciphertext (2 complete blocks). This provides 4 equations with 4 unknowns (the four matrix elements), creating a solvable system.

For 3x3 matrices: Minimum 9 characters of known plaintext-ciphertext pairs (3 complete blocks). This generates 9 equations with 9 unknowns (the nine matrix elements), again creating a solvable linear system.

The attack requires exactly n² characters for an n×n matrix, making larger matrices slightly more resistant but still fundamentally vulnerable to this cryptanalysis technique.

Attack Process Explained

Step 1: Build plaintext matrix P

Arrange known plaintext blocks as column vectors in a matrix. For example, if you know "HELL" maps to "APAD", form:

P = [[H, L],     [[7, 11],
     [E, L]]  =   [4, 11]]

Step 2: Build ciphertext matrix C

Arrange corresponding ciphertext blocks as columns:

C = [[A, A],     [[0, 0],
     [P, D]]  =   [15, 3]]

Step 3: Calculate P⁻¹

Compute the inverse of the plaintext matrix using the method described above. This inverse must exist—if det(P) is not coprime to 26, you need different plaintext blocks.

Step 4: Recover key matrix

Use the relationship C = K × P to derive K = C × P⁻¹ (mod 26). Multiply the ciphertext matrix by the inverse plaintext matrix to recover the original encryption key.

Step 5: Validate and decrypt

Test the recovered key on other ciphertext portions to verify correctness. Once validated, use this key to decrypt the entire message using standard Hill cipher decryption with the recovered key matrix.

Using Our Known-Plaintext Attack Tool

Switch to "Known Plaintext Attack" mode in the decoder above. Input the full ciphertext you want to decrypt, enter the known plaintext segment (minimum 4 characters for 2x2, 9 for 3x3), enter the corresponding ciphertext segment, and click "Find Key" to automatically recover the encryption matrix. The tool then applies the recovered key to decrypt all remaining ciphertext, displaying both the recovered key matrix and the complete plaintext message.

This attack mode demonstrates why the Hill cipher, despite its mathematical elegance, cannot provide security for modern communications. See decryption examples for complete attack walkthroughs.

Decryption Example

Let's decrypt the ciphertext "HIAT" using the known key matrix [[3,3],[2,5]], demonstrating every step of the Hill cipher decryption process.

Setup

Ciphertext: HIAT Key Matrix: K = [[3,3],[2,5]]

Step-by-Step Decryption

Step 1: Calculate the inverse matrix

Determinant: det(K) = (3×5) - (3×2) = 9

Modular inverse of 9: 9⁻¹ = 3 (since 9×3 = 27 ≡ 1 mod 26)

Adjugate: adj(K) = [[5,23],[24,3]] (after applying mod 26)

Inverse matrix: K⁻¹ = 3 × [[5,23],[24,3]] = [[15,17],[20,9]] (mod 26)

Step 2: Convert ciphertext to numbers

H = 7, I = 8, A = 0, T = 19

Form blocks: [7,8] and [0,19]

Step 3: Decrypt first block [7,8]

[[15, 17],     [7]     [15×7 + 17×8]     [241]     [7]
 [20, 9]]   ×  [8]  =  [20×7 + 9×8]   =  [212]  ≡  [4] (mod 26)

Convert to letters: [7,4] = HE

Step 4: Decrypt second block [0,19]

[[15, 17],     [0]      [15×0 + 17×19]     [323]     [11]
 [20, 9]]   ×  [19]  =  [20×0 + 9×19]   =  [171]  ≡  [15] (mod 26)

Convert to letters: [11,15] = LP

Result: HIAT → HELP

This example demonstrates how the inverse matrix perfectly reverses the encryption process. For more examples, visit our Examples page with additional decryption scenarios.

Frequently Asked Questions About Hill Cipher Decryption

How to decrypt Hill cipher without key?

Without the encryption key, you must use cryptanalysis techniques. The most effective method for Hill cipher is the known-plaintext attack: if you possess at least n² matching plaintext-ciphertext characters (4 for 2x2 matrices, 9 for 3x3 matrices), you can calculate the key matrix by setting up and solving a system of linear equations. Use our "Known Plaintext Attack" mode by switching tabs in the decoder above. Simply input your known plaintext-ciphertext pairs, and the tool automatically recovers the key matrix, then decrypts the entire message.

How to find the inverse matrix modulo 26?

To find K⁻¹ mod 26, follow this systematic process: Calculate det(K) using standard determinant formulas, find det(K)⁻¹ mod 26 using the extended Euclidean algorithm (a value x where det(K) × x ≡ 1 mod 26), calculate the adjugate matrix adj(K) by taking the transpose of the cofactor matrix, and compute K⁻¹ = det(K)⁻¹ × adj(K) mod 26, ensuring all operations use modular arithmetic. Our hill cipher decoder automates this complex calculation—just enter your key matrix above, and the tool instantly computes and displays the inverse matrix with full step-by-step visualization.

What if the key matrix is not invertible?

If det(K) = 0 or if det(K) shares common factors with 26 (divisible by 2 or 13), the matrix has no modular inverse and cannot be used for Hill cipher decryption. You must choose a different key matrix with a valid determinant. Valid determinants modulo 26 are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25. Our tool automatically warns you if a key is invalid and provides specific feedback about why the matrix cannot be inverted, helping you select an appropriate alternative key.

Can I decrypt Hill cipher 3x3 online?

Yes, absolutely. Our hill cipher decoder fully supports both 2x2 and 3x3 Hill cipher decryption. Simply select "3x3" as your matrix size, enter your key matrix (either as a keyword or direct numeric values), input your ciphertext, and click "Decrypt". The tool automatically calculates the complex 3x3 inverse matrix using cofactor expansion and adjugate matrix methods, applies it to your ciphertext, and produces the plaintext output. Enable "Show Steps" to see the complete 3x3 decryption process with detailed matrix multiplication calculations.

Hill cipher decoder with steps - how to enable?

Simply check the "Show Steps" checkbox located above the decrypt button in the decoder interface. When enabled, the tool displays each calculation step in detail: ciphertext-to-number conversion showing each letter's numeric value, inverse matrix calculation including determinant, modular inverse, and adjugate matrix, matrix multiplication for each ciphertext block with intermediate results, modulo 26 operation showing how values wrap within the alphabet range, and final number-to-letter conversion producing the plaintext. This step-by-step visualization is invaluable for learning how Hill cipher decryption works mathematically.

How accurate is the known-plaintext attack?

The known-plaintext attack on Hill cipher is extremely accurate and deterministic. If you provide correct plaintext-ciphertext pairs—minimum 4 characters for 2x2 matrices, 9 characters for 3x3 matrices—the attack calculates the exact key matrix used for encryption with 100 percent accuracy. This is not a probabilistic attack; it's a mathematical certainty due to the Hill cipher's completely linear nature. The attack works by solving a system of linear equations that has a unique solution. This demonstrates the Hill cipher's fundamental vulnerability: the linearity that makes it mathematically elegant also makes it cryptographically weak against this type of analysis.

Hill cipher decryption formula?

The Hill cipher decryption formula is P = (K⁻¹ × C) mod 26, where P represents the plaintext vector, K⁻¹ is the inverse of the key matrix, and C is the ciphertext vector. The inverse matrix is calculated using K⁻¹ = det(K)⁻¹ × adj(K) mod 26, where det(K)⁻¹ is the modular inverse of the determinant and adj(K) is the adjugate matrix. This formula mathematically reverses the encryption transformation C = (K × P) mod 26 by applying the inverse matrix. See complete calculation examples on our examples page with detailed step-by-step demonstrations.

Explore decryption tools for related classical ciphers. The Affine Cipher Decoder demonstrates modular inverse concepts similar to Hill cipher inverse matrix calculations. The Playfair Cipher Decoder shows another digraph decryption approach using a different method. The Vigenere Cipher Decoder illustrates polyalphabetic decryption contrasting with Hill cipher's matrix-based approach. Each decoder demonstrates different cryptanalytic techniques and mathematical principles used in classical cryptography.