-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathgenkey.py
More file actions
32 lines (28 loc) · 1.03 KB
/
genkey.py
File metadata and controls
32 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
# Generate a private key
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
# Serialize and write the private key to a file
with open("private_key.pem", "wb") as private_key_file:
private_key_file.write(
private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
)
# Generate the corresponding public key
public_key = private_key.public_key()
# Serialize and write the public key to a file
with open("public_key.pem", "wb") as public_key_file:
public_key_file.write(
public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
)