class TriDES: def __init__(self, key_list, input_str): self.key_list = key_list self.des1 = DES(self.key_list[0], input_str) def encryption(self): out1 = self.des1.ecb_encryption() des2 = DES(self.key_list[1], out1) out2 = des2.ecb_encryption() des3 = DES(self.key_list[2], out2) result = des3.ecb_encryption() return result
def encryption(self): out1 = self.des1.ecb_encryption() des2 = DES(self.key_list[1], out1) out2 = des2.ecb_encryption() des3 = DES(self.key_list[2], out2) result = des3.ecb_encryption() return result
# Text is from Wikipedia init_key_bin = hex_2_bin('133457799BBCDFF1') key_list = [hex_2_bin('133457799BBCDFF1'), hex_2_bin('1222222FFEB13ABB'), hex_2_bin('342257799BBCDFF1')] in_str = "Courbet was the lead ship of her class of four dreadnought battleships, the first ones built for the " \ "French Navy. In World War I, after helping to sink the Austro-Hungarian protected cruiser SMS Zenta in " \ "August 1914, she provided cover for the Otranto Barrage that blockaded the Austro-Hungarian Navy in the " \ "Adriatic Sea, and often served as a flagship. Although upgraded several times before World War II, " \ "by the 1930s she was no longer considered to be a first-line battleship and spent much of that decade " \ "as a gunnery training ship. A few weeks after the German invasion of France on 10 May 1940, Courbet was " \ "hastily reactivated. She supported Allied troops in the defence of Cherbourg during mid-June. As part " \ "of Operation Catapult, she was seized in Portsmouth by British forces on 3 July and was turned over to " \ "the Free French a week later. She was used as a stationary anti-aircraft battery and as an " \ "accommodation ship there. " init_str = hex_2_bin(str_2_hex(in_str)) des = DES(init_key_bin, init_str) total_run = 200 # print("Original ECB mode encryption runtime test start") # start_time = datetime.now() # for i in range(total_run): # text = des.ecb_encryption() # # if i % 5 == 0 and i != 0: # # print('Finished', i, 'runs') # finish_time = datetime.now() - start_time # # print(bin_2_hex(text)) # print(total_run, 'times of original ECB mode encryption finished with', finish_time, '\n') # # print("Original CBC mode encryption runtime test start") # iv = hex_2_bin('AB125AFC396214F3')
from Convert import bin_2_hex, hex_2_str, hex_2_bin, print_bin, str_2_hex from Des import DES ################################### # Test 1 # Key and string are taken from HW4 init_key_bin_1 = bin(int('0123456789abcdef', 16))[2:].zfill(64) init_str_1 = bin(int('0123456789abcdef', 16))[2:].zfill(64) des_1 = DES(init_key_bin_1, init_str_1) text_1 = bin_2_hex(des_1.encryption(des_1.input_list[0])) print_bin(text_1) print() ################################### # Test 2 # Key and string are taken from http://page.math.tu-berlin.de/~kant/teaching/hess/krypto-ws2006/des.htm init_key_bin_2 = hex_2_bin('133457799BBCDFF1') init_str_2 = hex_2_bin('0123456789ABCDEF') des_2 = DES(init_key_bin_2, init_str_2) text_2 = des_2.encryption(des_2.input_list[0]) print_bin(bin_2_hex(text_2)) print() ################################### # Test 3 # Decryption of Test 2 init_key_bin_3 = hex_2_bin('133457799BBCDFF1') init_str_3 = hex_2_bin('85e813540f0ab405') des_3 = DES(init_key_bin_3, init_str_3) text_3 = des_3.decryption(des_3.input_list[0]) print_bin(bin_2_hex(text_3))
zeros_number = 64 - len(keyVal) for i in range(zeros_number): updatedKeyVal += '0' updatedKeyVal += keyVal else: updatedKeyVal = keyVal if len(messageVal) != 64: zeros_number = 64 - len(messageVal) for i in range(zeros_number): updatedMessageVal += '0' updatedMessageVal += messageVal else: updatedMessageVal = messageVal return [updatedMessageVal, updatedKeyVal, iterNumber] [updatedMessageVal, updatedKeyVal, iterNumber] = prepareInput() des = DES() des.encrypt(updatedMessageVal, updatedKeyVal, iterNumber) closeProgram = int(input("1: Exit\n" + "2: Try another Message\n")) while closeProgram != 1: if closeProgram == 1: break elif closeProgram == 2: [updatedMessageVal, updatedKeyVal, iterNumber] = prepareInput() des = DES() des.encrypt(updatedMessageVal, updatedKeyVal, iterNumber) closeProgram = int(input("1: Exit\n" + "2: Try another Message\n")) else: closeProgram = int(input("1: Exit\n" + "2: Try another Message\n"))
def __init__(self, key_list, input_str): self.key_list = key_list self.des1 = DES(self.key_list[0], input_str)