From 6fed5d4ecda30a1131575a9e75beffa37843bec8 Mon Sep 17 00:00:00 2001 From: Miguel Muniz Date: Mon, 7 Oct 2024 23:40:39 -0700 Subject: [PATCH] good point --- DES.py | 149 ++++++++++++++++++++++++++++++++++++++------- desample_input.txt | 3 + output.txt | 56 +++++++++++------ 3 files changed, 168 insertions(+), 40 deletions(-) create mode 100644 desample_input.txt diff --git a/DES.py b/DES.py index 4cb5297..2d07046 100644 --- a/DES.py +++ b/DES.py @@ -2,6 +2,7 @@ # CS454 import argparse +import time #Constants/permutations PC1 = [ @@ -120,7 +121,8 @@ subkeys = [] left_half_f = [] right_half_f = [] -data_block = "" +encrypted_data = [] +decrypted_data = [] def hex_to_binary(hex): return bin(int(hex, 16))[2:].zfill(64) @@ -162,8 +164,9 @@ def DES_Encrypt(data_block, key): # Split data_block into left and right halves left_half = data_block[:32] right_half = data_block[32:] - # left_half_f.append(left_half) - # right_half_f.append(right_half) + + left_half_f.append(str(left_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + right_half_f.append(str(right_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) # print ("left half:", left_half) # print ("right half:", right_half) @@ -196,27 +199,120 @@ def DES_Encrypt(data_block, key): #print ("XOR result:", xor_result) # Swap left and right halves + left_half = right_half right_half = xor_result - + + # print ("left half:", left_half) + # print ("right half:", right_half) + + # left_half_f.append(str(left_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + # right_half_f.append(str(right_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) left_half_f.append(left_half) right_half_f.append(right_half) - - print ("left half:", left_half) - print ("right half:", right_half) - + # print ("Left half:", left_half_f) + # print ("Right half:", right_half_f) # Final permutation data_block = permute(right_half + left_half, FP) data_block = str(data_block).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","") #print ("Final Permutation:", data_block) data_block = binary_to_hex(data_block) - print ("Encrypted:", data_block) + encrypted_data.append(data_block) + print ("Encrypted:", encrypted_data) + def permute(data_block, permutation): return [data_block[i - 1] for i in permutation] + def DES_Decrypt(data_block, key): - pass + + data_block = hex_to_binary(data_block) + key = hex_to_binary(key) + + #print ("binary key:", key) + + key = permute(key, PC1) + #print ("permutated key:", key) + + # generate 16 subkeys + left_key = key[:28] + right_key = key[28:] + subkeys_left.append(left_key) + subkeys_right.append(right_key) + + # left shift + for i in range(16): + left_key = left_key[LEFT_SHIFT[i]:] + left_key[:LEFT_SHIFT[i]] + right_key = right_key[LEFT_SHIFT[i]:] + right_key[:LEFT_SHIFT[i]] + subkeys_left.append(left_key) + subkeys_right.append(right_key) + # print ("left key:",left_key) + # print ("right key:",right_key) + subkeys.append(permute(left_key + right_key, PC2)) + + + # Initial Permutation + data_block = permute(data_block, IP) + #print ("Initial Permutation:", data_block) + + # Split data_block into left and right halves + left_half = data_block[:32] + right_half = data_block[32:] + + left_half_f.append(str(left_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + right_half_f.append(str(right_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + # print ("left half:", left_half) + # print ("right half:", right_half) + + # 16 rounds of DES + for i in range(15, -1, -1): + # Expansion + expanded_right = permute(right_half, E) + #print ("expanded right half:", expanded_right) + + # XOR with subkey + xor_result = int(''.join(expanded_right), 2) ^ int(''.join(subkeys[i]), 2) + xor_result = bin(xor_result)[2:].zfill(48) + #print ("XOR result:", xor_result) + + # S-Box substitution + sbox_result = "" + for j in range(8): + row = int(xor_result[j*6] + xor_result[j*6 + 5], 2) + col = int(xor_result[j*6 + 1:j*6 + 5], 2) + sbox_result += bin(sbox[j][row][col])[2:].zfill(4) + #print ("S-Box result:", sbox_result) + + # Permutation + permuted_result = permute(sbox_result, P) + #print ("Permutated result:", permuted_result) + + # XOR with left half + xor_result = int(''.join(permuted_result), 2) ^ int(''.join(left_half), 2) + xor_result = bin(xor_result)[2:].zfill(32) + #print ("XOR result:", xor_result) + + # Swap left and right halves + + left_half = right_half + right_half = xor_result + + # print ("left half:", left_half) + # print ("right half:", right_half) + + left_half_f.append(str(left_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + right_half_f.append(str(right_half).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + # print ("Left half:", left_half_f) + # print ("Right half:", right_half_f) + # Final permutation + data_block = permute(right_half + left_half, FP) + data_block = str(data_block).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","") + #print ("Final Permutation:", data_block) + data_block = binary_to_hex(data_block) + decrypted_data.append(data_block) + print ("Decrypted:", decrypted_data) + def parse_variables(file_path): @@ -233,7 +329,7 @@ def parse_variables(file_path): print(f"Error: The file {file_path} was not found.") return variables -def output_file(output_file, data_block): +def output_file(output_file, operation): with open(output_file, 'w') as file: # write all the sub keys to the file remove brackets quotations and commas for i in range(17): @@ -248,20 +344,27 @@ def output_file(output_file, data_block): for i in range(16): file.write(f"K{i+1}=") file.write(str(subkeys[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) - file.write("\n") - + file.write("\n") + for i in range(17): + file.write("\n") file.write(f"L{i}=") - file.write(str(left_half_f).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + file.write(str(left_half_f[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) file.write("\n") file.write(f"R{i}=") - file.write(str(right_half_f).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) - - file.write("\n") - file.write(data_block) - - + file.write(str(right_half_f[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + if operation == 'encryption' : + file.write("\n") + file.write("\n") + file.write("Result=") + file.write(str(encrypted_data).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) + else : + file.write("\n") + file.write("\n") + file.write("Result=") + file.write(str(decrypted_data).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'","")) def main(): + start_time = time.time() # Set up command-line argument parsing parser = argparse.ArgumentParser(description='Parse variables for encryption or decryption.') parser.add_argument('input_file', type=str, help='Path to the text file containing the variables.') @@ -290,7 +393,11 @@ def main(): DES_Decrypt(data_block, key) # path to output file - output_file(args.output_file, data_block) + output_file(args.output_file, operation) + + end_time = time.time() + elapsed_time = end_time - start_time + print(f"Runtime: {elapsed_time:.4f} seconds") if __name__ == "__main__": main() \ No newline at end of file diff --git a/desample_input.txt b/desample_input.txt new file mode 100644 index 0000000..88f55f8 --- /dev/null +++ b/desample_input.txt @@ -0,0 +1,3 @@ +data_block: 85E813540F0AB405 +key: 133457799BBCDFF1 +operation: decryption \ No newline at end of file diff --git a/output.txt b/output.txt index 8c93f70..eae5599 100644 --- a/output.txt +++ b/output.txt @@ -49,22 +49,40 @@ K13=100101111100010111010001111110101011101001000001 K14=010111110100001110110111111100101110011100111010 K15=101111111001000110001101001111010011111100001010 K16=110010110011110110001011000011100001011111110101 -L0=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R0=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L1=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R1=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L2=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R2=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L3=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R3=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L4=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R4=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L5=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R5=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L6=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R6=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L7=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R7=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L8=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R8=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L9=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R9=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L10=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R10=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L11=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R11=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L12=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R12=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L13=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R13=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L14=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R14=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L15=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R15=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101L16=11110000101010101111000010101010111011110100101001100101010001001100110000000001011101110000100110100010010111000000101111110100011101110010001000000000010001011000101001001111101001100011011111101001011001111100110101101001000001100100101010111010000100001101010101101001010010111001000000100100011111001100011001111010101101111101010111010111101100101100010101111000001111000111100001110101101111010001100001011000000110001100001100010101010110101100001010001100100101100000110101000011010000100011001000110100 -R16=11101111010010100110010101000100110011000000000101110111000010011010001001011100000010111111010001110111001000100000000001000101100010100100111110100110001101111110100101100111110011010110100100000110010010101011101000010000110101010110100101001011100100000010010001111100110001100111101010110111110101011101011110110010110001010111100000111100011110000111010110111101000110000101100000011000110000110001010101011010110000101000110010010110000011010100001101000010001100100011010000001010010011001101100110010101 -0123456789ABCDEF \ No newline at end of file + +L0=11001100000000001100110011111111 +R0=11110000101010101111000010101010 +L1=11110000101010101111000010101010 +R1=11101111010010100110010101000100 +L2=11101111010010100110010101000100 +R2=11001100000000010111011100001001 +L3=11001100000000010111011100001001 +R3=10100010010111000000101111110100 +L4=10100010010111000000101111110100 +R4=01110111001000100000000001000101 +L5=01110111001000100000000001000101 +R5=10001010010011111010011000110111 +L6=10001010010011111010011000110111 +R6=11101001011001111100110101101001 +L7=11101001011001111100110101101001 +R7=00000110010010101011101000010000 +L8=00000110010010101011101000010000 +R8=11010101011010010100101110010000 +L9=11010101011010010100101110010000 +R9=00100100011111001100011001111010 +L10=00100100011111001100011001111010 +R10=10110111110101011101011110110010 +L11=10110111110101011101011110110010 +R11=11000101011110000011110001111000 +L12=11000101011110000011110001111000 +R12=01110101101111010001100001011000 +L13=01110101101111010001100001011000 +R13=00011000110000110001010101011010 +L14=00011000110000110001010101011010 +R14=11000010100011001001011000001101 +L15=11000010100011001001011000001101 +R15=01000011010000100011001000110100 +L16=01000011010000100011001000110100 +R16=00001010010011001101100110010101 + +Result=85e813540f0ab405 \ No newline at end of file