-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_keys.py
34 lines (26 loc) · 996 Bytes
/
gen_keys.py
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
33
34
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
# Generate private key
private_key = Ed25519PrivateKey.generate()
# Obtain the public key
public_key = private_key.public_key()
# Serialize the private key to PEM format
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
# Serialize the public key to PEM format
public_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# Print the keys
print("Private Key:\n", private_pem.decode('utf-8'))
print("Public Key:\n", public_pem.decode('utf-8'))
# Save the private key to a file
with open("private_key.pem", "wb") as f:
f.write(private_pem)
# Save the public key to a file
with open("public_key.pem", "wb") as f:
f.write(public_pem)