-
Notifications
You must be signed in to change notification settings - Fork 30
/
generate_keys.py
45 lines (35 loc) · 1.45 KB
/
generate_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
35
36
37
38
39
40
41
42
43
44
45
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
from getpass import getpass
# Generate the private key with a key size of 4096
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=4096,
backend=default_backend()
)
# Prompt the user for a passphrase to encrypt the private key
password = getpass("RSA Private Key Passphrase: ").encode('utf-8')
# Define the encryption algorithm with the desired characteristics
encryption_algorithm = serialization.BestAvailableEncryption(password)
# Serialize the private key with the specified encryption algorithm
pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=encryption_algorithm
)
# Write the encrypted private key to a file
with open("myprivatekey.pem", "wb") as f:
f.write(pem_private)
# Generate the public key
public_key = private_key.public_key()
# Serialize the public key
pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
# Write the public key to a file
with open("mypublickey.pem", "wb") as f:
f.write(pem_public)
print("myprivatekey.pem has been created successfully.")
print("mypublickey.pem has been created successfully.")