Hill Cipher — Matrix Encryption
Encrypt text using the Hill cipher with a 2×2 or 3×3 key matrix. Based on linear algebra modulo 26. Includes matrix inverse for decryption.
How It Works
The Hill cipher multiplies a letter vector by the key matrix mod 26. For decryption, the modular inverse of the matrix is used.
How to Use the Hill Cipher — Matrix Encryption
- Enter or paste your text in the input field.
- The encoded or decoded result appears automatically.
- Copy the output using the copy button.
- Switch between encode and decode modes if available.
快速参考
| 从 | 到 |
|---|---|
| A | 65 (ASCII) |
| Z | 90 (ASCII) |
| a | 97 (ASCII) |
| 0 | 48 (ASCII) |
| Space | 32 (ASCII) |
| @ | 64 (ASCII) |
使用场景
- •Encoding or decoding data for web development projects.
- •Learning about cryptography and different cipher techniques.
- •Preparing encoded content for APIs or data transmission.
- •Solving cipher puzzles, CTF challenges, or educational exercises.
公式
The Hill cipher multiplies a vector of letter indices (A=0…Z=25) by the key matrix modulo 26. Decryption uses the matrix inverse mod 26. The key matrix must be invertible mod 26.
常见问题
What is the Hill cipher?
The Hill cipher is a polygraphic substitution cipher based on linear algebra. It encrypts blocks of n letters by multiplying a vector of their indices (A=0…Z=25) by an n×n key matrix modulo 26.
What makes a key matrix valid?
The key matrix must be invertible modulo 26. This means its determinant must be coprime with 26 (i.e., gcd(det, 26) = 1). If the matrix is not invertible mod 26, decryption is impossible.
How does 2×2 vs 3×3 affect encryption?
A 2×2 matrix encrypts pairs of letters at a time; a 3×3 matrix encrypts triplets. Larger matrices provide better diffusion and stronger (though still breakable) encryption.
Why is padding added?
The plaintext length must be a multiple of the matrix size. If it is not, the letter X is appended until the length fits. This X padding is standard practice for the Hill cipher.
Can the Hill cipher be broken?
Yes. A known-plaintext attack can recover the key matrix with only n plaintext-ciphertext pairs (where n is the block size). Without knowing any plaintext, frequency analysis of blocks is used.