11from Modules import (
2- primality_tester_modular ,
2+ primality_tester_impl ,
33) # Function: primality_tester, arguments: detail, prime_number_to_check, coprimes_to_check_against
44from Modules import (
5- euclidean_algorithm_for_two_or_more_numbers_modular ,
5+ euclidean_algorithm_for_two_or_more_numbers_impl ,
66) # Function: modular_euclidean_algorithm, arguments: discrete number of integers to check the hcf of, 1, first_factor_value, second_factor_value
77import random
88
@@ -30,7 +30,7 @@ def generate_odd_positive_integers(primes_upper_bound, primes_lower_bound):
3030
3131 def check_if_prime (candidate ):
3232 if (
33- primality_tester_modular .primality_tester (
33+ primality_tester_impl .primality_tester (
3434 "-1" , candidate , coprime_integers_to_check_against
3535 )
3636 == "is prime"
@@ -63,7 +63,7 @@ def find_coprime(prime_1, prime_2, slicer):
6363 for coprime_search in range (((prime_1 - 1 ) * (prime_2 - 1 )) // slicer , 2 , - 1 ):
6464 (
6565 hcf_and_lcm_list
66- ) = euclidean_algorithm_for_two_or_more_numbers_modular .modular_euclidean_algorithm (
66+ ) = euclidean_algorithm_for_two_or_more_numbers_impl .modular_euclidean_algorithm (
6767 2 , 1 , (prime_1 - 1 ) * (prime_2 - 1 ), coprime_search
6868 )
6969 if hcf_and_lcm_list [0 ] == 1 :
@@ -88,25 +88,126 @@ def unicode_numeral(string, debug):
8888 "DEBUG: Unicode numeral representation of all the string characters is: %s"
8989 % unicode_of_string
9090 )
91+ unicode_of_string_length = []
92+ __count_length = 0
93+ for element in unicode_of_string :
94+ unicode_of_string_length .append (len (str (unicode_of_string [__count_length ])))
95+ __count_length += 1
96+ if debug == 1 :
97+ print (
98+ "DEBUG: Length of each character's unicode numeral representation: %s"
99+ % unicode_of_string_length
100+ )
91101 joining_unicode_of_string = [str (int ) for int in unicode_of_string ]
92- return "" .join (joining_unicode_of_string ), unicode_of_string
102+ return (
103+ "" .join (joining_unicode_of_string ),
104+ unicode_of_string ,
105+ unicode_of_string_length ,
106+ )
93107
94108
95- def inverse_unicode_numeral (string , debug ):
109+ def inverse_unicode_numeral (string , debug , length_of_unicode_characters ):
96110 # This function deconverts from Unicode Numerals
111+ unicode_of_string_inverse_organized = []
97112 if debug == 1 :
98113 print ("String passed to inverse unicode numeral representation %s:" % string )
99114 unicode_of_string_inverse = []
100- for character in string :
101- number_of_char = chr (int (character ))
102- unicode_of_string_inverse .append (number_of_char )
103- if debug == 1 :
104- print (
105- "DEBUG: Inverse unicode numeral representation of all the string characters is: %s"
106- % unicode_of_string_inverse
107- )
108- joining_unicode_of_string_inverse = [str (int ) for int in unicode_of_string_inverse ]
109- return "" .join (joining_unicode_of_string_inverse )
115+ if length_of_unicode_characters == 0 :
116+ for character in string :
117+ number_of_char = chr (int (character ))
118+ unicode_of_string_inverse .append (number_of_char )
119+ if debug == 1 :
120+ print (
121+ "DEBUG: Inverse unicode numeral representation of all the string characters is: %s"
122+ % unicode_of_string_inverse
123+ )
124+ else :
125+ string = str (string )[1 :- 1 ] # Remove the []
126+ string = string .split ("," ) # Split at each comma
127+ string = list (map (int , string )) # Map the string to integer
128+ string_c = string # To append later
129+ string = [
130+ "{:06d}" .format (item ) for item in string [:- 1 ]
131+ ] # Not including last element since we don't wanna append 0's to that one
132+ string .append (str (string_c [- 1 ])) # Re adding last element
133+ if debug == 1 :
134+ print ("New string with padded 0's %s" % string )
135+ print ("Debug 1: %s" % string )
136+ string = str (string )[1 :- 1 ]
137+ string = string .replace ("'" , "" )
138+ string = string .replace ("," , "" )
139+ string = string .replace (" " , "" )
140+ if debug == 1 :
141+ print ("Debug 2: %s" % string )
142+
143+ # length_of_unicode_characters = str(length_of_unicode_characters)[1:-1]
144+ length_of_unicode_characters = [
145+ int (s ) for s in length_of_unicode_characters .split ("," )
146+ ]
147+ if debug == 1 :
148+ print ("Length of unicode characters: %s" % length_of_unicode_characters )
149+ for i in range (len (length_of_unicode_characters )):
150+ a , string = (
151+ string [: length_of_unicode_characters [0 ]],
152+ string [length_of_unicode_characters [0 ] :],
153+ )
154+
155+ unicode_of_string_inverse_organized .append (a )
156+ del length_of_unicode_characters [0 ]
157+ print (unicode_of_string_inverse_organized )
158+ for character in unicode_of_string_inverse_organized :
159+ number_of_char = chr (int (character ))
160+ unicode_of_string_inverse .append (number_of_char )
161+ if debug == 1 :
162+ print (
163+ "DEBUG: Inverse unicode numeral representation of all the string characters is: %s"
164+ % unicode_of_string_inverse
165+ )
166+ joining_unicode_of_string_inverse = [
167+ str (int ) for int in unicode_of_string_inverse
168+ ]
169+ return "" .join (joining_unicode_of_string_inverse )
170+
171+ # string = str(string)[1:-1] # Remove the []
172+ # string = string.split(",") # Split at each comma
173+ # string = list(map(int, string)) # Map the string to integers
174+
175+
176+ #
177+ # string_c = string # To append later
178+ # string = [
179+ # "{:06d}".format(item) for item in string[:-1]
180+ # ] # Not including last element since we don't wanna append 0's to that one
181+ # string.append(string_c[-1]) # Re adding last element
182+ # print("New string with padded 0's %s" % string)
183+ #
184+ # # string = "".join(str(string))
185+ # # string = string.replace(",", "")
186+ # # string = string.replace(" ", "")
187+ # # string = str(string)
188+ # length_of_unicode_characters = [
189+ # int(s) for s in length_of_unicode_characters.split(",")
190+ # ]
191+ # print(string)
192+ # print("Length of unicode characters: %s" % length_of_unicode_characters)
193+ # for i in range(len(length_of_unicode_characters)):
194+ # a, string = (
195+ # string[: length_of_unicode_characters[0]],
196+ # string[length_of_unicode_characters[0] :],
197+ # )
198+ # unicode_of_string_inverse_organized.append(a)
199+ # del length_of_unicode_characters[0]
200+ # print(unicode_of_string_inverse_organized)
201+ # for character in unicode_of_string_inverse_organized:
202+ # number_of_char = chr(int(character))
203+ # unicode_of_string_inverse.append(number_of_char)
204+ # if debug == 1:
205+ # print(
206+ # "DEBUG: Inverse unicode numeral representation of all the string characters is: %s"
207+ # % unicode_of_string_inverse
208+ # )
209+ # joining_unicode_of_string_inverse = [str(int) for int in unicode_of_string_inverse]
210+ # return "".join(joining_unicode_of_string_inverse)
110211
111212
112213def encoder (message , N , e , debug ):
@@ -136,8 +237,8 @@ def encoder(message, N, e, debug):
136237 for element in unicode_of_string_split :
137238 element = int (element )
138239 encoded_element = (element ** e ) % N
139- if debug == 1 :
140- print ("%d encoded as %d" % (element , encoded_element ))
240+ # if debug == 1:
241+ print ("%d encoded as %d" % (element , encoded_element ))
141242 unicode_of_encoded_string .append (encoded_element )
142243
143244 return unicode_of_encoded_string
@@ -152,11 +253,11 @@ def decoder(N, e, decoder_key, encoded_message, list_of_primes, debug):
152253 s ,
153254 decoder_key ,
154255 hcf ,
155- ) = euclidean_algorithm_for_two_or_more_numbers_modular .bezouts_identity (
256+ ) = euclidean_algorithm_for_two_or_more_numbers_impl .bezouts_identity (
156257 (list_of_primes [0 ] - 1 ) * (list_of_primes [1 ] - 1 ), e
157258 )
158259 if decoder_key < 0 :
159- decoder_key = euclidean_algorithm_for_two_or_more_numbers_modular .bezouts_identity_positive (
260+ decoder_key = euclidean_algorithm_for_two_or_more_numbers_impl .bezouts_identity_positive (
160261 decoder_key , (list_of_primes [0 ] - 1 ) * (list_of_primes [1 ] - 1 ), e
161262 )
162263 if debug == 1 :
@@ -173,12 +274,11 @@ def decoder(N, e, decoder_key, encoded_message, list_of_primes, debug):
173274 encoded_message = encoded_message .split ("," ) # Necessary to remove the commas
174275 while naive_count < len (encoded_message ):
175276 decoded_piece = (int (encoded_message [naive_count ]) ** decoder_key ) % N
176- if debug == 1 :
177- # print("Decoder key: %d\nN value: %d" % (decoder_key, N))
178- print (
179- "%d decoded as: %d"
180- % (int (encoded_message [naive_count ]), decoded_piece )
181- )
277+ # if debug == 1:
278+ # print("Decoder key: %d\nN value: %d" % (decoder_key, N))
279+ print (
280+ "%d decoded as %d" % (int (encoded_message [naive_count ]), decoded_piece )
281+ )
182282 decoded_list .append (decoded_piece )
183283 naive_count += 1
184284 return decoded_list
@@ -212,6 +312,7 @@ def write_to_file(
212312 file_name ,
213313 encode_or_decode ,
214314 decoder_key ,
315+ unicode_of_string_length ,
215316):
216317
217318 f = open ("keys.txt" , "w" )
@@ -230,32 +331,50 @@ def write_to_file(
230331 # Keys and message
231332 if encode_or_decode == "e" :
232333 f .write (
233- "Public keys: %s\n Private keys: %s\n Decryption key: %s\n "
334+ "Public keys: %s\n Private keys: %s\n Decryption key: %s\n Unicode decryption list: %s \ n "
234335 % (
235336 pubkey ,
236337 privkey ,
237338 decoder_key ,
339+ str (unicode_of_string_length )[1 :- 1 ],
238340 )
239341 )
342+
240343 elif encode_or_decode == "d" :
241344 f .write (
242- "Public keys: %s\n Decryption key: %s\n "
345+ "Public keys: %s\n Decryption key: %s\n Unicode decryption list: %s \ n "
243346 % (
244347 pubkey ,
245348 decoder_key ,
349+ unicode_of_string_length ,
246350 )
247351 )
248352
249353 if is_file != "1" :
250- f .write (
251- "Encoded message associated with the above keys: %s\n Uncrypted message associated with the above keys: %s \n "
252- % (str (encoded_message )[1 :- 1 ], unencrypted_message )
253- )
254- # To do: figure out how to find the proper unicode numeral representation for the decrypted data set when decrypting (not encrypting), might not be possible though, the main issue is that the decrypted files are hard to guess what their unicode representation would be for each letter.
354+ if encode_or_decode == "e" :
355+ f .write (
356+ "Encoded message associated with the above keys: %s\n Uncrypted message associated with the above keys: %s \n "
357+ % (str (encoded_message )[1 :- 1 ], unencrypted_message )
358+ )
359+ elif encode_or_decode == "d" :
360+ f .write (
361+ "Encoded message associated with the above keys: %s\n Decrypted message associated with the above keys: %s \n "
362+ % (encoded_message , unicode_of_string )
363+ )
364+
255365 if encode_or_decode == "e" :
256366 if unicode_numeral_message == 1 :
257367 inverse_unicode_string = inverse_unicode_numeral (
258- unicode_of_string , debug
368+ unicode_of_string , debug , 0
369+ )
370+ f .write (
371+ "Decrypted message in character unicode form: %s"
372+ % inverse_unicode_string
373+ )
374+ elif encode_or_decode == "d" :
375+ if unicode_numeral_message == 1 :
376+ inverse_unicode_string = inverse_unicode_numeral (
377+ unicode_of_string , debug , unicode_of_string_length
259378 )
260379 f .write (
261380 "Decrypted message in character unicode form: %s"
@@ -264,10 +383,20 @@ def write_to_file(
264383 else :
265384 if encode_or_decode == "e" :
266385 file_name_appended = "" .join (("encrypted_" , file_name ))
386+ file_acquired = open (file_name_appended , "w" )
387+ file_acquired .write (str (encoded_message )[1 :- 1 ])
267388 elif encode_or_decode == "d" :
268- file_name_appended = "" .join (("decrypted_" , file_name ))
269- file_acquired = open (file_name_appended , "w" )
270- file_acquired .write (str (encoded_message )[1 :- 1 ])
389+ if unicode_numeral_message == 1 :
390+ inverse_unicode_string = inverse_unicode_numeral (
391+ unicode_of_string , debug , unicode_of_string_length
392+ )
393+ file_name_appended = "" .join (("decrypted_" , file_name ))
394+ file_acquired = open (file_name_appended , "w" )
395+ file_acquired .write (str (inverse_unicode_string ))
396+ else :
397+ file_name_appended = "" .join (("decrypted_" , file_name ))
398+ file_acquired = open (file_name_appended , "w" )
399+ file_acquired .write (str (unicode_of_string ))
271400 file_acquired .close ()
272401
273402 # Close files
0 commit comments