【发布时间】:2018-04-05 10:40:18
【问题描述】:
标题可能不正确,我不确定如何表达我的问题。
我正在尝试使用 Python3.6 编写一种非对称密码,我相信它类似于用于 RSA 加密通信的密码
我对此的逻辑理解如下:
Person1 (p1) picks two prime numbers say 17 and 19
let p = 17 and q = 19
the product of these two numbers will be called n (n = p * q)
n = 323
p1 will then make public n
P1 will then make public another prime called e, e = 7
Person2(p2) wants to send p1 the letter H (72 in Ascii)
To do this p2 does the following ((72 ^ e) % n) and calls this value M
M = 13
p2 sends M to p1
p1 receives M and now needs to decrypt it
p1 can do this by calculating D where (e^D) % ((p-1)*(q-1)) = 1
In this example i know D = 247
With D p1 can calculate p2 message using M^D % n
which successfully gives 72 ('H' in ASCII)
话虽如此,以下规则必须适用:
GCD(e,m) = 1
在哪里m = ((p-1)*(q-1))
否则(e^D) % ((p-1)*(q-1)) = 1 不存在。
现在问题来了! :)
在数字不太容易处理的情况下计算 D。
现在请告诉我是否有更简单的方法来计算 D,但这是我开始使用在线帮助的地方。
(我在网上看到的例子使用了不同的值,所以它们如下:
p=47
q=71
n = p*q = 3337
(p-1)*(q-1) = 3220
e = 79
现在我们必须找到 D。我们知道 (e^D) % ((p-1)*(q-1)) = 1
因此 D = 79^-1 % 3220
方程改写为 79*d = 1 mod 3220
这就是我感到困惑的地方
使用常规欧几里得算法 gcd(79,3220) 必须等于 1 否则实际上可能没有解决方案(我的描述是否正确?)
3220 = 40*79 + 60 (79 goes into 3220 40 times with remainder 60)
79 = 1*60 + 19 (The last remainder 60 goes into 79 once with r 19)
60 = 3*19 + 3 (The last remainder 19 goes into 60 three times with r 3)
19 = 6*3 + 1 (The last remainder 3 goes into 19 6 times with r 1)
3 = 3*1 + 0 (The last remainder 1 goes into 3 three times with r 0)
最后一个非零余数是 gcd。因此 gcd(79,3220) = 1 (应该是)
这里的最后一步我不知道到底发生了什么
我被告知通过备份树将 gcd(one) 写为 19 和 3220 的线性组合...
1 = 19-6*3
= 19-6*(60-3*19)
= 19*19 - 6*60
= 19*(79-60) - 6*60
= 19*79 - 25*60
= 19*79 - 25*(3220-40*79)
= 1019*79 - 25*3220
在此之后,我留下了1019*79 - 25*3220 = 1,如果我两边都是 mod 3220,我得到 1019*79 = 1 mod 3220
(包含 3220 的术语消失了,因为 3220 = 0 mod 3220)。
因此 d = 1019。
【问题讨论】:
-
我现在没时间正确回答。谷歌搜索“扩展欧几里得算法”和“模逆”和“Bézout 恒等式”来学习一些有趣的数学和问题的答案。
-
所以 d=1019。有什么问题?
标签: algorithm python-3.x encryption rsa linear-algebra