Hill Cipher
Another interesting multi letter cipher is the Hill cipher, developed by the mathematician Lester Hill in 1929.
THE HILL ALGORITHM
This encryption algorithm takes n successive plaintext letters and substitutes for them n ciphertext letters. The substitution is determined by linear equations in which each character is assigned a numerical value (a=0,b=1,…,z=25).
C=KP, where K is the key matrix and P is the plaintext vector
P=K−1C, where K−1 is the inverse of key matrix K in \pmod{26}
How the Hill Cipher WorksKey Matrix:
- Choose a key matrix
K
of size (e.g., , ). - The key matrix should be invertible modulo 26 (the number of letters in the English alphabet).
- Choose a key matrix
Plaintext Preparation:
- Convert the plaintext into vectors of size .
- If the plaintext length is not a multiple of , pad it with extra letters (e.g., 'X').
Encryption:
- Multiply the key matrix by each plaintext vector, and take the result modulo 26 to get the ciphertext vector.
- Convert the resulting numbers back into letters.
Decryption:
- Calculate the inverse of the key matrix modulo 26.
- Multiply the inverse matrix by each ciphertext vector, and take the result modulo 26 to get the plaintext vector.
- Convert the resulting numbers back into letters.
Example
Let's encrypt the plaintext "HELP" using a key matrix.
Step 1: Key Matrix
Choose the key matrix:
Step 2: Plaintext Preparation
Convert "HELP" into numbers: H=7, E=4, L=11, P=15
Since our key matrix is , divide the plaintext into pairs:
- (H, E) -> (7, 4)
- (L, P) -> (11, 15)
Step 3: Encryption
Encrypt each pair using matrix multiplication and take modulo 26.
For (H, E):
Take modulo 26:
Convert back to letters: (7, 8) -> HI
For (L, P):
Take modulo 26:
Convert back to letters: (0, 19) -> AT
So, the ciphertext for "HELP" is "HIAT"
Step 4: Decryption
To decrypt, we need the inverse of the key matrix modulo 26.
Find the determinant of :
Find the modular inverse of the determinant modulo 26, which is 3 (since
Compute the inverse matrix:
Decrypt the ciphertext "HIAT" using K^{-1}
- Convert "HIAT" to numbers: H=7, I=8, A=0, T=19
For (H, I):
Take modulo 26:
Convert back to letters: (7, 4) -> HE
For (A, T):
Take modulo 26:
Convert back to letters: (11, 15) -> LP
The decrypted plaintext is "HELP".
Python Code Hill Cipher
Comments
Post a Comment