0

我正在编写 RSA 算法的代码作为一个项目。对于熟悉这个加密系统的人来说,下面的函数计算 phi(n) 的值。但是,当我运行它时,会出现此错误:

    Traceback (most recent call last):
  File "C:\Python27\RSA.py", line 127, in <module>
    phi_n()
  File "C:\Python27\RSA.py", line 30, in phi_n
    prime_f = prime_list[random.randint(0,length)]
  File "C:\Python27\lib\random.py", line 241, in randint
    return self.randrange(a, b+1)
  File "C:\Python27\lib\random.py", line 217, in randrange
    raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
ValueError: empty range for randrange() (0,0, 0)

我不完全理解为什么会出现这个错误。这是我的 phi_n 函数代码:

def phi_n():  
    global prime_list
    length = len(prime_list) - 1
    prime_f = prime_list[random.randint(0,length)]
    prime_s = prime_list[random.randint(0,length)]
    global pq
    n = prime_f * prime_s
    global phi_n
    phi_n = (prime_f -1) * (prime_s -1)
    return phi_n

评论将不胜感激。

谢谢

4

1 回答 1

5

问题是prime_list空的,因此length将等于-1导致random.randint(0, -1)由于明显原因而无效的调用。

于 2012-10-07T17:07:06.693 回答