Final
This commit is contained in:
40
DES.py
40
DES.py
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
|
|
||||||
#Constants/permutations
|
#Constants/permutations
|
||||||
PC1 = [
|
PC1 = [
|
||||||
@@ -221,10 +222,6 @@ def DES_Encrypt(data_block, key):
|
|||||||
print ("Encrypted:", encrypted_data)
|
print ("Encrypted:", encrypted_data)
|
||||||
|
|
||||||
|
|
||||||
def permute(data_block, permutation):
|
|
||||||
return [data_block[i - 1] for i in permutation]
|
|
||||||
|
|
||||||
|
|
||||||
def DES_Decrypt(data_block, key):
|
def DES_Decrypt(data_block, key):
|
||||||
|
|
||||||
data_block = hex_to_binary(data_block)
|
data_block = hex_to_binary(data_block)
|
||||||
@@ -313,7 +310,8 @@ def DES_Decrypt(data_block, key):
|
|||||||
decrypted_data.append(data_block)
|
decrypted_data.append(data_block)
|
||||||
print ("Decrypted:", decrypted_data)
|
print ("Decrypted:", decrypted_data)
|
||||||
|
|
||||||
|
def permute(data_block, permutation):
|
||||||
|
return [data_block[i - 1] for i in permutation]
|
||||||
|
|
||||||
def parse_variables(file_path):
|
def parse_variables(file_path):
|
||||||
variables = {}
|
variables = {}
|
||||||
@@ -329,6 +327,11 @@ def parse_variables(file_path):
|
|||||||
print(f"Error: The file {file_path} was not found.")
|
print(f"Error: The file {file_path} was not found.")
|
||||||
return variables
|
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):
|
def output_file(output_file, operation):
|
||||||
with open(output_file, 'w') as file:
|
with open(output_file, 'w') as file:
|
||||||
# write all the sub keys to the file remove brackets quotations and commas
|
# 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("\n")
|
||||||
file.write(f"R{i}=")
|
file.write(f"R{i}=")
|
||||||
file.write(str(right_half_f[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
|
file.write(str(right_half_f[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
|
||||||
if operation == 'encryption' :
|
|
||||||
file.write("\n")
|
|
||||||
file.write("\n")
|
if operation == 'encryption':
|
||||||
file.write("Result=")
|
write_result(file, "Result", encrypted_data)
|
||||||
file.write(str(encrypted_data).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
|
else:
|
||||||
else :
|
write_result(file, "Result", decrypted_data)
|
||||||
file.write("\n")
|
|
||||||
file.write("\n")
|
# if operation == 'encryption' :
|
||||||
file.write("Result=")
|
# file.write("\n")
|
||||||
file.write(str(decrypted_data).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
|
# 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():
|
def main():
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
# Set up command-line argument parsing
|
# Set up command-line argument parsing
|
||||||
|
|||||||
@@ -85,4 +85,4 @@ R15=01000011010000100011001000110100
|
|||||||
L16=01000011010000100011001000110100
|
L16=01000011010000100011001000110100
|
||||||
R16=00001010010011001101100110010101
|
R16=00001010010011001101100110010101
|
||||||
|
|
||||||
Result=85E813540F0AB405
|
Result=85E813540F0AB405
|
||||||
Reference in New Issue
Block a user