Skip to content

Instantly share code, notes, and snippets.

@algomaster99
Created December 15, 2018 15:35
Show Gist options
  • Save algomaster99/830edacfc6b36fcf5604332cf7b8cbc1 to your computer and use it in GitHub Desktop.
Save algomaster99/830edacfc6b36fcf5604332cf7b8cbc1 to your computer and use it in GitHub Desktop.
Key generation using RSA algorithm
def gen_key():
private_key = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend()
)
return private_key
def encode_private_key(pk):
pem = pk.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
with open('private_key.pem', 'wb+') as f:
f.write(pem)
def encode_public_key(pk):
public_key = pk.public_key()
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open('public_key.pem', 'wb+') as f:
f.write(pem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment