-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA Key Pair Generation.py
305 lines (207 loc) · 9.46 KB
/
RSA Key Pair Generation.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# By: Tim Tarver also known as CryptoKeyPlayer
# This script was designed to generate an RSA public and private
# key pair for encrypting any web application.
# import cryptosyspki as pki
# import Gen
import os
import sys
import pytest
import shutil
from glob import iglob
import tempfile
import requests
import crypto
# We now begin the function to generate our RSA Key Pairs
# for securing the web application (or website) using the
# fastest algorithm possible (until updates say otherwise).
# 512-bit encryption for speed is not secure so we will use
# 1024-bit instead.
def rsa_keypair_generator():
print("\n TEST RSA KEY FUNCTIONS....")
print(" MAking a new 512-bit RSA Key Pair...")
rsa_private_key_file = "myrsaprivate.p8"
rsa_public_key_file = "myrsapublic.p1"
rsa_key_generator = pki.RSA.make_keys(rsa_public_key_file, rsa_private_key_file,
1024, pki.RSA.PublicExponent.RSAEXP_EQ_65537,
'password')
assert (rsa_key_generator == 0)
# The lines below reads from the key pair file into and puts it into an
# internal private key string. Now print out the private and public key pair
private_key_string = pki.RSA.read_private_key(rsa_private_key_file, 'password')
print("Private Key String = ", private_key_string)
assert (len(private_key_string) > 0)
number_of_bits = pki.RSA.key_bits(private_key_string)
print("Number of Private Bits = ", number_of_bits)
assert (number_of_bits > 0)
print("HashCode = ", pki.RSA.key_hashcode(private_key_string))
public_key_string = pki.RSA.read_public_key(rsa_public_key_file)
print("Public Key String = ", public_key_string)
assert (len(public_key_string) > 0)
number_of_bits2 = pki.RSA.key_bits(public_key_string)
print("Number of Public Bits = ", number_of_bits2)
assert (number_of_bits2 > 0)
print("HashCode = ", pki.RSA.key_hashcode(public_key_string))
# Prepare the Exponent and Modulus values to be used in Encryption.
exponent = pki.RSA.key_value(public_key_string, "Exponent")
print("Exponent in Base64: ", exponent)
modulus = pki.RSA.key_value(public_key_string, "Modulus")
print("Modulus in Base64: ", modulus)
# The lines below create an XML file version of the Public Key
# String and converts them into non-standard Base 64
# hexadecimal values and standard Base64 values.
non_standard_hex = pki.RSA.from_xmlstring(public_key_string,
pki.RSA.XmlOptions.HEXBINARY)
print("Hexadecimal Version of XML: ", non_standard_hex)
standard_hex = pki.RSA.to_xmlstring(public_key_string)
print("Standard XML Value: ", standard_hex)
# The lines below goes back to our XML String to a new
# internal string different from the first one with same hash code.
new_internal_string = pki.RSA.from_xmlstring(standard_hex)
print("New Key String: ", new_internal_string)
print("HashCode = ", pki.RSA.keu_hashcode(new_internal_string))
# The docstring below consists of print statements to be ran
# by the test functions itself separate from the RSA Key Pair
# Generator function.
"""
# This line is the minimum PKI version constant
min_pki_version_constant = 200300
# First, we want to display information about the main PKI Crypto system
print("PKI Version = ", pki.Gen.version())
print("Module Name = ", pki.Gen.module_name())
print("Compile Time = ", pki.gen.compile_time())
print("Platform = ", pki.Gen.core_platform())
print("License Type = ", pki.Gen.license_type())
print("Module Info = ", pki.Gen.module_info())
# Secondly, we must display some values of the system we might need.
print("sys.getdefaultencoding() = ", sys.getdefaultcoding())
print("sys.getfilesystemencoding() = ", sys.getfilesystemencoding())
print("sys.platform() = ", sys.platform)
print("cwd = ", os.getcwd())
# Make the system require a current version or higher (if not previously installed)
if pki.Gen.version() < min_pki_version_constant:
raise Exception('Require PKI Version' + str(min_pki_version_constant) + 'or greater')
# We define our global variables to create and initialize a temporary directory
# used for security tests.
initial_directory = os.getcwd()
current_temporary_directory = ""
# This line will be used to depete our directory when we do not need it
# anymore.
delete_temporary_directory = True
# We begin defining our function that sets up our temporary directory
# and use it to put our test files in it.
"""
# The methods below is the process to create a temporary directory
# to transfer files you want to store and encrypt.
"""
def setup_temporary_directory():
global current_temporary_directory
# This is the sub-directory to the current temporary directory
# and we change from initial temporary directory to working directory.
working_directory = os.path.join(initial_directory, "work")
print("\n Expecting to find work directory: ", woring_directory)
assert os.path.isdir(working_directory)
# Insert all test files needed to create a temporary sub-directory
# in the work directory.
current_temporary_directory = os.path.join(working_directory, "pki.tmp." +
pki.Cnv.tohex(pki.Rng.bytestring(4)))
os.mkdir(current_temporary_directory)
assert os.path.isdir(current_temporary_directory)
# Now, copy all temporary files
for files in iglob(os.path.join(working_directory, "*.*")):
if os.path.isfile(files) and not files.endswith('.zip'):
shutil.copy(files, current_temporary_directory)
# Change the currenct working directory to be inside temporary directory.
os.chdir(current_temporary_directory)
print("Working in new temporary directory: ", os.getcwd())
"""
# The next method is designed to reset the starting directory and
# removes the temporary directory.
"""
def reset_initial_directory():
if not os.path.isdir(initial_directory):
return
if (current_temporary_directory == initial_directory):
return
os.chdir(initial_directory)
print("")
# Then print the current working directory (cwd) and remove the temporary
# directory.
if (delete_temporary_directory and 'pki.tmp' in current_temporary_directory):
print("Removing Temporary Directory: ", current_temporary_directory)
shutil.rmtree(current_temporary_directory, ignore_errors = True)
"""
# The methods below utilize the Pytest module to begin setting up our security tests
# with decorators that call fixtures and receive data we want to test.
"""
@pytest.fixture(scope = "module", autouse = True)
def divider_module(request):
print("\n --- module %s() start ---" % request.module.__name__)
setup_temp_dir()
def finalize_module():
print("\n --- module %s() done ---" % request.function.__name__)
reset_initial_directory()
request.addfinalizer(finalize_module)
@pytest.fixture(scope = "function", autouse = True)
def divider_function(request):
print("\n --- function %s() start ---" % request.function.__name__)
os.chdir(current_temporary_directory)
def finalize_function():
print("\n --- function %s() done ---" % request.function.__name__)
os.chdir(initial_directory)
request.addfinalizer(finalize_function)
"""
# The methods below begin implementing CRUD Operations via functions to print, read,
# write, update and delete which ever binary or text files necessary.
"""
def read_binary_file(filename):
with open(filename, "rb") as file1:
return bytearray(file1.read())
def write_binary_file(filename, data):
with open(filename, "wb") as file1:
file1.write(data)
def read_text_file(filename, enc = 'utf8'):
with open(filename, encoding = enc) as file1:
return file1.read()
def write_text_file(filename, save, enc = 'utf8'):
with open(filename, "w", encoding = enc) as file1:
file1.write(save)
def print_file(filename):
text_file = read_text_file(filename)
print(text_file)
def print_hexdecimal_file(filename):
binary_file = read_binary_file(filename)
print(pki.Cnv.tohex(binary_file)
def file_dumper(filename):
string_1 = read_text_file(filename)
n_dash = (24 if len(string_1) > 24 else len(string_1))
print("FILE: ", filename)
print("-" * n_dash)
print(string_1)
print("-" * n_dash)
def x509_printer_and_dumper(filename):
dumpfile = 'temporarydump.txt'
if os.path.isfile(filename):
print("FILE: ", filename)
else:
print("STRING: ", filename)
try:
pki.X509.text_dump(dumpfile, filename)
print_file(dumpfile)
except pki.PKIError as e:
print("Whoops! PKI Error: ", e)
def asn1_printer_and_dumper(filename, opts = 0):
if os.path.isfile(filename):
print("FILE: ", filename)
else:
print("STRING: ", filename)
(filedump, dumpfile) = tempfile.mkstemp()
try:
pki.Asn1.text_dump(dumpfile, filename, opts)
string = read_text_file(dumpfile)
print(string)
os.close(filedump)
except pki.PKIError as e:
print("Whoops! PKIError: ", e)
finally:
os.remove(dumpfile)
"""