【发布时间】:2019-10-21 19:34:29
【问题描述】:
我正在编写一个程序来计算弱 RSA 公钥的私钥。我想知道如何从值n 中确定p 和q 的值。这是到目前为止的 Python 代码:
from Crypto.PublicKey import RSA #PyCryptoDome
import .math as cm # My own module
with open(public_keyfile, 'rb') as key: # Public Keyfile Is in PEM format
public_key = RSA.import_key(key)
n = public_key.n # N value of the public_key
e = public_key.e # E value of the public_key
p, q = get_factors_of(n) # This I don't know how to do, though there is a question that might help [see bottom]
t = cm.lcm(p-1, q-1) # Get the lowest common multiple of q and q
d = cm.mod_inverse(e, t) # Get d, the modular inverse of e % t
private_key = RSA.construct((n, e, d, p, q) # Construct the RSA private_key
上面引用的.math 模块:
from math import gcd
def mod_inverse(a, b):
a = a % b
for x in range(1, b):
if (a * x) % b == 1:
return x
return 1
def lcm(x, y):
return x * y // gcd(x, y)
我需要做的似乎被引用了 here 但这段代码是用 Java 编写的。
如果有人知道如何使用 python 从n 获取p 和q,将不胜感激。
非常感谢,Legorooj。
【问题讨论】:
-
SO 不是免费的代码转换服务。转换为 Python,然后发布带有问题的代码。
-
我知道。我根本不懂 Java,我希望 SO 能够帮助解决如何使用 python 从
n获取p和q。 -
你懂手动操作吗?如果不是,那么你真的有一个数学问题,而不是一个编程问题(尤其不是 Python 问题)。如果你这样做了,那么从那里开始,如果你不能让实现工作,请提出一个更具体的问题。
-
我认为您不了解分解作为两个近似相等素数乘积的大整数的难度,即 RSA 模数。如果没有大量硬件和/或时间,您将无法做到。
-
幸运的是我两者都有。 @KarlKnechtel,我了解如何手动操作,并且已经找到了实现它的方法 - 目前仅作为伪代码。如果可行,我将回答我自己的问题。如果没有,我会问一个新问题,为什么实施不起作用。
标签: python encryption rsa primes cryptanalysis