
from Crypto.PublicKey import RSA

## To run type python rsa.py from the commandline (assuming you've pythonh installed
import binascii				

def string2int(my_str):
    return int(binascii.hexlify(my_str), 16) 

def int2string(my_int):
    return binascii.unhexlify(format(my_int, "x").encode("utf-8")).decode("utf-8")

###Same as exercise 3, open the file, read the private key, generate a rsa object and read the values
f = open("mykey3", "r")
privateKey = f.read()
key = RSA.importKey(privateKey)
ciphertext = 474862643754336865489984490773307542016161159436213530034995807183836312346778617047666360854948178434525541089212091928949344492697684657497682106740050084305554758259427768463395264318566101255923490595579348647860471822284428834756812967844672795316325109976652375135659724572710513755433401072885408968307124213606768098411795080747616961236626790699862671834311406129266854138764009952421206625693567227556664511584573464971029270576495696636132292906861410359486612705079004947333371264698887189359265840678094723729950785568382017843975809503403984016678927664449791524785943376314787680072596720311587221852
n = key.n
e = key.e
d = key.d
p = key.p
q = key.q

## ----- decrypt ciphertext then convert number back to a string
decrypted = pow(ciphertext, d, n)   ## decrypt
plaintext = int2string(decrypted)
print (plaintext)

