# Untitled

unknown
plain_text
a year ago
4.6 kB
2
Indexable
Never
```import BOXES
KEYS = []
def alphaToBin(string):
converted = ""
for i in string:
b = bin(ord(i))[2:]
while len(b)!= 8:
b = f"0{b}"
converted += b
return converted

def hexToBin(hex):
hex_mapping = {'0' : "0000",
'1' : "0001",
'2' : "0010",
'3' : "0011",
'4' : "0100",
'5' : "0101",
'6' : "0110",
'7' : "0111",
'8' : "1000",
'9' : "1001",
'A' : "1010",
'B' : "1011",
'C' : "1100",
'D' : "1101",
'E' : "1110",
'F' : "1111" }
converted = ''
for i in hex:
converted += hex_mapping[i]
return converted

def binToHex(binary):
bin_mapping = {"0000" : '0',
"0001" : '1',
"0010" : '2',
"0011" : '3',
"0100" : '4',
"0101" : '5',
"0110" : '6',
"0111" : '7',
"1000" : '8',
"1001" : '9',
"1010" : 'A',
"1011" : 'B',
"1100" : 'C',
"1101" : 'D',
"1110" : 'E',
"1111" : 'F' }
converted = ''
for i in range(0, len(binary), 4):
converted += bin_mapping[binary[i:i+4]]
return converted

def leftShift(string, n):
shifted = string
for _ in range(n):
shifted = shifted[1:] + shifted[0]
return shifted

def permutation(string, keyp):
return "".join(string[i - 1] for i in keyp)

def subKeyGen(key, keyp, key_comp):
new_key = permutation(key, keyp)
lkey = new_key[:28]
rkey = new_key[28:]
for i in range(1,17):
if i in (1,2,9,16):
lkey = leftShift(lkey,1)
rkey = leftShift(rkey,1)
else:
lkey = leftShift(lkey,2)
rkey = leftShift(rkey,2)
KEYS.append(permutation(lkey+rkey, key_comp))

def compSbox(key, sbox):
row = int(key[0] + key[5], 2)
col = int(key[1:5], 2)
result = bin(sbox[row][col])[2:]
while len(result) != 4:
result = f"0{result}"
return result

def compFunc(right, key,ebox, sboxes, pbox):
right = permutation(right,ebox)
new_right = ""
for i in range(len(key)):
new_right += str(int(right[i]) ^ int(key[i]))
s = ""
s_count = 0
for i in range(0, len(new_right), 6):
temp = compSbox(new_right[i:i+6], sboxes[s_count])
s += temp
s_count += 1
return permutation(s, pbox)

def round(pt, key, ebox, sboxes, pbox):
lpt = pt[:32]
rpt = pt[32:]
temp = compFunc(rpt, key, ebox, sboxes, pbox)
new_rpt = ""
for i in range(len(lpt)):
new_rpt += str(int(lpt[i]) ^ int(temp[i]))
return [rpt, new_rpt]

def DESEncrypt(pt, key, ip, fp, keyp, key_comp, ebox, sboxes, pbox):
print("Encryption: ")
temp_pt = permutation(pt, ip)
print(f"After Initial Permutation: {binToHex(temp_pt)}")
left, right = "", ""
subKeyGen(key, keyp, key_comp)
print("Round", "Left Half".center(16), "Right Half".center(16), "Key".center(24))
for i in range(16):
temp_key = KEYS[i]
left, right = round(temp_pt, temp_key, ebox, sboxes, pbox)
temp_pt = left+right
print(f"{i+1}.".center(5), binToHex(left).center(16), binToHex(right).center(16), binToHex(temp_key).center(24))
cipher_text = permutation(right+left, fp)
return cipher_text

def DESDecrypt(ct, keys, ip, fp, ebox, sboxes, pbox):
print("Decryption: ")
r_keys = keys[::-1]
temp_ct  = permutation(ct, ip)
print(f"After Initial Permutation: {binToHex(temp_ct)}")
left, right = "", ""
print("Round", "Left Half".center(16), "Right Half".center(16), "Key".center(24))
for i in range(16):
temp_key = r_keys[i]
left, right = round(temp_ct, temp_key, ebox, sboxes, pbox)
temp_ct = left+right
print(f"{i+1}.".center(5), binToHex(left).center(16), binToHex(right).center(16), binToHex(temp_key).center(24))
plain_text = permutation(right+left, fp)
return plain_text

pt = input("Enter plain text: ")
key = input("Enter key: ")
ct = DESEncrypt(hexToBin(pt), hexToBin(key), BOXES.initial_perm, BOXES.final_perm, BOXES.keyp, BOXES.key_comp, BOXES.exp_d, BOXES.sboxes, BOXES.per)
print(f"Cipher Text: {binToHex(ct)}")
pt = DESDecrypt(ct, KEYS, BOXES.initial_perm, BOXES.final_perm, BOXES.exp_d, BOXES.sboxes, BOXES.per )
print(f"Plain Text: {binToHex(pt)}")```