[英]RSA Python & Extended Euclidean algorithm to generate the private key. Variable is None
Problem with simple RSA encryption algorithm. 简单的RSA加密算法存在问题。 Extended Euclidean algorithm is used to generate the private key .
扩展欧几里得算法用于生成私钥 。 The problem with
multiplicative_inverse(e, phi)
method. 与
multiplicative_inverse(e, phi)
方法有关的问题。 It is used for finding the multiplicative inverse of two numbers. 它用于查找两个数字的乘法逆。 The function does not return private key correctly.
该函数不能正确返回私钥。 It returns
None
value. 它返回
None
值。
I have the following code: 我有以下代码:
import random
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
#Euclidean extended algorithm for finding the multiplicative inverse of two numbers
def multiplicative_inverse(e, phi):
d = 0
x1 = 0
x2 = 1
y1 = 1
temp_phi = phi
while e > 0:
temp1 = temp_phi/e
temp2 = temp_phi - temp1 * e
temp_phi = e
e = temp2
x = x2- temp1* x1
y = d - temp1 * y1
x2 = x1
x1 = x
d = y1
y1 = y
if temp_phi == 1:
return d + phi
def generate_keypair(p, q):
n = p * q
#Phi is the totient of n
phi = (p-1) * (q-1)
#An integer e such that e and phi(n) are coprime
e = random.randrange(1, phi)
#Euclid's Algorithm to verify that e and phi(n) are comprime
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)
#Extended Euclid's Algorithm to generate the private key
d = multiplicative_inverse(e, phi)
#Public key is (e, n) and private key is (d, n)
return ((e, n), (d, n))
if __name__ == '__main__':
p = 17
q = 23
public, private = generate_keypair(p, q)
print("Public key is:", public ," and private key is:", private)
Since the variable d
in the following line d = multiplicative_inverse(e, phi)
contains None
value, then during encryption I receive the following error: 由于以下行
d = multiplicative_inverse(e, phi)
中的变量d
包含None
值,因此在加密期间我收到以下错误:
TypeError: unsupported operand type(s) for pow(): 'int', 'NoneType', 'int'
TypeError: pow()不支持的操作数类型:'int','NoneType','int'
Output for the code that I provided in the question: 我在问题中提供的代码的输出:
Public key is: (269, 391) and private key is: (None, 391)
公钥为:(269,391),私钥为:(无,391)
Question: Why the variable contains None
value. 问题:为什么变量包含
None
值。 How to fix that? 如何解决?
Well I'm not sure about the algorithm itself, and can't tell if you if it's wrong or right, but you only return a value from multiplicative_inverse
function when if temp_phi == 1
. 好吧,我不确定算法本身,也无法确定算法是对还是错,但是当
if temp_phi == 1
时,您只能从multiplicative_inverse
函数返回一个值。 Otherwise, the result is None. 否则,结果为无。 So I bet your
temp_phi != 1
when you run the function. 所以我打赌您在运行该函数时
temp_phi != 1
。 There's probably some mistake in the function's logic. 函数的逻辑中可能存在一些错误。
I think that this is a problem: 我认为这是一个问题:
if temp_phi == 1:
return d + phi
This function return some value only under condition that temp_phi is equal to 1, otherwise it will not return any value. 该函数仅在temp_phi等于1的条件下才返回某个值,否则它将不返回任何值。
This looks like you are converting from python 2 to 3. In 2 temp_phi/e will be an integer, but in 3 it's a float. 看起来您正在从python 2转换为3。在2中,temp_phi / e是整数,但在3中,它是浮点数。 As the multiplative inverses method of division uses integers you need to change the line to int(temp_phi / e) or temp_phi // e
由于多级除法使用整数,因此您需要将行更改为int(temp_phi / e)或temp_phi // e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.