-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRSAKeyGenerator.py
More file actions
67 lines (49 loc) · 1.91 KB
/
Copy pathRSAKeyGenerator.py
File metadata and controls
67 lines (49 loc) · 1.91 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from sys import argv
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from base64_encode_decode import base64_encode_decode
class RSAKeyGenerator:
def write(priv_keypath, pub_keypath):
private_key = rsa.generate_private_key(public_exponent=65537, key_size=1024,)
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
private_key_encoded = base64_encode_decode.encode(private_pem)
with open(priv_keypath, 'wb') as f:
f.write(private_key_encoded)
public_key = private_key.public_key()
public_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open(pub_keypath, 'wb') as f:
f.write(public_pem)
def read(priv_keyPath, pub_keyPath):
f = open(priv_keyPath, "r")
key_encoded = f.read()
private_key_decoded = base64_encode_decode.decode(key_encoded)
print("Private RSA Key:")
print(private_key_decoded)
print("\n")
print("Public RSA Key:")
f = open(pub_keyPath, "r")
pub_key = f.read()
print(pub_key)
def main(argv):
if (len(argv) != 4):
print("Usage: RSAKeyGenerator.py [r|w] <priv-key-file>.pem <pub-key-file>.pem")
print("Do not ever share your private keys.")
return
mode = argv[1]
priv_keyPath = argv[2]
pub_keyPath = argv[3]
if (mode.lower() == "w"):
print("Generating and saving keys")
RSAKeyGenerator.write(priv_keyPath, pub_keyPath)
else:
RSAKeyGenerator.read(priv_keyPath, pub_keyPath)
print("Done.")
if __name__ == '__main__':
main(argv)