Hill Cipher 希尔密码 已知明文密文 求秘钥方法

Hill Cipher,已知明文密文,求秘钥

设K为秘钥,

P=\begin{pmatrix} 5&8 \\ 17 & 3 \end{pmatrix}为明文字母对应的数字矩阵,

C=\begin{pmatrix} 15&2 \\ 16 & 5 \end{pmatrix}为密文字母对应的数字矩阵。

C=KPmod26是希尔加密的过程。

为了求解K,我们需要求解P的膜26逆元。

注意,这里是膜26逆元,不是直接求逆元

由于P^{-1}=\left | P \right |^{-1}\cdot P^{*}

P^{-1}mod26=(\left | P \right |^{-1}mod26\cdot P^{*})mod26

又由\left | P \right |^{-1}=5*3-8*17=-121

P^{*}=\begin{pmatrix} 3 & -8\\ -17 & 5 \end{pmatrix}

所以

P^{-1}mod26

=({\color{Red} \left | P \right |^{-1}mod26}\cdot P^{*})mod26

=({\color{Red} 3}P^{*})mod26

=\begin{pmatrix} 9 & -24\\ -51&15 \end{pmatrix}mod26

=\begin{pmatrix} 9 & 2\\ 1&15 \end{pmatrix}

完。

 

上文中,红色部分的求解过程:

n=-\frac{1}{121}mod26

-121n=1mod26

-121n mod26=1

也就是说,26x-121n=1,其中x和n都是正整数,且n\in \left [ 0 ,25\right ],可以尝试得到n=3,x=14。

 

最近再学信息安全,讲到了这个部分,但是并没有将如何计算这个逆矩阵,自己查了很多资料(线代忘得比较干净),终于会了

敲公式不易,转载注明出处

 

参考:

https://www.cnblogs.com/lcbwwy/p/13125084.html

https://jingyan.baidu.com/article/925f8cb8a74919c0dde056e7.html

https://zhuanlan.zhihu.com/p/82035891

https://baike.baidu.com/item/%E4%BC%B4%E9%9A%8F%E7%9F%A9%E9%98%B5/10034983?fr=aladdin

 

 

 

 

  • 13
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
为了回答你的问题,我需要先了解一下 Hill 密码。Hill 密码是一种基于矩阵运算的对称密码,它将明文分成若干个数字组合,然后通过矩阵运算进行加密。因此,我们需要使用矩阵计算的工具来进行 Hill 密码的加密和解密。 下面是使用 Python 和 NumPy 库实现 Hill 密码的加密和解密的示例代码: ```python import numpy as np # 加密函数 def encrypt(plain_text, key): # 将明文转换成数字向量 plain_text = [ord(c) - ord('a') for c in plain_text] plain_text = np.array(plain_text) # 将密钥转换成矩阵 key = np.array(key) # 将数字向量按照密钥的大小分成若干个块 block_size = key.shape[0] num_blocks = int(np.ceil(len(plain_text) / block_size)) plain_text = np.pad(plain_text, (0, num_blocks * block_size - len(plain_text)), 'constant') plain_text = plain_text.reshape(-1, block_size) # 对每个块进行加密 cipher_text = [] for block in plain_text: cipher_block = np.mod(key @ block, 26) cipher_text += cipher_block.tolist() # 将密文转换成字符串 cipher_text = ''.join([chr(c + ord('a')) for c in cipher_text]) return cipher_text # 解密函数 def decrypt(cipher_text, key): # 将密文转换成数字向量 cipher_text = [ord(c) - ord('a') for c in cipher_text] cipher_text = np.array(cipher_text) # 将密钥转换成矩阵 key = np.array(key) # 将数字向量按照密钥的大小分成若干个块 block_size = key.shape[0] num_blocks = int(np.ceil(len(cipher_text) / block_size)) cipher_text = np.pad(cipher_text, (0, num_blocks * block_size - len(cipher_text)), 'constant') cipher_text = cipher_text.reshape(-1, block_size) # 对每个块进行解密 plain_text = [] for block in cipher_text: # 计算矩阵的逆 det = np.linalg.det(key) inv_key = np.mod(np.round(det * np.linalg.inv(key)), 26) plain_block = np.mod(inv_key @ block, 26) plain_text += plain_block.tolist() # 将明文转换成字符串 plain_text = ''.join([chr(c + ord('a')) for c in plain_text]) return plain_text # 测试代码 key = [[3, 10], [20, 9]] plain_text = 'hello' cipher_text = encrypt(plain_text, key) print(cipher_text) plain_text = decrypt(cipher_text, key) print(plain_text) ``` 在这个示例中,我们定义了两个函数 `encrypt` 和 `decrypt` 分别实现了 Hill 密码的加密和解密。函数的参数 `plain_text` 表示明文字符串,`cipher_text` 表示密文字符串,`key` 表示 Hill 密钥矩阵。在加密和解密函数中,我们都采用了相同的方式把明文密文分成若干个块,并对每个块进行加密或解密。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

字江慕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值