在Python中无限循环(查找素数和GCD)

时间:2016-04-12 08:33:54

标签: python python-3.x

我目前被程序中的无限循环困住,我真的不知道它是什么错误。

这是我的代码:

from reference import brute_prime, euclid_gcd, extended_euclid

def rsa(min_p, min_q, min_e):

    p = brute_prime(min_p)
    q = brute_prime(min_q)

    n = p * q
    phi = (p - 1) * (q - 1)
    e_prime = brute_prime(min_e)

    while euclid_gcd(e_prime, phi) != 1:
        e_prime = brute_prime(min_e)
        print(e_prime)
        e_prime += 1
    return e_prime

    #d = extended_euclid(e_prime, phi)                
    #tup1 = (d, e_prime, n)    
    #return tup1

我的目标是使用brute_primeeuclid_gcd找到e,通过使用e_prime迭代地找到下一个最小素数brute_prime,并测试{{1} }}。如果成功,则终止,如果不成功,则递增gcd(e′,ϕ)=1并继续。

请注意,我调用的这些函数都已导入,函数中没有任何错误。有什么问题?

编辑:

当试图查看每次迭代的结果时,我尝试了类似这样的事情

e_prime

循环每次给我3次。

编辑: 这是print(rsa(11, 13, 3))

brute_prime

编辑: 这是def brute_prime(number): #Making sure that the input from number will be type int #setting two convenient limits for future use in the loop number = int(number) point_begin = 2 point_end = int((number**0.5)) + 1 #Two specific exceptions in the function that will execute and ignores #later commands so that it is a tiny bit more efficient... if number == 2: return 2 elif number == 1: return 1 while True: condition = True for i in range(point_begin, point_end): if number % i == 0: condition = False break if condition: return number else: number += 1

Euclid_gcd

2 个答案:

答案 0 :(得分:1)

由于min_e未被更新,因此RSA函数中的while循环看起来有问题,因此每次迭代都使用相同的值,因此循环被卡住,检查您的逻辑。
或者用这个:

    while euclid_gcd(e_prime, phi) != 1:
            e_prime = brute_prime(min_e)
            min_e = e_prime + 1 # check for next prime-number co-prime to phi

另请注意,1是不是素数。对于1作为输入,brute_prime应返回2

答案 1 :(得分:-1)

也许不相关,但为什么不做像

这样的事情
e_prime = brute_prime(min_e)
#This loop will not calculate and return me e_prime.
#Instead it just keeps running and exits later with nothing.
while euclid_gcd(e_prime, phi) != 1:
    e_prime = brute_prime(min_e)
    e_prime += 1
return e_prime