-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeygen.py
More file actions
108 lines (88 loc) · 3.1 KB
/
keygen.py
File metadata and controls
108 lines (88 loc) · 3.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from Crypto.PublicKey import RSA
from Crypto.PublicKey import ECC
import sys
def get_ec_key(subject, pub_path, priv_path):
complete_key = ECC.generate(curve='P-256')
pub_key = complete_key.public_key().export_key(format='PEM')
priv_key = complete_key.export_key(format='PEM')
pub_file = open(pub_path, "w")
pub_file.write(subject + '\n')
pub_file.write('ECC\n')
pub_file.write(pub_key)
pub_file.close()
priv_file = open(priv_path, "w")
priv_file.write(subject + '\n')
priv_file.write('ECC\n')
priv_file.write(priv_key)
priv_file.close()
def get_rsa_key(subject, pub_path, priv_path):
complete_key = RSA.generate(2048)
pub_key = complete_key.publickey().exportKey("PEM")
priv_key = complete_key.exportKey("PEM")
# print(pub_key)
pub_file = open(pub_path, "w")
pub_file.write(str(subject) + '\n')
pub_file.write('RSA' + '\n')
pub_file.write(str(pub_key, 'utf-8') + '\n')
pub_file.close()
priv_file = open(priv_path, "w")
priv_file.write(str(subject) + '\n')
priv_file.write('RSA' + '\n')
priv_file.write(str(priv_key, 'utf-8') + '\n')
priv_file.close()
# need to wrap it in command line stuff, and give rsa vs ec option
sub = "Michaela"
pub_f_name = "pubkey_ec.txt"
priv_f_name = "privkey_ec.txt"
if len(sys.argv) != 9:
print('usage: keygen -t <type of key pair> -s <subject> -pub <path to public key file> -priv <paht to private key file>')
exit()
if sys.argv[1] == '-s':
sub = sys.argv[2]
elif sys.argv[3] == '-s':
sub = sys.argv[4]
elif sys.argv[5] == '-s':
sub = sys.argv[6]
elif sys.argv[7] == '-s':
sub = sys.argv[8]
else:
print('usage: keygen -t <type of key pair> -s <subject> -pub <path to public key file> -priv <paht to private key file>')
exit()
if sys.argv[1] == '-pub':
pub_f_name = sys.argv[2]
elif sys.argv[3] == '-pub':
pub_f_name = sys.argv[4]
elif sys.argv[5] == '-pub':
pub_f_name = sys.argv[6]
elif sys.argv[7] == '-pub':
pub_f_name = sys.argv[8]
else:
print('usage: keygen -t <type of key pair> -s <subject> -pub <path to public key file> -priv <paht to private key file>')
exit()
if sys.argv[1] == '-priv':
priv_f_name = sys.argv[2]
elif sys.argv[3] == '-priv':
priv_f_name = sys.argv[4]
elif sys.argv[5] == '-priv':
priv_f_name = sys.argv[6]
elif sys.argv[7] == '-priv':
priv_f_name = sys.argv[8]
else:
print('usage: keygen -t <type of key pair> -s <subject> -pub <path to public key file> -priv <paht to private key file>')
exit()
key_type = "ec"
if sys.argv[1] == '-t':
key_type = sys.argv[2]
elif sys.argv[3] == '-t':
key_type = sys.argv[4]
elif sys.argv[5] == '-t':
key_type = sys.argv[6]
elif sys.argv[7] == '-t':
key_type = sys.argv[8]
else:
print('usage: keygen -t <type of key pair> -s <subject> -pub <path to public key file> -priv <paht to private key file>')
exit()
if key_type == "rsa":
get_rsa_key(sub, pub_f_name, priv_f_name)
if key_type == "ec":
get_ec_key(sub, pub_f_name, priv_f_name)