Skip to content

Instantly share code, notes, and snippets.

@lkdocs
Last active December 22, 2015 19:28
Show Gist options
  • Save lkdocs/6519387 to your computer and use it in GitHub Desktop.
Save lkdocs/6519387 to your computer and use it in GitHub Desktop.
def generate_RSA(bits=2048):
'''
Generate an RSA keypair with an exponent of 65537 in PEM format
param: bits The key length in bits
Return private key and public key
'''
from M2Crypto import RSA, BIO
new_key = RSA.gen_key(bits, 65537)
memory = BIO.MemoryBuffer()
new_key.save_key_bio(memory, cipher=None)
private_key = memory.getvalue()
new_key.save_pub_key_bio(memory)
return private_key, memory.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment