picoctf/crypto/mind_your_ps_and_qs/solve.py

31 lines
844 B
Python
Raw Normal View History

2024-09-06 18:59:44 +02:00
#!/usr/bin/env nix-shell
#!nix-shell -i sage -p sage
from sage.all import *
import time
import sys
c = 8533139361076999596208540806559574687666062896040360148742851107661304651861689
n = 769457290801263793712740792519696786147248001937382943813345728685422050738403253
e = 65537
if len(sys.argv) >= 2 and sys.argv[1] == 'CACHE':
print('Using precalculated p & q')
p = 1617549722683965197900599011412144490161
q = 475693130177488446807040098678772442581573
else:
print('Factorizing N')
# Takes about 7-8 minutes on my own pc
start = time.time()
factorization = ZZ(n).factor(algorithm='qsieve')
stop = time.time()
print(f"Took: {stop - start}")
p, q = [x[0] for x in list(factorization)]
phi = (p - 1) * (q - 1)
d = inverse_mod(e, phi)
m = power_mod(c, d, n)
print(m.to_bytes((m.bit_length() + 7) // 8, 'big').decode())