RSA编码和解码[e,d]。在python中查找e和d ..更新

时间:2016-04-23 18:38:10

标签: python performance cryptography rsa infinite-loop

我一直试图找到编码号码和解码号码。我知道你需要找到这些的基本规则,并且已经开始尝试创建一个python程序,它从随机选择的前150个素数中找到e变量和d变量。我这样做的方式是:

import random
from primesieve import *
from sympy.solvers import *
from sympy import *
f = 0
choiceOfPrimes = generate_primes(10)

p = random.choice(choiceOfPrimes)
q = random.choice(choiceOfPrimes)

while p == q:
    p = random.choice(choiceOfPrimes)

n = p * q
phiN = (p-1) * (q-1)
lista = []
listb = []
for naa in range(1, phiN):
    lista.append(naa)
for naaa in range(1, 35):
    listb.append(naaa)

e = random.choice(lista)


def finding_d():
    global phiN
    global e
    global f
    f = e
    abc = []
    abc = divmod(phiN, f)
    abc1 = f * abc[0]
    abc2 = abc[1]
    phiN = f * abc1 + abc2  # phiN = f * {how many it goes in} + {remainder}
    abc3 = divmod(f, abc2)
    f = abc2 * abc3[0] + abc3[1]  # (moved f to phiN) and (remainder to f)
    abc4 = divmod(abc2, abc3[1])
    e1 = abc3[1] * abc4[0]
    e2 = abc4[1]
    abc2 = e1 + e2
    e3 = abc2 + (e1*-1) # This bit I am struggling





d = f


while (e % n) == 0 and (e % phiN) == 0:
    for r in range(phiN, 0, -1):
        e = r

while ((d * e) % phiN) != 1:
    for r in range(1, 35):
        e = r

print(p, q, n, phiN, e, d)

永远运行并且从未完成运行。我甚至尝试将generate_primes(150)更改为generate_primes(10)但发生了同样的问题。

有没有人有解决方案,因为我很乐意听到它(因为primesieve库不是自动在python库中,你必须自己下载)。感谢。

编辑:

我做了:     而p == q:         p = random.choice(choiceOfPrimes) 但是我很难做出欧几里得垫

1 个答案:

答案 0 :(得分:1)

您的代码存在很多问题:

首先,

while p == q:
   p = random.choice(choiceOfPrimes)
  

在计算phiN的值之前,您应该执行此步骤,因为如果您更改p的值,则phiN的值会发生变化。

其次,

d = random.choice(lista)
  

要计算d,您应使用e找到与phiN相关的Extended Euclidean algorithm e,选择$currentTime = \Carbon\Carbon::now()->subMinutes(15); 的值,直至$albums = Album::with(array('pictures' => function($q){ $currentTime = \Carbon\Carbon::now()->subMinutes(15); $q->whereDate('updated_at','<=', $currentTime)); }))->get(); 他们的工作效率非常低。