使用扩展欧几里德算法创建RSA私钥

时间:2013-09-22 04:06:48

标签: python algorithm encryption rsa modular-arithmetic

这是我在学校做的一项任务。我在生成私钥时遇到问题。我的主要问题是理解我的方程式之间的关系。为了设置一切,我们有:

p = 61
q = 53
n = p * q (which equals 3233)

从这里我们得到n(phi(n))的总数等于3120,现在我们可以选择素数e;其中1 < e&lt; 3120

e = 17

好的很容易。

对于我的作业,我们已经意识到d = 2753,但我仍然需要能够随意生成此值。

现在这里是我遇到麻烦的地方。我一直在仔细阅读维基百科以及某些东西没有连接。我知道我需要找到e (mod phi(n)) d def egcd(a, b): x, lastX = 0, 1 y, lastY = 1, 0 while (b != 0): q = a // b a, b = b, a % b x, lastX = lastX - q * x, x y, lastY = lastY - q * y, y return (lastX, lastY) ax + bx = gcd(a, b) = 1,我们的私人指数。

虽然维基百科告诉我们找到mmi我们需要使用modular multiplicative inverse。我在python中实现了如下算法:

e*x + phi(n)*y = gcd(e, phi(n)) = 1

这是我迷失的地方。据我所知,等式egcd(e, phi(n))[-367, 2]相同。 我们致电{{1}},现在我的x和y得到{{1}}。

从这里我老实说不知道去哪里。我已经阅读Extended Euclidian Algorithm,我发现有一些替代发生,但我不明白这些数字与我得到的答案或我开始的价值观有何关联。有人可以从我这里务实地向我解释我需要做什么吗? (当我说实用时,我的意思是没有实际的代码。伪代码很好,但如果我得到实际的代码,我将无法学习没有抄袭我的任务,这是一个很大的禁忌)。

与往常一样,任何帮助都是真正的赞赏。谢谢大家!

(是的,我见过这些:this similar questionRSA: Private key calculation with Extended Euclidean Algorithm

2 个答案:

答案 0 :(得分:6)

您拥有的扩展欧几里德算法的实现并不完整,因为它为私钥生成负数。请改用此代码:

https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm

对于您的示例,私钥d为2753。

p=61
q=53
n = 3233
phi(n)=3120
e=17
d=modinv(17,3120)=2753

尝试一下:

message m m=65


encryption: m^e mod n = c    (65**17) % 3120 =  65
decryption: c^d mod n = m      (65**2753) % 3120 =   65

这一切都在这里解释:

http://southernpacificreview.com/2014/01/06/rsa-key-generation-example/

答案 1 :(得分:1)

def egcd(a,b):   
    s1, s2 = 1, 0   
    t1, t2 = 0, 1   
    while b!=0:   
        q = a//b    
        r = a%b   
        a, b = b, r     
        s = s1-q*s2    
        s1, s2 = s2, s      
        t = t1-q*t2    
        t1, t2 = t2, t    
    return (s1, t1)
  

尝试比较以上。

     

我会告诉你你的错误在哪里:

     

a,b = b,a%b

     

a现在具有b的值   (B = A%B)==(B = B%B)
  和进行两行的类似原因