title: how to decrypt a message using exported RSA public key with PyCrypto?
body: <p>I faced this challenge where I asked to decrypt an encrypted message using the exported public key. I am given with 3 files: </p>
  
  <ol>
  <li>The encryption python script</li>
  <li>The encrypted message </li>
  <li>The exported public key</li>
  </ol>
  
  <p>I tried to import the public key and then decrypt but I think I have to figure out the private key in order to decrypt the message.</p>
  
  <p>The encryption code is:</p>
  
  <pre><code>import gmpy
  from Crypto.Util.number import *
  from Crypto.PublicKey import RSA
  from Crypto.Cipher import PKCS1_v1_5
  
  message = open('message', 'r').read() * 30
  
  def ext_rsa_encrypt(p, q, e, msg):
      m = bytes_to_long(msg)
      while True:
          n = p * q
          try:
              phi = (p - 1)*(q - 1)
              d = gmpy.invert(e, phi)
              pubkey = RSA.construct((long(n), long(e)))
              key = PKCS1_v1_5.new(pubkey)
              enc = key.encrypt(msg).encode('base64')
              return enc
          except:
              p = gmpy.next_prime(p**2 + q**2)
              q = gmpy.next_prime(2*p*q)
              e = gmpy.next_prime(e**2)
  
  p = getPrime(128)
  q = getPrime(128)
  n = p*q
  e = getPrime(64)
  pubkey = RSA.construct((long(n), long(e)))
  f = open('pubkey.pem', 'w')
  f.write(pubkey.exportKey())
  g = open('msg.enc', 'w')
  g.write(ext_rsa_encrypt(p, q, e, message))
  </code></pre>