diff --git a/DES.py b/DES.py index 4dd9adf..d5467b4 100644 --- a/DES.py +++ b/DES.py @@ -98,14 +98,76 @@ sbox = [[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7], [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8], [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]] +LEFT_SHIFT = [ + 1, 1, 2, 2, 2, 2, 2, 2, + 1, 2, 2, 2, 2, 2, 2, 1 +] + +subkeys = [] + +def hex_to_binary(hex): + return bin(int(hex, 16))[2:].zfill(64) + + +def DES_Encrypt(data_block, key): + + 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:] + + # 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.append(permute(left_key + right_key, PC2)) + print ("subkey: ", "K", i+1, subkeys[i]) + # + + # # Initial Permutation + # data_block = permute(data_block, IP) + + # # Split data_block into left and right halves + # left_half = data_block[:28] + # right_half = data_block[28:] + + print ("left key:",left_key) + print ("right key:",right_key) + +# # Generate 16 subkeys +# subkeys = generate_subkeys(key) + +# # Perform 16 rounds of DES +# for i in range(16): +# new_left_half = right_half +# right_half = xor(left_half, f(right_half, subkeys[i])) +# left_half = new_left_half + +# # Combine the left and right halves +# data_block = left_half + right_half + +# # Final Permutation +# data_block = permute(data_block, FP) + +# return data_block + +def permute(data_block, permutation): + return [data_block[i - 1] for i in permutation] + +#def generate_subkeys(key): -def DES_Encrypt(data_block, key, operation): - - - +def DES_Decrypt(data_block, key): + pass def parse_variables(file_path): @@ -122,10 +184,19 @@ def parse_variables(file_path): print(f"Error: The file {file_path} was not found.") return variables +def output(output_file, data_block): + with open(output_file, 'w') as file: + for subkeys in subkeys: + file.write("K", subkeys) + + file.write(data_block) + + def main(): # Set up command-line argument parsing parser = argparse.ArgumentParser(description='Parse variables for encryption or decryption.') - parser.add_argument('file', type=str, help='Path to the text file containing the variables.') + parser.add_argument(' input_file', type=str, help='Path to the text file containing the variables.') + parser.add_argument(' output_file', type=str, help='Path to the text file containing the output.') # Parse the arguments args = parser.parse_args() @@ -133,6 +204,9 @@ def main(): # Parse the file and retrieve variables variables = parse_variables(args.file) + # path to output file + output_file = args.output_file + # Get data_block and key from the parsed variables data_block = variables.get('data_block', '') key = variables.get('key', '') @@ -142,8 +216,12 @@ def main(): print(f"Data Block: {data_block}") print(f"Key: {key}") print(f"Operation: {operation}") + if operation == 'encryption' : - DES_Encrypt(data_block, key, operation) + DES_Encrypt(data_block, key) + + else : + DES_Decrypt(data_block, key) if __name__ == "__main__": main() \ No newline at end of file