Introduction
Hill cipher is an alternative cipher that uses the principles of basic matrix theory, invented by Lester S. Hill in 1929.
Each letter is treated as a 26-base digit: A=0, B=1, C=2... A string of letters is treated as an n-dimensional vector, multiplied by an n×n matrix, and the result is modulo 26. (Note that the matrix used for encryption (that is, the key) must be invertible, otherwise it will be impossible to decode. Only the determinant of the matrix and the 26 coprime are invertible.)
example:
Use Hill password to encrypt the plaintext string x = EastChinaNormalUniversity ,
Key matrix:
encryption:
Ciphertext vector = plaintext vector * key matrix (mod 26)
-
First convert the plaintext string corresponding to the English letter code table for number conversion 4 0 18 19 2 7 8 13 0 13 14 17 12 0 11 20 13 8 21 4 17 18 8 19 24
and then write two by two into a matrix form:
I went and found If one is missing, can't the teacher put together a question? ? ? In this way, we will make 0 processing. -
Next, start encrypting. After
obtaining the ciphertext matrix, turn it into letters according to the vector corresponding to the group:
IK BX NB DH NN JD YE SR OB KB UJ HL W
Python implementation:
import numpy as np
def encode(string, size):
# 转换小写字母
if not string.islower():
string = string.lower()
# 分成 size个 字的分段
blocks = [string[i:i+size] for i in range(0, len(string), size)]
# 明文字串与密钥矩阵阶数不整除。。字串补a
if len(blocks[-1]) != size:
blocks[-1] = blocks[-1].ljust(size,'a')
# 将 a-z 编码为 0-25
temp = np.array([list(map(ord, block)) for block in blocks]) - ord('a')
# print(temp)
return temp
def analysis(crypter, code):
return ((crypter @ code.T) % 26).T + ord('a')
# 要加密的信息
encode_msg = 'eastchinanormaluniversity'.lower()
print('待加密的信息:'+encode_msg)
# 密钥
encryption_matrix = np.array([[2, 5], [9, 5]])
print('密钥:')
print(encryption_matrix)
# 加密代码
encrypted_code = analysis(encryption_matrix, encode(encode_msg, 2))
# 密文
Decryption_matrixtext = ''.join(map(chr, encrypted_code.ravel()))
print("密文:" + Decryption_matrixtext[:len(encode_msg)].upper())
"""
待加密的信息:eastchinanormaluniversity
密钥:
[[2 5]
[9 5]]
密文:IKBXNBDHNNJDYESROBKBUJHLW
"""
Decrypt
References that will not invert the matrix inversion defined on Zm
Decryption is similar to encryption. First calculate the inverse matrix modulo m by the method just now, and then use A−1 to decrypt:
The method of finding the inverse matrix: the inverse matrix of the
actual second-order matrix is:
mod 26, and then use the same method as above to convert it into a plaintext string.
Analysis and cracking
Example:
Suppose that the known plaintext friday is encrypted with the Hill cipher of n=2 to obtain the ciphertext VYUZSM, and find the secret key K
n is equal to 2, indicating that friday is divided into 3 segments, and the matrix is 2*2
According to the correspondence between letters and integers:
(5,17)---->(21,24),
(8,3)---->(20,25),
(0,24)—>(18, 12), get
Use (0,24)—>(18,12) to verify K:
correct! Get the key K and just crack the ciphertext