-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_keys.py
34 lines (27 loc) · 963 Bytes
/
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
#%%
import os
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
""" Gerar Par de Chaves """
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
pem_private_key = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
pem_public_key = private_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
abs_path = os.path.dirname(__file__)
private_key_file = open(f"{abs_path}\\keys\\rsa.pem", "w")
private_key_file.truncate()
private_key_file.write(pem_private_key.decode())
private_key_file.close()
private_key_file = open(f"{abs_path}\\keys\\rsa.pub", "w")
private_key_file.truncate()
private_key_file.write(pem_public_key.decode())
private_key_file.close()