I am doing a hill cipher encryption and decryption program, the encryption is good but I can't determine what's wrong with the decryption

huangapple go评论87阅读模式
英文:

I am doing a hill cipher encryption and decryption program, the encryption is good but I can't determine what's wrong with the decryption

问题

以下是代码的翻译部分:

def hill_encrypt(plain_text, key_cipher):
    n = len(key_cipher)
    plain_text = plain_text.upper().replace(' ', '')
    if len(plain_text) % n != 0:
        plain_text += 'X' * (n - len(plain_text) % n)
    encrypted_text = ''
    for i in range(0, len(plain_text), n):
        block = plain_text[i:i+n]
        block_nums = np.array([char_to_num(c) for c in block])
        encrypted_block_nums = np.dot(key_cipher, block_nums) % 26
        encrypted_block = ''.join([num_to_char(num) for num in encrypted_block_nums])
        encrypted_text += encrypted_block
    return encrypted_text

name = 'HELLO'
key = [[2, 4, 8], [14, 23, 14], [10, 20, 17]]
name_encrypted = hill_encrypt(name, key)
print('Encrypted Text:', name_encrypted)

inverse_key = np.linalg.inv(key)
print(inverse_key)

def hill_decrypt(key_cipher, K_inv):
    n = len(K_inv)
    plain_text = ''
    for i in range(0, len(key_cipher), n):
        block = key_cipher[i:i+n]
        block_nums = np.array([char_to_num(c) for c in block])
        decrypted_block_nums = np.dot(K_inv, block_nums) % 26
        decrypted_block_nums = decrypted_block_nums.astype(int)
        decrypted_block = ''.join([num_to_char(num) for num in decrypted_block_nums])
        plain_text += decrypted_block
    return plain_text

key_inv = inverse_key
name_decrypted = hill_decrypt(name_encrypted, key_inv)
print('Decrypted Text:', name_decrypted)

Inverse Key from the Inverse key calculation:

[[ 4.82608696e-01  4.00000000e-01 -5.56521739e-01]
 [-4.26086957e-01 -2.00000000e-01  3.65217391e-01]
 [ 2.17391304e-01 -1.36191955e-17 -4.34782609e-02]]

'HELLO' 加密后返回如下:

Encrypted Text: OGZCSB

但解密版本返回的是已加密字符串文本的进一步加密:

Decrypted Text: VBBHVA

我尝试了一个更长的方法,使用for循环迭代每个元素,每个行和列,但结果是一样的。我认为错误在于迭代的方式,但我找不到错误。

英文:

This is the entire code

    def hill_encrypt(plain_text, key_cipher):
        n = len(key_cipher)
     plain_text = plain_text.upper().replace(' ', '')
     if len(plain_text) % n != 0: 
     plain_text += 'X' * (n - len(plain_text) % n)
     encrypted_text = ''
     for i in range(0, len(plain_text), n):
            block = plain_text[i:i+n]
     block_nums = np.array([char_to_num(c) for c in block])
     encrypted_block_nums = np.dot(key_cipher, block_nums) % 26
            
    encrypted_block = ''.join([num_to_char(num) for num in encrypted_block_nums])
    encrypted_text += encrypted_block
    return encrypted_text
    
    name = 'HELLO' key = [[2, 4, 8], [14, 23, 14], [10, 20, 17]] 
    name_encrypted = hill_encrypt(name, key) 
    print('Encrypted Text:', name_encrypted)
    
    inverse_key = np.linalg.inv(key) print(inverse_key)
    
    def hill_decrypt(key_cipher, K_inv): n = len(K_inv)
    
    plain_text = ''
    
    for i in range(0, len(key_cipher), n):
        block = key_cipher[i:i+n]
        block_nums = np.array([char_to_num(c) for c in block])
        decrypted_block_nums = np.dot(K_inv, block_nums) % 26
        decrypted_block_nums = decrypted_block_nums.astype(int)
        decrypted_block = ''.join([num_to_char(num) for num in decrypted_block_nums])
        plain_text += decrypted_block
    
    return plain_text
    
    key_inv = inverse_key 
    name_decrypted = hill_decrypt(name_encrypted, key_inv) 
    print('Decrypted Text:', name_decrypted)

'''Inverse Key from the Inverse key calculation: 
[[ 4.82608696e-01  4.00000000e-01 -5.56521739e-01] 
[-4.26086957e-01 -2.00000000e-01  3.65217391e-01] 
[ 2.17391304e-01 -1.36191955e-17 -4.34782609e-02]]'''

'HELLO' encrypted returns as follows

Encrypted Text: OGZCSB

But the decrypted version returns as a further encryption of the already encrypted string text

Decrypted Text: VBBHVA 

I have tried a longer method of iterating over the blocks using a for loop to go over each element at each row and column but the return is the same. I think the error is in the way I iterate but I can't seem to find.

答案1

得分: 2

你不能使用 np.linalg.key 来获取 Hill 密码的解密密钥。Hill 密码使用模 26 算术。解密矩阵是完全由整数组成的矩阵,当与加密矩阵相乘时得到单位矩阵。

我不知道在 Python 中有任何简单的计算方法。

英文:

You cannot use np.linalg.key to get the decryption key of a Hill cipher. Hill ciphers work in mod 26 arithmetic. The decryption matrix is the matrix that consists entirely of integers, than when multiplied by the encryption matrix gives the identity matrix.

I do not know of any easy way of calculating this in Python.

huangapple
  • 本文由 发表于 2023年6月22日 07:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定