Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/rsa/known_d.py
2589 views
1
from math import gcd
2
from random import randrange
3
4
5
def attack(N, e, d):
6
"""
7
Recovers the prime factors from a modulus if the public exponent and private exponent are known.
8
:param N: the modulus
9
:param e: the public exponent
10
:param d: the private exponent
11
:return: a tuple containing the prime factors
12
"""
13
k = e * d - 1
14
t = 0
15
while k % (2 ** t) == 0:
16
t += 1
17
18
while True:
19
g = randrange(1, N)
20
for s in range(1, t + 1):
21
x = pow(g, k // (2 ** s), N)
22
p = gcd(x - 1, N)
23
if 1 < p < N and N % p == 0:
24
return p, N // p
25
26