Search code examples
pythonrsapycrypto

Exporting public key into binary in pycrypto


I'm using python 2 with pycrypto and generating my keys like that.

random_generator = Random.new().read
self.parent.private_key = RSA.generate(1024, random_generator)
self.parent.public_key = self.parent.private_key.publickey()

And I've got a problem with getting a public key which I need to send as binary/base64 I have searched whole RSA pycrypto lib and there are no methods that can convert my public into binary.

Can someone take a look and try to help me?


Solution

  • You can use the _RSAobj.exportKey(self, format='PEM', passphrase=None, pkcs=1) function to serialize the the key (_RSAobj is the implementation of the key objects). If you use DER format it will use a binary encoding instead of the textual encoding of PEM. When using DER you might need to encode it using Base64 too.

    You can then deserialize it using RSA.importKey(externKey, passphrase=None).