0% found this document useful (0 votes)
44 views

Network Security: Assignment No 1

This document provides code to implement DES (Data Encryption Standard) encryption algorithm. It defines tables used in DES like PC1, PC2, S-boxes etc. It takes a binary key and message as input, generates 16 subkeys by circular left shifting the key, and performs 16 rounds of encryption using Feistel cipher structure. In each round, it expands the right part using expansion table, XORs with subkey, applies S-boxes substitution and permutation using P-table. After 16 rounds, it applies inverse initial and final permutations to get the cipher text in hexadecimal format.

Uploaded by

Jibran Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Network Security: Assignment No 1

This document provides code to implement DES (Data Encryption Standard) encryption algorithm. It defines tables used in DES like PC1, PC2, S-boxes etc. It takes a binary key and message as input, generates 16 subkeys by circular left shifting the key, and performs 16 rounds of encryption using Feistel cipher structure. In each round, it expands the right part using expansion table, XORs with subkey, applies S-boxes substitution and permutation using P-table. After 16 rounds, it applies inverse initial and final permutations to get the cipher text in hexadecimal format.

Uploaded by

Jibran Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Network Security

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

DES Standard Code

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)

# Intial Permutation Table


ip=(58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
)

# Expension Table Table


exp_tb=(
32,1,2,3,4,5,
4,5,6,7,8,9,
8,9,10,11,12,13,
12,13,14,15,16,17,
16,17,18,19,20,21,
20,21,22,23,24,25,
24,25,26,27,28,29,
28,29,30,31,32,1
)

# Subsistuation box(s_box) Table


s_box={
"s1":(
(14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7),
(0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8),
(4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0),
(15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13)),
"s2":(
(15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10),
(3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5),
(0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15),
(13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9)),
"s3":(
(10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8),
(13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1),
(13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7),
(1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12)),
"s4":(
(7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15),
(13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9),
(10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4),
(3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14)),
"s5":(
(2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9),
(14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6),
(4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14),
(11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3)),
"s6":(
(12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11),
(10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8),
(9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6),
(4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13)),
"s7":(
(4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1),
(13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6),
(1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2),
(6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12)),
"s8":(
(13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7),
(1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2),
(7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8),
(2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11))
}

# 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
)

# Inverse Permutation Table


Inverse_ptb=(40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25
)

# List of Keys
keys_list=[]

# Key & Message Intialized


key="133457799BBCDFF1"
Message="0123456789ABCDEF"

# Key Conversion in hexadecimal


bkey=""
for i in key:
bkey=bkey+"{0:04b}".format(int(i,16))

print("Orignal Key :",key)


print("Key (Binary) : ",bkey)

# Apply PC1 table on Key


# Convert 64bit key into 56bit

temp=copy.deepcopy(list(bkey))

bkey=""
for i in pc1:
bkey=bkey+temp[i-1]

print("\n%%%%%%%% After apply PC1 on Key %%%%%%%%%")


print("56 bit Key :",bkey)

# Divide key into 2 parts

print("\n%%%%%%%% Divide key into 2 parts %%%%%%%%%")


c0=bkey[0:28]
d0=bkey[28:57]
print("C0 :",c0,"\tD0 :",d0)

# Circular shift & generate sub keys


print("\n%%%%%%%% Circular Shift on Both Parts %%%%%%%%%")

for shift_round in range(1,17):


if shift_round==1 or shift_round==2 or shift_round==9 or
shift_round==16:
c0=c0[1:28]+c0[0]
d0=d0[1:28]+d0[0]

print("C"+str(shift_round)," :",c0,"\tD"+str(shift_round)," :",d0)


else:
c0 = c0[2:28] + c0[0:2]
d0 = d0[2:28] + d0[0:2]

print("C"+str(shift_round)," :",c0,"\tD"+str(shift_round)," :",d0)

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)

print("\n%%%%%%%% 16 Sub Keys %%%%%%%%%")


k=0
for i in keys_list:
print("key",k+1," :",i)
k=k+1

# 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)

# Apply IP table on Message

temp=copy.deepcopy(list(msg))

msg=""
for i in ip:
msg=msg+temp[i-1]

print("Message After Intial Permutation :",msg)

# DES Encryption Rounds


for rd in range(16):
print("\n$$$$$ Round "+str(rd+1)+" $$$$$")

# Divide in Right & left part


l0=msg[:32]
r0=msg[32:]

l1=copy.deepcopy(r0)

# Apply Expension table on right 32bits


# to convert into 48bits
temp = copy.deepcopy(list(r0))

r0 = ""
for i in exp_tb:
r0 = r0 + temp[i - 1]

# XOR of Key Kn & Rn message (n=1,2,3,...,16)


k=0

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=""

for rd1 in range(8):


block=xor[rd1*6:(rd1*6)+6]
rw1=block[0]
rw2=block[5]
row=int(rw1+rw2,2) # convert binary to decimal
column=int(block[1:5],2)

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)

#Aplly Inverse Permutation table on msg


temp = copy.deepcopy(list(msg))
msg = ""
for i in Inverse_ptb:
msg = msg + temp[i - 1]
else:
msg=l1+r1 # combine left and right msg
print("L"+str(rd+1)+" :", l1, "R"+str(rd+1)+" :", r1)

print("Message After Inverse Permutation :",msg)


ciper_txt=hex(int(msg,2))
print("\nCiper Text :",ciper_txt[2:])

Output

You might also like