[pycrypto] Exporting/importing RSA publickeys

Lorenz Quack don at amberfisharts.com
Sun Apr 18 11:10:22 CST 2010

Hello death!

The easiest way is to use the pickle module from the python standard library.
untested pseudo code to show the general idea:
 >>> key = Crypto.publicKey.RSA.generate(1024)
 >>> pubkey = key.pubkey()
 >>> pubkey_string = pickle.dumps(pubkey)
 >>> socket.send(pubkey_string)
and on the other side
 >>> pubkey_string = socket.recv()
 >>> pubkey = pickle.loads(pubkey_string)

an alternative is to access the relevant attributes of the pubkey and then use the construct method.
again untested code:
 >>> key = Crypto.publicKey.RSA.generate(1024)
 >>> pubkey = key.pubkey()
 >>> n, e = key.n, key.e
 >>> # send n and e over socket
and on the other side
 >>> # get n and e from the socket
 >>> pubkey = Crypto.publicKey.RSA.construct((n,e))

I hope this points you in the right direction.

sincerely yours

On 04/18/2010 01:18 AM, death wrote:
> i'm just wondering is there a way to import AND export the public keys
> from RSA so i can, send the public key over a socket, import it on the
> otherside so i can create a "secure" socket ( i know authentication is
> not done in this process ).
> the socket is still subject to a MIM attack, but this is not the point
> at the moment
> it could be my inability to read the documentation but i found no way to
> dump the key, and import it again.

More information about the pycrypto mailing list