我正在将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
评论将不胜感激。
谢谢
答案 0 :(得分:4)
问题是prime_list
为空,因此length
将等于-1
,导致调用random.randint(0, -1)
由于显而易见的原因而无效。