Created
December 15, 2018 15:35
-
-
Save algomaster99/830edacfc6b36fcf5604332cf7b8cbc1 to your computer and use it in GitHub Desktop.
Key generation using RSA algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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