1001Ferramentas
🧮 Security

Hill Cipher 2×2

Encrypt text with a 2×2 matrix mod 26 (Hill cipher) — algebraic explanation and key invertibility check.

Linear algebra mod 26, hands on

The Hill cipher usually shows up right after matrix inverses, and what wrecks the exercise is almost never the multiplication. It is the modulus: taking the determinant mod 26, deciding whether it has an inverse, and finding that inverse by hand. One flipped sign along the way and your ciphertext disagrees with the answer key, with no clue about where the arithmetic went off.

The text is split into letter pairs, each pair becomes a vector (x, y), and it is multiplied by the matrix [[a, b], [c, d]] modulo 26. With the key 3, 3, 2, 5 the word HELP becomes HI AT, and the page reports det(K) mod 26 = 9 along with its inverse 3, since 9 × 3 = 27, which is 1 mod 26. The rule that governs everything: the key is invertible only when gcd(det, 26) = 1. Because 26 = 2 × 13, an even determinant or a multiple of 13 kills the key.

Note that the tool still encrypts after warning that the key is not invertible: you get ciphertext, it just has no unique inverse. Read the determinant line before committing to a key. It does not decrypt either: compute the inverse, det⁻¹ × [[d, −b], [−c, a]] mod 26, and enter that matrix instead, since decryption is the same multiplication. An odd number of letters gets padded with a trailing X, which will show up when you decrypt. All of it runs in the browser.

Frequently asked questions

How do I know my matrix works as a key?
Its determinant mod 26 must be coprime with 26, meaning neither even nor a multiple of 13. The page computes the determinant and shows the modular inverse when one exists.
Can it decrypt?
No. Work out the inverse matrix mod 26 and type its four entries into the fields: encrypting with the inverse is exactly what decryption does.
Where did the trailing X come from?
A 2×2 Hill cipher works on pairs. When the letter count is odd, an X is appended as padding to complete the final pair.

Related Tools