Hill Cipher Examples & Tutorials

Learn Hill cipher with step-by-step examples for beginners, intermediate, and advanced users.

2 Examples

Basic 2×2 Encryption

2×2

This is the most basic Hill cipher example using a 2×2 matrix. Each pair of letters is encrypted together as a vector.

Plaintext
HELP
Ciphertext
HIAT
Key Matrix (K)
33
25

Basic 2×2 Decryption

2×2

Decryption requires calculating the inverse matrix. The same process applies to all blocks.

Plaintext
HELP
Ciphertext
HIAT
Key Matrix (K)
33
25

Ready to practice?

Try encrypting and decrypting your own messages with the Hill cipher tool.

Hill Cipher Examples - Learn Step by Step

Master the Hill cipher through comprehensive step-by-step examples that progress from simple 2x2 matrix encryption to advanced 3x3 implementations and cryptanalysis techniques. These examples provide complete calculations, demonstrating every stage of the encryption and decryption process. Follow along sequentially to build understanding, or jump directly to specific topics that interest you. All examples can be verified using our Hill cipher encoder and decoder tools.

Example 1: Basic 2x2 Hill Cipher Encryption

This foundational example demonstrates encrypting "HELP" using a simple 2x2 key matrix, perfect for understanding core Hill cipher principles.

Problem Setup

Plaintext: HELP Key Matrix:

K = [[3, 3],
     [2, 5]]

Step 1: Verify the key matrix is valid

Calculate the determinant: det(K) = (3 × 5) - (3 × 2) = 15 - 6 = 9

Check coprimality with 26: 9 shares no common factors with 26 (9 = 3², 26 = 2 × 13). Since GCD(9, 26) = 1, this is a valid key matrix.

Step 2: Convert plaintext to numbers

Using the standard mapping A=0, B=1, ..., Z=25:

  • H = 7
  • E = 4
  • L = 11
  • P = 15

Step 3: Form plaintext vectors

Split the text into blocks of 2 letters (matching the 2x2 matrix size):

  • Block 1: HE = [7, 4]
  • Block 2: LP = [11, 15]

Step 4: Encrypt first block (HE)

Multiply the key matrix by the plaintext vector:

[[3, 3],     [7]     [3×7 + 3×4]     [21 + 12]     [33]
 [2, 5]]  ×  [4]  =  [2×7 + 5×4]  =  [14 + 20]  =  [34]

Step 5: Apply modulo 26

[33, 34] mod 26 = [7, 8]

Step 6: Convert to letters

[7, 8] = [H, I]

Step 7: Encrypt second block (LP)

Multiply the key matrix by the second plaintext vector:

[[3, 3],     [11]     [3×11 + 3×15]     [33 + 45]     [78]
 [2, 5]]  ×  [15]  =  [2×11 + 5×15]  =  [22 + 75]  =  [97]

Apply modulo 26:

[78, 97] mod 26 = [0, 19]

Convert to letters: [0, 19] = [A, T]

Final Result: HELP → HIAT

This 2x2 hill cipher example demonstrates how matrix multiplication transforms plaintext systematically. Try this example yourself in our Hill cipher encoder to see the calculations in real-time.

Example 2: Basic 2x2 Hill Cipher Decryption

Decrypt the ciphertext "HIAT" back to "HELP" using the inverse matrix, demonstrating the complete decryption process step by step.

Problem Setup

Ciphertext: HIAT Original Key Matrix:

K = [[3, 3],
     [2, 5]]

Step 1: Calculate the inverse matrix

First, find the determinant (already calculated): det(K) = 9

Step 2: Find the modular inverse of the determinant

We need to find a number x such that (9 × x) ≡ 1 (mod 26).

Testing values:

  • 9 × 1 = 9 (mod 26) = 9
  • 9 × 2 = 18 (mod 26) = 18
  • 9 × 3 = 27 (mod 26) = 1 ✓

Therefore, det⁻¹ = 3

Step 3: Calculate the adjugate matrix

For a 2x2 matrix [[a, b], [c, d]], the adjugate is [[d, -b], [-c, a]]:

adj(K) = [[5, -3],
          [-2, 3]]

Apply mod 26 to negative values (-3 mod 26 = 23, -2 mod 26 = 24):

adj(K) = [[5, 23],
          [24, 3]]

Step 4: Compute the inverse matrix

Multiply the determinant inverse by the adjugate matrix:

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

Verification: K × K⁻¹ should equal the identity matrix [[1,0],[0,1]] mod 26.

Step 5: Convert ciphertext to numbers

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

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

Step 6: Decrypt first block (HI)

[[15, 17],     [7]     [15×7 + 17×8]     [105 + 136]     [241]
 [20, 9]]   ×  [8]  =  [20×7 + 9×8]   =  [140 + 72]   =  [212]

Apply modulo 26:

[241, 212] mod 26 = [7, 4] = [H, E]

Step 7: Decrypt second block (AT)

[[15, 17],     [0]      [15×0 + 17×19]     [0 + 323]     [323]
 [20, 9]]   ×  [19]  =  [20×0 + 9×19]   =  [0 + 171]  =  [171]

Apply modulo 26:

[323, 171] mod 26 = [11, 15] = [L, P]

Final Result: HIAT → HELP

This hill cipher decryption example shows how the inverse matrix perfectly reverses the encryption. Practice this in our Hill cipher decoder with step-by-step visualization.

Example 3: 3x3 Matrix Encryption

This advanced example demonstrates encrypting with a 3x3 matrix, which provides stronger security through better diffusion and larger key space.

Problem Setup

Plaintext: ACT Key Matrix:

K = [[6, 24, 1],
     [13, 16, 10],
     [20, 17, 15]]

Step 1: Verify determinant

For a 3x3 matrix, calculate using cofactor expansion:

det(K) = 6(16×15 - 10×17) - 24(13×15 - 10×20) + 1(13×17 - 16×20)
       = 6(240 - 170) - 24(195 - 200) + 1(221 - 320)
       = 6(70) - 24(-5) + 1(-99)
       = 420 + 120 - 99
       = 441

Apply mod 26: 441 mod 26 = 25

Check coprimality: GCD(25, 26) = 1 ✓ Valid key

Step 2: Convert plaintext to numbers

A = 0, C = 2, T = 19

Form vector: [0, 2, 19]

Step 3: Perform matrix multiplication

[[6, 24, 1],      [0]      [6×0 + 24×2 + 1×19]      [0 + 48 + 19]      [67]
 [13, 16, 10]  ×  [2]   =  [13×0 + 16×2 + 10×19]  =  [0 + 32 + 190]  =  [222]
 [20, 17, 15]]    [19]     [20×0 + 17×2 + 15×19]    [0 + 34 + 285]     [319]

Step 4: Apply modulo 26

[67, 222, 319] mod 26 = [15, 14, 7]

Step 5: Convert to letters

[15, 14, 7] = [P, O, H]

Final Result: ACT → POH

This 3x3 hill cipher example demonstrates increased complexity and security. The 3x3 matrix processes three letters simultaneously, providing better diffusion than 2x2 matrices. Use our matrix calculator to verify these complex calculations.

Example 4: Keyword to Matrix Conversion

Learn how to convert a memorable keyword into a valid Hill cipher key matrix, making key management more practical.

Problem: Convert keyword "HILL" to a 2x2 matrix

Step 1: Write out the keyword

Keyword: H I L L

Step 2: Convert letters to numbers

  • H = 7
  • I = 8
  • L = 11
  • L = 11

Step 3: Fill the matrix by rows

K = [[7, 8],
     [11, 11]]

Step 4: Validate the key matrix

Calculate determinant:

det(K) = (7 × 11) - (8 × 11) = 77 - 88 = -11

Apply mod 26: -11 mod 26 = 15

Check coprimality: GCD(15, 26) = 1 ✓ Valid key

The keyword "HILL" produces a valid encryption matrix.

Handling Different Keyword Lengths

