Path: blob/master/RSA-encryption/Intro-Challenges/Dp&Dq/encrypt.py
1402 views
from Crypto.Util.number import *1import gmpy22from Crypto.PublicKey import RSA3from secret import flag, factor_p, factor_q45n = 789470487490789215535333971682233063270774145861839446098185535846636657476777571640910697317078881978181232658916903483817362312295617303956573332629056139583777398281188755860000688158673546642969569311063423669722099873120068822394204355320198235048512461622281123282680313057828698245495892680290302349416e = 6553778p = factor_p9q = factor_q10assert p*q == n11phin = (p - 1)*(q - 1)1213d = inverse(e, phin)1415# d congruent to dp (mod p)16# d congruent to dq (mod q)17# Apply Chinese Remainder Theorem to solve the equations and get d18# Ref. to: https://crypto.stanford.edu/pbc/notes/numbertheory/crt.html19# Ref. to: https://en.wikipedia.org/wiki/Chinese_remainder_theorem20dp = d % (p-1)21dq = d % (q-1)2223key = RSA.construct((n, long(e)))24obj1 = open("publickey.pem",'w')25obj1.write(key.exportKey('PEM'))26obj1.close()2728message = bytes_to_long(flag)29ciphertext = pow(message, e, n)30ciphertext = long_to_bytes(ciphertext)31ciphertext = ciphertext.encode("hex")3233f = open("data.txt",'w')34f.write("[*] ciphertext: " + ciphertext + "\n" + "[*] dp: " + str(dp) + "\n" + "[*] dq: " + str(dq))35f.close()3637