我正在编写一个程序来计算弱 RSA 公钥的私钥。我想知道我将如何确定 value 的值p
和q
来自 value 的值n
。到目前为止,这是 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)
我需要做的似乎在 这里被引用,但这段代码是用 Java 编写的。
如果有人知道如何使用 python 获取p
和q
获取n
,将不胜感激。
非常感谢,Legorooj。