-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives.asymmetric import rsa | ||
from cryptography.hazmat.primitives import serialization | ||
|
||
class KeyManagement: | ||
def __init__(self): | ||
self.private_key = rsa.generate_private_key( | ||
public_exponent=65537, | ||
key_size=2048, | ||
backend=default_backend() | ||
) | ||
self.public_key = self.private_key.public_key() | ||
|
||
def save_keys(self, private_key_file, public_key_file): | ||
with open(private_key_file, 'wb') as f: | ||
f.write(self.private_key.private_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PrivateFormat.TraditionalOpenSSL | ||
)) | ||
with open(public_key_file, 'wb') as f: | ||
f.write(self.public_key.public_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PublicFormat.SubjectPublicKeyInfo | ||
)) | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
km = KeyManagement() | ||
km.save_keys('private_key.pem', 'public_key.pem') | ||
print("Keys saved successfully.") |