Network Security: Assignment No 1
Network Security: Assignment No 1
Assignment No 1
Presented by
Abu Baker 17581556-052
Saad Javed 17581556-048
Muhammad Umair 17581556-033
Jibran Ahmed 17581556-013
Usama Rauf 17581556-050
Faizan Arshed 17581556-018
Kaamil Javad 17581556-034
Ali Haider Shah 17811556-007
DES
import math
import copy
# PC1 Table
pc1=(57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4)
# PC2 Table
pc2=(14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32)
# Permutation Table
p_tb=(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
)
# List of Keys
keys_list=[]
temp=copy.deepcopy(list(bkey))
bkey=""
for i in pc1:
bkey=bkey+temp[i-1]
combine_cd=c0+d0
temp = copy.deepcopy(list(combine_cd))
combine_cd = ""
for i in pc2:
combine_cd = combine_cd + temp[i - 1]
keys_list.append(combine_cd)
# MESSAGE PART
# Message Conversion in hexadecimal
print("\n%%%%%%%% Message Encryption %%%%%%%%%")
msg=""
for i in Message:
msg=msg+"{0:04b}".format(int(i,16))
print("Orignal Message :",Message)
print("Message (Binary) :",msg)
temp=copy.deepcopy(list(msg))
msg=""
for i in ip:
msg=msg+temp[i-1]
l1=copy.deepcopy(r0)
r0 = ""
for i in exp_tb:
r0 = r0 + temp[i - 1]
xor=""
for kk in keys_list[rd]:
xor=xor+str(int(r0[k],2)^int(kk,2))
k=k+1
# r0=copy.deepcopy(xor)
r0=""
sf=str(s_box["s"+str(rd1+1)][row][column])
r0=r0+"{0:04b}".format(int(sf,10))
temp = copy.deepcopy(list(r0))
r0=""
for i in p_tb:
r0 = r0 + temp[i - 1]
k=0
r1=""
for kk in r0:
r1=r1+str(int(l0[k],2)^int(kk,2))
k=k+1
if rd==15:
msg=r1+l1 # last round combine left and right in
reverse
print("L"+str(rd+1)+" :", l1, "R"+str(rd+1)+" :", r1)
print("\nCombine L16 & R16 in Reverse(R16L16) :",msg)
Output