summaryrefslogtreecommitdiffstats
path: root/solve/solve.py
blob: 68b58f3f7fb62dee19b7fd9200f1e92d97441865 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python3

from Crypto.PublicKey import RSA
from Crypto.Util.number import long_to_bytes
from math import isqrt, inf
from tqdm import tqdm

pubkey = RSA.importKey(open("pubkey.pem").read())

# Small primes can be found with naive approach.

for i in tqdm(range(3, isqrt(pubkey.n), 2), total=inf):
    if pubkey.n % i == 0:
        p = i
        break
else:
    raise

q = pubkey.n // p
phi = (p-1)*(q-1)
d = pow(pubkey.e, -1, phi)

ciphertext = int(open("message.txt").read())
plaintext = pow(ciphertext, d, pubkey.n)

print(long_to_bytes(plaintext).decode())