-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_keys.py
More file actions
31 lines (26 loc) · 1.03 KB
/
generate_keys.py
File metadata and controls
31 lines (26 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
# generate_keys.py
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
# --- Generate the private key ---
# The private key is the secret part, keep it safe!
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
# --- Derive the public key from the private key ---
# The public key can be shared with anyone.
public_key = private_key.public_key()
# --- Save the private key to a file ---
with open("private_key.pem", "wb") as f:
f.write(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption() # No password protection
))
# --- Save the public key to a file ---
with open("public_key.pem", "wb") as f:
f.write(public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
))
print(" Public and private keys have been generated and saved.")