🔐 Hill Cipher – Matrix-Based Cryptography
The Hill Cipher is a classical polygraphic substitution cipher invented by mathematician Lester S. Hill in 1929. Unlike simple letter-for-letter substitution ciphers, the Hill Cipher encrypts blocks of n letters simultaneously using linear algebra — making it one of the most mathematically elegant classical ciphers and a foundational example of applying matrix arithmetic to cryptography.
How the Hill Cipher Works
Encryption is performed in three steps. First, each letter in the plaintext is mapped to a number using the standard alphabet index: A=0, B=1, C=2, … Z=25. Second, the text is split into blocks of length n (where n is the matrix dimension). Third, each block is treated as a column vector and multiplied by the key matrix modulo 26:
C = K × P (mod 26)Where K is the n×n key matrix, P is the plaintext vector, and C is the resulting ciphertext vector. Decryption reverses the process using the modular inverse of the key matrix:
P = K⁻¹ × C (mod 26)Key Matrix and Validity
Not every matrix can be used as a Hill Cipher key. The matrix must be invertible modulo 26, which requires:
- The determinant
det(K)must be non-zero modulo 26. gcd(det(K) mod 26, 26) = 1— the determinant must be coprime with 26.
Since 26 = 2 × 13, a valid determinant must not be divisible by 2 or 13. The tool automatically validates your key matrix and displays a ✅ Valid or ❌ Invalid badge in real time.
Computing the Matrix Inverse (mod 26)
For a 2×2 matrix K = [[a,b],[c,d]]:
det(K) = ad − bc
det⁻¹ = modular_inverse(det mod 26, 26)
K⁻¹ = det⁻¹ × [[d, −b], [−c, a]] (mod 26)For a 3×3 matrix, the tool computes the adjugate matrix (transpose of the cofactor matrix) and scales it by det⁻¹ mod 26. The modular inverse of the determinant is found using the extended Euclidean algorithm.
Step-by-Step Block Processing
Enable the Show Steps toggle to see a detailed breakdown of how each block is processed:
- Block splitting — input text is divided into n-grams; padding character (default
X) is appended if needed. - Vector conversion — each letter block is converted to a numeric column vector.
- Matrix multiplication — the vector is multiplied by the key matrix (or its inverse for decryption).
- Modular reduction — each element is reduced mod 26.
- Letter reconstruction — numeric values are mapped back to uppercase letters.
Practical Example
To encrypt ACT with the 3×3 key matrix [[6,24,1],[13,16,10],[20,17,15]]:
- Convert:
A=0, C=2, T=19 - Multiply:
K × [0,2,19]ᵀ mod 26 - Result:
[15,14,7]ᵀ→POH
Alphabet Index Reference
The Hill Cipher uses the standard 26-letter mapping: A=0, B=1, C=2, D=3, E=4, F=5, G=6, H=7, I=8, J=9, K=10, L=11, M=12, N=13, O=14, P=15, Q=16, R=17, S=18, T=19, U=20, V=21, W=22, X=23, Y=24, Z=25. Only letters are processed — spaces and punctuation are either stripped or preserved depending on your settings.
Educational Use & Limitations
The Hill Cipher is not secure for protecting real data. It is vulnerable to known-plaintext attacks: an attacker who knows just n plaintext-ciphertext block pairs can recover the key matrix by solving a system of linear equations. For modern secure encryption, use AES-256 or other contemporary standards. The Hill Cipher remains invaluable for teaching linear algebra applications, modular arithmetic, and the foundations of modern cryptography.