Encrypt finished

This commit is contained in:
2024-10-07 21:53:56 -07:00
parent 505a08645c
commit 05ba84c4ec
2 changed files with 165 additions and 49 deletions

129
DES.py
View File

@@ -35,6 +35,17 @@ IP = [
63, 55, 47, 39, 31, 23, 15, 7
]
P = [
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
]
FP = [
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
@@ -102,74 +113,108 @@ LEFT_SHIFT = [
1, 1, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 2, 1
]
subkeys_left = []
subkeys_right = []
subkeys = []
left_half_f = []
right_half_f = []
data_block = ""
def hex_to_binary(hex):
return bin(int(hex, 16))[2:].zfill(64)
def binary_to_hex(binary):
return hex(int(binary, 2))[2:].zfill(16)
def DES_Encrypt(data_block, key):
data_block = hex_to_binary(data_block)
key = hex_to_binary(key)
print ("binary key:", key)
#print ("binary key:", key)
key = permute(key, PC1)
print ("permutated key:", key)
#print ("permutated key:", key)
# generate 16 subkeys
left_key = key[:28]
right_key = key[28:]
# left shift via iteration C0 and D0
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))
# print ("subkey: ", "K", i+1, subkeys[i])
# # Initial Permutation
# data_block = permute(data_block, IP)
# 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[:28]
# right_half = data_block[28:]
# 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)
# print ("left half:", left_half)
# print ("right half:", right_half)
print ("left key:",left_key)
print ("right key:",right_key)
# 16 rounds of DES
for i in range(16):
# Expansion
expanded_right = permute(right_half, E)
#print ("expanded right half:", expanded_right)
# # Generate 16 subkeys
# subkeys = generate_subkeys(key)
# 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)
# # 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
# 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)
# # Combine the left and right halves
# data_block = left_half + right_half
# Permutation
permuted_result = permute(sbox_result, P)
#print ("Permutated result:", permuted_result)
# # Final Permutation
# data_block = permute(data_block, FP)
# 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)
# return data_block
# Swap left and right halves
left_half = right_half
right_half = xor_result
left_half_f.append(left_half)
right_half_f.append(right_half)
print ("left half:", left_half)
print ("right half:", right_half)
# 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)
def permute(data_block, permutation):
return [data_block[i - 1] for i in permutation]
#def generate_subkeys(key):
def DES_Decrypt(data_block, key):
pass
@@ -191,10 +236,28 @@ def parse_variables(file_path):
def output_file(output_file, data_block):
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):
file.write(f"C{i}=")
file.write(str(subkeys_left[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
file.write("\n")
file.write(f"D{i}=")
file.write(str(subkeys_right[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
file.write("\n")
file.write("\n")
for i in range(16):
file.write(f"K{i+1} ")
file.write(f"K{i+1}=")
file.write(str(subkeys[i]).replace("[","").replace("]","").replace(",","").replace(" ","").replace("'",""))
file.write("\n")
for i in range(17):
file.write(f"L{i}=")
file.write(str(left_half_f).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)

View File

@@ -1,17 +1,70 @@
K1 000110110000001011101111111111000111000001110010
K2 011110011010111011011001110110111100100111100101
K3 010101011111110010001010010000101100111110011001
K4 011100101010110111010110110110110011010100011101
K5 011111001110110000000111111010110101001110101000
K6 011000111010010100111110010100000111101100101111
K7 111011001000010010110111111101100001100010111100
K8 111101111000101000111010110000010011101111111011
K9 111000001101101111101011111011011110011110000001
K10 101100011111001101000111101110100100011001001111
K11 001000010101111111010011110111101101001110000110
K12 011101010111000111110101100101000110011111101001
K13 100101111100010111010001111110101011101001000001
K14 010111110100001110110111111100101110011100111010
K15 101111111001000110001101001111010011111100001010
K16 110010110011110110001011000011100001011111110101
C0=1111000011001100101010101111
D0=0101010101100110011110001111
C1=1110000110011001010101011111
D1=1010101011001100111100011110
C2=1100001100110010101010111111
D2=0101010110011001111000111101
C3=0000110011001010101011111111
D3=0101011001100111100011110101
C4=0011001100101010101111111100
D4=0101100110011110001111010101
C5=1100110010101010111111110000
D5=0110011001111000111101010101
C6=0011001010101011111111000011
D6=1001100111100011110101010101
C7=1100101010101111111100001100
D7=0110011110001111010101010110
C8=0010101010111111110000110011
D8=1001111000111101010101011001
C9=0101010101111111100001100110
D9=0011110001111010101010110011
C10=0101010111111110000110011001
D10=1111000111101010101011001100
C11=0101011111111000011001100101
D11=1100011110101010101100110011
C12=0101111111100001100110010101
D12=0001111010101010110011001111
C13=0111111110000110011001010101
D13=0111101010101011001100111100
C14=1111111000011001100101010101
D14=1110101010101100110011110001
C15=1111100001100110010101010111
D15=1010101010110011001111000111
C16=1111000011001100101010101111
D16=0101010101100110011110001111
K1=000110110000001011101111111111000111000001110010
K2=011110011010111011011001110110111100100111100101
K3=010101011111110010001010010000101100111110011001
K4=011100101010110111010110110110110011010100011101
K5=011111001110110000000111111010110101001110101000
K6=011000111010010100111110010100000111101100101111
K7=111011001000010010110111111101100001100010111100
K8=111101111000101000111010110000010011101111111011
K9=111000001101101111101011111011011110011110000001
K10=101100011111001101000111101110100100011001001111
K11=001000010101111111010011110111101101001110000110
K12=011101010111000111110101100101000110011111101001
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