def create_septuple_user_input(self): if len(self.prime_list) == 0: print("No primes list loaded") p_choice = input("Enter p: ") try: p_choice = int(p_choice) except ValueError: print("'", str(p_choice), "' is not a valid integer!") return None if not self.is_prime(p_choice): print(str(p_choice), " is not a prime number!") return None q_choice = input("Enter q: ") try: q_choice = int(q_choice) except ValueError: print("'", str(q_choice), "' is not a valid integer!") return None if not self.is_prime(q_choice): print(str(q_choice), " is not a prime number!") return None choice = input("Specify e? Press enter to use default (65537) [Y/N]: ") if ((choice == "y") or (choice == "Y")): e_choice = input("Select e: ") print("The choice is: ", str(e_choice)) try: val = int(e_choice) except ValueError: print("Integer input required!") return None if not check_coprimality((int(p_choice) - 1)*(int(q_choice) - 1), int(e_choice)): print("Co primality conditions not met!") return None else: septuple_object = encryption_set(p=p_choice, q=q_choice, custom_e = int(e_choice)) else: septuple_object = encryption_set(p =p_choice, q=q_choice, custom_e = 65537) print("\nEncryption object created!") septuple_object.to_String() self.add_key_to_septuple(septuple_object, septuple_object.get_e()) if self.active_encryption_object is None: self.active_encryption_object = septuple_object print() return septuple_object
def load_encryption_objects(folder_name): septuple_list = [] try: with open("./Profiles/" + str(folder_name) + "/septuples.csv") as f: reader = csv.reader(f) for line in reader: data = ast.literal_eval(line[0]) septuple = encrypt.encryption_set(p=int(data[0]), q=int(data[1]), custom_e=int(data[4]), custom_k=int(data[5]), custom_d=int(data[6])) septuple_list.append(septuple) except Exception as e: print("Error loading septuple data: ", str(e)) return septuple_list
def load_key_data(folder_name): encryption_keys = {} try: with open("./Profiles/" + str(folder_name) + "/keys.csv", 'r') as f: reader = csv.reader(f) for line in reader: sept_list = ast.literal_eval(line[0]) keys = ast.literal_eval(line[1]) septuple = encrypt.encryption_set(p=sept_list[0], q=sept_list[1], custom_e=sept_list[4], custom_k=sept_list[5], custom_d=sept_list[6]) encryption_keys[septuple] = keys except Exception as e: print("Error loading key data: ", str(e)) return encryption_keys
def load_active_object_data(folder_name): active_encryption_object = None try: with open("./Profiles/" + str(folder_name) + "/active_septuple.csv", 'r') as f: reader = csv.reader(f) for line in reader: if len(line) > 0: data = ast.literal_eval(line[0]) active_encryption_object = encrypt.encryption_set( p=int(data[0]), q=int(data[1]), custom_e=int(data[4]), custom_k=int(data[5]), custom_d=int(data[6])) except Exception as e: print("Error loading active object: ", str(e)) return active_encryption_object
def main(): #Sample plain text to encrypt plain_text = "But soft, what light from yonder window breaks? It is the East...and Juliet is the Sun" #Variables to store ascii-lists, plain-text lists, etc plain_text_ascii = [] cipher_ascii = [] cipher_text = "" decrypted_cipher_ascii = [] decrypted_cipher_text = "" encrytped_test_list = [] decrypted_test_list = [] #Create an RSA encryption set object and print out the values it created for itself RSA_object = encrypt.encryption_set(47, 59) RSA_object.to_String() #Obtain a list of ascii values for the plain text plain_text_ascii = get_ascii_list(plain_text) #Encrypt each number in the plain-text ascii list to obtain the cipher-ascii list for i in plain_text_ascii: cipher_ascii.append(int(RSA_object.encrypt_int(i))) #Generate the cipher text from the cipher-ascii list cipher_text = get_string_from_ascii(cipher_ascii) #Decrypt the cipher-ascii. This should result back to the original plain-ascii list for i in cipher_ascii: decrypted_cipher_ascii.append(RSA_object.decrypt_int(i)) #Generate the plain-text from the plain-ascii for i in decrypted_cipher_ascii: decrypted_cipher_text += chr(int(i)) #Display the original plain text, cipher text, and the decrypted_cipher_text (which should equal the plain text) print("\n\nPlain text: ", plain_text) print("\nCipher text: ", cipher_text) print("\nDecrypted cipher text: ", decrypted_cipher_text)
def run(): #Get handle to prime list file prime_list = load_primes("primes1.txt") #Get the start and end choice start_choice = get_start_prime() end_choice = get_end_prime() print("Analyzing prime ", start_choice, " to prime ", end_choice, " in first million primes") time.sleep(3) #Create file name from the start/end choices file_name = "./Excel_Data/prime_" + str(start_choice) + "_to_" + str( end_choice) + "_holes.csv" header = [ "p, q, n, Phi, e, k, d", "n", "totient", "e", "# holes", "Transparency Percentage" ] with open(file_name, "w") as csv_file: writer = csv.writer(csv_file, dialect='excel') writer.writerow(header) #Create our prime lists p_list = prime_list[start_choice - 1:end_choice - 1] q_list = prime_list[start_choice - 1:end_choice - 1] print("Start val: ", prime_list[start_choice - 1], " End val: ", prime_list[end_choice - 1]) pub_keys = [3, 5, 17, 257, 65537] #Loop through all p/q combinations trying each e-value for all combinations start_time = time.time() for p_val in p_list: for q_val in q_list: for e_val in pub_keys: #Ignore repeat cases and cases where p==q if q_val > p_val: try: #Make a temp object for the current septuple temp_object = encrypt.encryption_set(p=p_val, q=q_val, custom_e=e_val) #temp_object.enable_debug_mode() sept = temp_object.get_septuple() #Analyze the holes in the septuple holes_num = search_septuple(temp_object) #Round transparency to nearest hundreth transparency = round( (float(holes_num) / (temp_object.n - 1)) * 100, 2) print("Analyzing sept: ", sept, " Holes - ", holes_num) #Append to the output file each iteration with open(file_name, "a", newline='') as csv_file: writer = csv.writer(csv_file, dialect='excel') csv_entry = [ sept, temp_object.get_n(), temp_object.totient, e_val, holes_num, transparency ] writer.writerow(csv_entry) except Exception as e: print("Exception occured: ", str(e)) traceback.print_exc() end_time = time.time() elapsed_time = end_time - start_time start_time_readable = time.ctime(int(start_time)) end_time_readable = time.ctime(int(end_time)) elapsed_time_minutes = round(float(elapsed_time) / 60.0, 2) mins_string = str(elapsed_time_minutes) + " minutes" print("Start time: ", start_time_readable) print("End time: ", end_time_readable) print("Program duration: ", mins_string) time_header = ["Start Time", "End Time", "Elapsed time"] with open(file_name, "a", newline='') as csv_file: writer = csv.writer(csv_file, dialect='excel') csv_entry = [start_time_readable, end_time_readable, mins_string] writer.writerow("") writer.writerow(time_header) writer.writerow(csv_entry)
def run(): #Load first million primes prime_list = load_primes("primes1.txt") #Get user choice for primes to analyze start_choice = get_first_prime() end_choice = get_second_prime() print("Analyzing prime ", start_choice, " with prime ", end_choice) time.sleep(3) #Create an output file file_name = "./Excel_Data/prime_" + str(start_choice) + "_and_" + str(end_choice) + "_specific_analysis.csv" header = ["p, q, n, Phi, e, k, d", "N", "Totient", "E", "Transparency Percentage"] with open(file_name, "w") as csv_file: writer = csv.writer(csv_file, dialect='excel') writer.writerow(header) #Load our chosen primes p_list = [start_choice] q_list = [end_choice] #Test all valid e from 0 to 65537. e_list = prime_list[0:6541] #Loop through all p/q combinations trying each e-value for all combinations start_time = time.time() counter = 0 for p_val in p_list: for q_val in q_list: for e_val in e_list: counter +=1 if (counter % 1000 == 0): print("Evaluating E: ", e_val) try: #Make a temp object for the current septuple temp_object = encrypt.encryption_set(p=p_val, q=q_val, custom_e=e_val) sept = temp_object.get_septuple(); #Analyze the holes in the septuple holes_num = search_septuple(temp_object) #Round transparency to nearest hundreth transparency = round((float(holes_num)/(temp_object.n -1))*100, 2) #Append to the output file with open(file_name, "a", newline='') as csv_file: writer = csv.writer(csv_file, dialect='excel') csv_entry = [sept, temp_object.n, temp_object.totient, e_val, transparency] writer.writerow(csv_entry) except Exception as e: print("Exception occured: ", str(e)) traceback.print_exc() end_time = time.time() elapsed_time = end_time - start_time start_time_readable = time.ctime(int(start_time)) end_time_readable = time.ctime(int(end_time)) elapsed_time_minutes = round(float(elapsed_time)/60.0, 2) mins_string = str(elapsed_time_minutes) + " minutes" print("Start time: ", start_time_readable) print("End time: ", end_time_readable) print("Program duration: ", mins_string) time_header = ["Start Time", "End Time", "Elapsed time"] with open(file_name, "a", newline='') as csv_file: writer = csv.writer(csv_file, dialect='excel') csv_entry = [start_time_readable, end_time_readable, mins_string] writer.writerow("") writer.writerow(time_header) writer.writerow(csv_entry)