简体   繁体   English

Python 程序似乎永远运行而不在终端中输出

[英]Python program seems to run forever without outputting in terminal

I have been working on a library to implement the RSA encryption method, and this file (along with others I have been working on) do not output, but instead after executing the script only output a blank line in terminal.我一直在研究一个库来实现 RSA 加密方法,而这个文件(以及我一直在研究的其他文件)不是 output,而是在执行脚本后仅 output 终端中的空白行。 I have run it through an autograder, and it times out.我已经通过自动评分器运行它,它超时了。 Below is the code for the library, but something tells me my issue could be an interpreter issue or something outside of the file itself.下面是库的代码,但有些东西告诉我我的问题可能是解释器问题或文件本身之外的问题。 It looks like it could be getting stuck before reaching a return or output statement.看起来它可能在到达返回或 output 语句之前就卡住了。 I've also included a screenshot of the terminal output.我还附上了终端 output 的屏幕截图。

import stdio
import stdrandom
import sys


# Generates and returns the public/private keys as a tuple (n, e, d). Prime numbers p and q
# needed to generate the keys are picked from the interval [lo, hi).
def keygen(lo, hi):
    primes = []
    for i in range(lo, hi):
        if _primes(0, i):
            primes += [i]

    ptemp = stdrandom.uniformInt(0, len(primes))
    qtemp = stdrandom.uniformInt(0, len(primes))
    p = primes[ptemp]
    q = primes[qtemp]
    n = p * q
    m = (p - 1) * (q - 1)
    while True:
        e = stdrandom.uniformInt(2, m)
        if e % m == 0 and m % e != 0:
            break
    d = 0
    for a in range(1, m):
        if (e * a) % m == 1:
            d = a
            break
    return n, e, d


# Encrypts x (int) using the public key (n, e) and returns the encrypted value.
def encrypt(x, n, e):
    return (x ** e) % n


# Decrypts y (int) using the private key (n, d) and returns the decrypted value.
def decrypt(y, n, d):
    return (y ** d) % n


# Returns the least number of bits needed to represent n.
def bitLength(n):
    return len(bin(n)) - 2


# Returns the binary representation of n expressed in decimal, having the given width, and padded
# with leading zeros.
def dec2bin(n, width):
    return format(n, '0%db' % (width))


# Returns the decimal representation of n expressed in binary.
def bin2dec(n):
    return int(n, 2)


# Returns a list of primes from the interval [lo, hi).
def _primes(lo, hi):
    primes = []
    for p in range(lo, hi + 1):
        j = 2
        f = 1
        while(j * j <= p):
            if(p % j == 0):
                f = 0
                break
            j = j + 1
        if(f == 1):
            primes += [p]
    return primes


# Returns a list containing a random sample (without replacement) of k items from the list a.
def _sample(a, k):
    b = a.copy()
    c = b[0:k]
    stdrandom.shuffle(c)
    return c


# Returns a random item from the list a.
def _choice(a):
    random = stdrandom.uniformInt(0, len(a))
    return random



# Unit tests the library [DO NOT EDIT].
def _main():
    x = ord(sys.argv[1])
    n, e, d = keygen(25, 100)
    encrypted = encrypt(x, n, e)
    stdio.writef('encrypt(%c) = %d\n', x, encrypted)
    decrypted = decrypt(encrypted, n, d)
    stdio.writef('decrypt(%d) = %c\n', encrypted, decrypted)
    width = bitLength(x)
    stdio.writef('bitLength(%d) = %d\n', x, width)
    xBinary = dec2bin(x, width)
    stdio.writef('dec2bin(%d) = %s\n', x, xBinary)
    stdio.writef('bin2dec(%s) = %d\n', xBinary, bin2dec(xBinary))


    if __name__ == '__main__':
        _main()

As @iz_ suggests, you have an infinite loop in your code.正如@iz_ 建议的那样,您的代码中有一个无限循环。 This code:这段代码:

while True:
    e = stdrandom.uniformInt(2, m)
    if e % m == 0 and m % e != 0:
        break

will never exit because e will always be less than m , and therefore e % m == 0 will always be False .永远不会退出,因为e将始终小于m ,因此e % m == 0将始终为False The loop won't exit until e % m == 0 is True, which will never happen.直到e % m == 0为真,循环才会退出,这永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM