Skip to content

Instantly share code, notes, and snippets.

@lkdocs
Last active April 22, 2022 01:29
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save lkdocs/6519378 to your computer and use it in GitHub Desktop.
Save lkdocs/6519378 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 Crypto.PublicKey import RSA
new_key = RSA.generate(bits, e=65537)
public_key = new_key.publickey().exportKey("PEM")
private_key = new_key.exportKey("PEM")
return private_key, public_key
@aravindaran
Copy link

from Crypto.PublicKey import RSA
code = 'nooneknows'

key = RSA.generate(2048)
privatekey = key.exportKey(passphrase=code, pkcs=8)
publickey = key.publickey().exportKey()

@WarAtLord
Copy link

Nice But How Can I Write The Private Key I Tried This:
f = open('PublicKey.pem','w')
f.write(publick_key)
f.close()

BUT IT DOESN'T WORK WITH THE PRIVATE KEY, JUST RETURNS 0B

@mynameisvinn
Copy link

@WarAtLord try publick_key.exportKey("PEM")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment