Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@galgalesh

This comment has been minimized.

Copy link

@galgalesh galgalesh commented Aug 5, 2016

Pycrypto is unmaintained and has known vulnerabilities. Use pycryptodome, it is a drop-in replacement.

@mohit1337

This comment has been minimized.

Copy link

@mohit1337 mohit1337 commented Aug 16, 2016

Gives me error:

    new_key = RSA.generate(bits, e=65537)
TypeError: generate_c() got an unexpected keyword argument 'e'
@miigotu

This comment has been minimized.

Copy link

@miigotu miigotu commented Jan 17, 2017

e should be random methinks =P

@warsm

This comment has been minimized.

Copy link

@warsm warsm commented May 17, 2017

@miigotu "youthinks" wrong. e should be chosen so that e and λ(n) are coprime. It is not chosen at random, and since it is usually small for computation reasons, and included in the public key, it can always be known by an attacker anyway.

@aravindaran

This comment has been minimized.

Copy link

@aravindaran aravindaran commented Aug 17, 2017

from Crypto.PublicKey import RSA
code = 'nooneknows'

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

@WarAtLord

This comment has been minimized.

Copy link

@WarAtLord WarAtLord commented Jan 15, 2018

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

This comment has been minimized.

Copy link

@mynameisvinn mynameisvinn commented Jan 30, 2018

@WarAtLord try publick_key.exportKey("PEM")

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