Keyword shorter than needed (e.g., "HI" for 2x2)

If the keyword has fewer than n² letters, pad with additional letters from the alphabet:

  • Keyword: HI
  • Add: AB
  • Matrix: [[7, 8], [0, 1]]

Keyword longer than needed (e.g., "HILLCIPHER" for 2x2)

Take only the first n² letters:

  • Keyword: HILLCIPHER
  • Use: HILL
  • Matrix: [[7, 8], [11, 11]]

For 3x3 matrices

Need 9 letters. Example keyword: "CRYPTOGRA":

K = [[2, 17, 24],
     [15, 19, 14],
     [6, 17, 0]]

Remember to always validate the resulting matrix before use. Our Hill cipher calculator automatically handles keyword-to-matrix conversion with validation.

Example 5: Known-Plaintext Attack

This example demonstrates the Hill cipher's critical vulnerability, showing how an attacker can recover the secret key with minimal known plaintext-ciphertext pairs.

Attack Scenario

Intercepted ciphertext: "APADJ" Known plaintext: The first 4 characters of plaintext are "HELL"

Attack Setup

Known information:

  • Plaintext blocks: HE, LL → [7,4], [11,11]
  • Corresponding ciphertext: AP, AD → [0,15], [0,3]

Attack Process

Step 1: Build the plaintext matrix P

P = [[7, 11],
     [4, 11]]

Each column represents one plaintext block.

Step 2: Build the ciphertext matrix C

C = [[0, 0],
     [15, 3]]

Each column represents the corresponding ciphertext block.

Step 3: Set up the equation

We know that C = K × P (mod 26), so K = C × P⁻¹ (mod 26)

Step 4: Calculate P⁻¹

Find determinant: det(P) = (7 × 11) - (11 × 4) = 77 - 44 = 33 ≡ 7 (mod 26)

Find modular inverse of 7 mod 26: 7⁻¹ = 15 (since 7 × 15 = 105 ≡ 1 mod 26)

Calculate adjugate: adj(P) = [[11, -11], [-4, 7]] ≡ [[11, 15], [22, 7]] (mod 26)

Compute inverse:

P⁻¹ = 15 × [[11, 15],     [[165, 225],     [[9, 17],
             [22, 7]]  =   [330, 105]]  ≡   [18, 1]] (mod 26)

Step 5: Recover the key matrix

K = C × P⁻¹ (mod 26)

K = [[0, 0],     [[9, 17],     [[0, 0],
     [15, 3]] ×   [18, 1]]  =   [189, 258]]

Apply mod 26:

K = [[0, 0],
     [7, 24]]

Step 6: Decrypt remaining ciphertext

With the recovered key, calculate K⁻¹ and decrypt the final character "J" to determine the complete plaintext.

Educational Significance

This known-plaintext attack example demonstrates why the Hill cipher is not suitable for modern secure communications. Only 4 characters (2 blocks for 2x2) of known plaintext-ciphertext pairs are sufficient to completely break the cipher. The attack works because Hill cipher encryption is entirely linear, creating a solvable system of linear equations.

For 3x3 matrices, you would need 9 known characters (3 blocks) to perform the same attack. Try this attack method in our decoder's known-plaintext attack mode to see it in action.

Mathematical Deep Dive

Determinant Calculation Methods

2x2 Determinant Formula

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

det(K) = ad - bc

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

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

Apply mod 26: 9 mod 26 = 9

3x3 Determinant Using Cofactor Expansion

For matrix [[a,b,c],[d,e,f],[g,h,i]]:

det(K) = a(ei - fh) - b(di - fg) + c(dh - eg)

Example with [[6,24,1],[13,16,10],[20,17,15]]:

det = 6(16×15 - 10×17) - 24(13×15 - 10×20) + 1(13×17 - 16×20)
    = 6(240-170) - 24(195-200) + 1(221-320)
    = 6(70) - 24(-5) + 1(-99)
    = 420 + 120 - 99
    = 441 ≡ 25 (mod 26)

Modular Inverse Calculation

Definition: The modular inverse a⁻¹ mod n is a number x such that (a × x) ≡ 1 (mod n)

Extended Euclidean Algorithm Method

To find 9⁻¹ mod 26:

  1. Apply the algorithm:

    • 26 = 2 × 9 + 8
    • 9 = 1 × 8 + 1
    • 8 = 8 × 1 + 0
  2. Back-substitute to express 1 as a linear combination:

    • 1 = 9 - 1 × 8
    • 1 = 9 - 1 × (26 - 2 × 9)
    • 1 = 3 × 9 - 1 × 26
  3. Therefore: 9⁻¹ ≡ 3 (mod 26)

Common Modular Inverses mod 26

Quick reference table:

  • 1⁻¹ = 1
  • 3⁻¹ = 9
  • 5⁻¹ = 21
  • 7⁻¹ = 15
  • 9⁻¹ = 3
  • 11⁻¹ = 19
  • 15⁻¹ = 7
  • 17⁻¹ = 23
  • 19⁻¹ = 11
  • 21⁻¹ = 5
  • 23⁻¹ = 17
  • 25⁻¹ = 25

Numbers without modular inverse mod 26: 2, 4, 6, 8, 10, 12, 13, 14, 16, 18, 20, 22, 24 (all share common factors with 26)

Adjugate Matrix Calculation

2x2 Adjugate Formula

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

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

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

adj(K) = [[5, -3],
          [-2, 3]]

Apply mod 26 to negative values:

adj(K) = [[5, 23],
          [24, 3]]

3x3 Adjugate Formula

The adjugate is the transpose of the cofactor matrix. Each cofactor is calculated by:

  1. Remove the row and column of the element
  2. Calculate the determinant of the remaining 2x2 matrix
  3. Apply the sign pattern: [[+,-,+],[-,+,-],[+,-,+]]

This process is complex for 3x3 matrices. Use our matrix calculator to compute 3x3 adjugate matrices automatically.

Practice Problems with Solutions

Problem 1: Encrypt with 2x2 Matrix

Given:

  • Key: [[5, 8], [17, 3]]
  • Plaintext: "COLD"

Find: Ciphertext

Click to reveal solution

Solution:

  1. Verify key: det = (5×3) - (8×17) = 15 - 136 = -121 ≡ 9 (mod 26) ✓

  2. Convert: C=2, O=14, L=11, D=3

  3. Blocks: [2,14], [11,3]

  4. Encrypt block 1:

    • [[5,8],[17,3]] × [2,14] = [122, 76] ≡ [18, 24] = [S, Y]
  5. Encrypt block 2:

    • [[5,8],[17,3]] × [11,3] = [79, 196] ≡ [1, 14] = [B, O]

Answer: COLD → SYBO

Problem 2: Find Inverse Matrix

Given: Key matrix [[3,2],[5,7]]

Find: Inverse matrix mod 26

Click to reveal solution

Solution:

  1. det = (3×7) - (2×5) = 21 - 10 = 11

  2. det⁻¹: Find x where 11x ≡ 1 (mod 26)

    • 11 × 19 = 209 = 8×26 + 1 ≡ 1 (mod 26)
    • So det⁻¹ = 19
  3. adj = [[7,-2],[-5,3]] ≡ [[7,24],[21,3]] (mod 26)

  4. K⁻¹ = 19 × [[7,24],[21,3]] = [[133,456],[399,57]] ≡ [[3,14],[9,5]] (mod 26)

Answer: K⁻¹ = [[3,14],[9,5]]

Problem 3: Decrypt with 3x3 Matrix

Given:

  • Key: [[2,4,5],[9,2,1],[3,17,7]]
  • Ciphertext: "XYZ"

Find: Plaintext

Click to reveal solution

Solution: This problem requires calculating the 3x3 inverse matrix, which involves complex cofactor calculations. The determinant is 111 ≡ 7 (mod 26), and 7⁻¹ = 15 (mod 26). Using the full adjugate matrix computation and applying the inverse to vectors [23,24,25], you can decrypt the ciphertext. Use our Hill cipher decoder to verify this 3x3 decryption automatically.

Problem 4: Determine Matrix Validity

Given: Matrix [[2,3],[4,5]]

Find: Is this a valid Hill cipher key?

Click to reveal solution

Solution:

  1. Calculate determinant: det = (2×5) - (3×4) = 10 - 12 = -2 ≡ 24 (mod 26)

  2. Check coprimality: GCD(24, 26) = 2

Answer: Invalid. The determinant 24 shares a common factor (2) with 26, making the matrix non-invertible mod 26. Choose a different matrix.

Problem 5: Advanced Challenge

Given: You intercepted ciphertext "KQOWEFVJP" and know it was encrypted with a 3x3 Hill cipher. You discovered the first three letters of plaintext are "THE".

Find: Can you recover the key and decrypt the full message?

Click to reveal solution

Hint: You need 9 letters (3 blocks) for a complete 3x3 attack. With only one block known, you cannot uniquely determine the 9-element key matrix. You would need two more known blocks to perform the attack successfully. This demonstrates that 3x3 Hill ciphers require more known plaintext than 2x2 for cryptanalysis.

Frequently Asked Questions

Where can I find Hill cipher examples with solutions?

This page provides comprehensive Hill cipher examples with complete step-by-step solutions, including 2x2 and 3x3 encryption and decryption, keyword-to-matrix conversion, known-plaintext attack demonstrations, and detailed mathematical calculations. All examples include verification steps and can be tested using our online Hill cipher tools. Practice problems with solutions help reinforce learning through hands-on application.

How to solve Hill cipher step by step?

To solve Hill cipher encryption step by step: Convert each letter to its numeric value (A=0 through Z=25), form the key matrix and ensure it's valid by checking determinant coprimality, divide plaintext into blocks matching matrix dimensions, form column vectors from each block, perform matrix multiplication between key and plaintext vectors, apply modulo 26 to all results, and convert resulting numbers back to letters. See Example 1 above for a complete walkthrough with every calculation shown in detail.

Hill cipher 2x2 example with solution?

Example 1 demonstrates encrypting "HELP" to "HIAT" using key matrix [[3,3],[2,5]], showing all matrix multiplication steps, modulo operations, and conversions. Example 2 shows the reverse decryption process from "HIAT" back to "HELP", including inverse matrix calculation using determinant, modular inverse, and adjugate matrix methods. Both examples provide every calculation step in complete detail, making them perfect for learning 2x2 hill cipher operations.

Hill cipher 3x3 example?

Example 3 demonstrates encrypting "ACT" to "POH" using a 3x3 key matrix, showing the more complex cofactor expansion method for determinant calculation, 3x3 matrix multiplication with three-element vectors, and the increased security benefits of larger matrices. The 3x3 hill cipher example illustrates how processing three letters simultaneously provides better diffusion and larger key space compared to 2x2 matrices, though with increased computational complexity.

Hill cipher example with known plaintext attack?

Example 5 demonstrates a complete known-plaintext attack where an attacker recovers the secret key matrix using only 4 known characters (two blocks) of matching plaintext-ciphertext pairs. The example shows how to build plaintext and ciphertext matrices, calculate the inverse plaintext matrix, and multiply to recover the original key. This example reveals the Hill cipher's fundamental vulnerability to this type of attack, explaining why it's unsuitable for modern secure communications despite its mathematical elegance.

How to convert keyword to Hill cipher matrix?

Example 4 shows the complete process of converting keyword "HILL" into a 2x2 matrix: write the keyword letters, convert each letter to its numeric value (H=7, I=8, L=11, L=11), fill the matrix by rows to create [[7,8],[11,11]], and validate by checking determinant coprimality. The example also covers handling keywords shorter or longer than needed, and demonstrates 3x3 keyword conversion. Always validate the resulting matrix before use to ensure it's invertible mod 26.

Verify all these examples using our Hill cipher calculator and practice with different matrices to build mastery.