Skip to content

Instantly share code, notes, and snippets.

@RDxR10
Last active May 12, 2021 17:49
Show Gist options
  • Save RDxR10/9988c808a0865ab288dd1f42c94ad537 to your computer and use it in GitHub Desktop.
Save RDxR10/9988c808a0865ab288dd1f42c94ad537 to your computer and use it in GitHub Desktop.
Factorization of N given e and d based on trial and error. [Divide k by powers of 2 satisfying x to be greater than 1]. Full explanation here: https://crypto.stackexchange.com/questions/62482/algorithm-to-factorize-n-given-n-e-d/62487#62487
import random
from math import gcd
n = 113138904645172037883970365829067951997230612719077573521906183509830180342554841790268134999423971247602095979484887092205889453631416247856139838680189062511282674134361726455828113825651055263796576482555849771303361415911103661873954509376979834006775895197929252775133737380642752081153063469135950168223
e = 65537
d = 87345713405055532428664184040885638635456003191089749453199952101307167014234779974982171268609415280584641472420424299514002514548043646741981648196634644960356958819956637431278502574332925957523028825580469419959164626563649612912919564472132340496010962167627957743115660323378023656051813802028938198977
k = e*d - 1
g = random.randint(2, n - 1)
t = k
while(t % 2 == 0):
t = t // 2
x = pow(g, t, n)
y = gcd(x - 1, n)
while(x > 1 and y > 1):
p = y
q = n // y
print(p, q)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment