Final
This commit is contained in:
36
DES.py
36
DES.py
@@ -3,6 +3,7 @@
|
||||
|
||||
import argparse
|
||||
import time
|
||||
import re
|
||||
|
||||
#Constants/permutations
|
||||
PC1 = [
|
||||
@@ -221,10 +222,6 @@ def DES_Encrypt(data_block, key):
|
||||
print ("Encrypted:", encrypted_data)
|
||||
|
||||
|
||||
def permute(data_block, permutation):
|
||||
return [data_block[i - 1] for i in permutation]
|
||||
|
||||
|
||||
def DES_Decrypt(data_block, key):
|
||||
|
||||
data_block = hex_to_binary(data_block)
|
||||
@@ -313,7 +310,8 @@ def DES_Decrypt(data_block, key):
|
||||
decrypted_data.append(data_block)
|
||||
print ("Decrypted:", decrypted_data)
|
||||
|
||||
|
||||
def permute(data_block, permutation):
|
||||
return [data_block[i - 1] for i in permutation]
|
||||
|
||||
def parse_variables(file_path):
|
||||
variables = {}
|
||||
@@ -329,6 +327,11 @@ def parse_variables(file_path):
|
||||
print(f"Error: The file {file_path} was not found.")
|
||||
return variables
|
||||
|
||||
def write_result(file, label, data):
|
||||
file.write("\n\n")
|
||||
file.write(f"{label}=")
|
||||
file.write(re.sub(r"[\[\], ']", "", str(data)))
|
||||
|
||||
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
|
||||
@@ -353,16 +356,23 @@ def output_file(output_file, operation):
|
||||
file.write("\n")
|
||||
file.write(f"R{i}=")
|
||||
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("'",""))
|
||||
write_result(file, "Result", encrypted_data)
|
||||
else:
|
||||
file.write("\n")
|
||||
file.write("\n")
|
||||
file.write("Result=")
|
||||
file.write(str(decrypted_data).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
|
||||
write_result(file, "Result", decrypted_data)
|
||||
|
||||
# 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
|
||||
|
||||
Reference in New Issue
Block a user