title: Saving RSA keys to a file, using pycrypto
body: <p>I’m using PyCrypto 2.3 and I would like to save the keys I have generated into a file, so as to distribute them to the client and server. I can’t seem to find a way to print the keys correctly, neither can I find examples on the internet.</p>
  
  <pre><code>    def resetKeys(self):
          keys = RSA.generate(1024)
  
          privHandle = open(self.privateKeyFile, 'wb')
          privHandle.write(keys.privatekey())
          privHandle.close()
  
          pubHandle = open(self.publicKeyFile, 'wb')
          pubHandle.write(keys.publickey())
          pubHandle.close()
  </code></pre>
  
  <p>This codes does not work for many reasons: first, <code>keys.publickey()</code> doesn’t seem to be printable, it returns: </p>
  
  <pre><code>    &lt;_RSAobj @0x10f810d0 n(1024),e&gt;
  </code></pre>
  
  <p>and second, <code>keys</code> has no function named <code>privatekey</code>. </p>
  
  <p>Has anyone done that before?</p>