solve.py (573B)
1#!/usr/bin/env python3 2 3from Crypto.PublicKey import RSA 4from Crypto.Util.number import long_to_bytes 5from math import isqrt, inf 6from tqdm import tqdm 7 8pubkey = RSA.importKey(open("pubkey.pem").read()) 9 10# Small primes can be found with naive approach. 11 12for i in tqdm(range(3, isqrt(pubkey.n), 2), total=inf): 13 if pubkey.n % i == 0: 14 p = i 15 break 16else: 17 raise 18 19q = pubkey.n // p 20phi = (p-1)*(q-1) 21d = pow(pubkey.e, -1, phi) 22 23ciphertext = int(open("message.txt").read()) 24plaintext = pow(ciphertext, d, pubkey.n) 25 26print(long_to_bytes(plaintext).decode())