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

Python program seems to run forever without outputting in terminal

我一直在开发一个库来实现 RSA 加密方法,这个文件(以及我一直在处理的其他文件)不输出,而是在执行脚本后只在终端中输出一个空行。我通过自动分级机 运行 它,它超时了。下面是库的代码,但有些东西告诉我我的问题可能是解释器问题或文件本身之外的问题。看起来它可能在到达 return 或输出语句之前卡住了。我还提供了 terminal 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()

正如@iz_ 所暗示的,您的代码中有一个无限循环。此代码:

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

永远不会退出,因为 e 将始终小于 m,因此 e % m == 0 将始终是 False。直到 e % m == 0 为真,循环才会退出,这永远不会发生。