Computer Networks Lab
Computer Networks Lab
TECHNOLOGY
Autonomous Institution – UGC, Govt. of India
2024-25
COMPUTER NETWORKS LAB
(R22A0596)
LAB MANUAL
COMPUTER NETWORKS
LAB MANUAL
a
Computer Networks Lab
b
Computer Networks Lab
INDEX
c
Computer Networks Lab
Algorithm:
Step 1: Initially give the user 2 choices, whether to character stuff or to directly exit,
if wrong choice is entered then prompt an invalid choice message.
Step 2: Intake from the user the number of characters which are to be
character stuffed. Step 3: Then the characters which are to be stuffed are to
be taken inside the for loop.
Step 4: Original data is displayed and the characters to be stuffed at the start and
end of the frame are uploaded in the program.
Step 5: If DLE character is present then stuff DLE character before it.
Step 6: The characters DLESTX are inserted at the start and end of
the data. Step 7: The data along with the stuffed characters are
displayed
Step 8: The original data is recovered and displayed on the
receiving side Step 9: Stop
Program :
#include<stdio.
h>
#include<conio
.h>
#include<stdlib
.h> void
charc(void);
void main()
{
int
choice;
while(1)
{
printf("\n\n\n1.character stuffing"); printf("\
n\n2.exit");printf("\n\n\nenterchoice");
d
Computer Networks Lab
scanf("%d",&choice);
printf("%d",choice);if(choice>2)
printf("\n\n invalid option ...please
renter"); switch(choice)
{
case
1:charc();
break;
case 2:_exit(0);
d
Computer Networks Lab
}
}
}
void charc(void)
{
clrscr();
char
c[50],d[50],t[50];
int i,m,j;
printf("enter the number of characters\n");
scanf("%d",&m);printf("\n enter the
characters\n"); for(i=0;i<m+1;i++)
{
scanf("%c",&c[i]);
}
printf("\n original
data\n");
for(i=0;i<m+1;i++)
printf("%c",c[i]);d[0]='
d';
d[1]='l';d[2]='e';
d[3]='s';d[4]
='t';
d[5]='x';
for(i=0,j=6;i<m+1;i++,j++)
{
if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))
{
d[j]='
d'; j+
+;
d[j]='
l'; j+
+;
d[j]='
e'; j+
+;
m=m+3;
}
d[j]=c[i];
}
m=m+
6; m+
+;
d[m]='
d'; m+
e
Computer Networks Lab
+;
d[m]=
'l'; m+
+;
d[m]='
e'; m+
+;
d[m]='
s'; m+
+;
d[m]='
t'; m+
+;
e
Computer Networks Lab
d[m]='
x'; m+
+;
printf("\n\n transmitted
data: \n"); for(i=0;i<m;i++)
{
printf("%c",d[i]);
}
for(i=6,j=0;i<m-6;i++,j++)
{
if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l
'&&d[i+5]=='e') i=i+3;
t[j]=d[i];
}
printf("\n\nreceived
data:"); for(i=0;i<j;i+
+)
{
printf("%c",t[i]);
}
}
o/p:
f
Computer Networks Lab
PROGRAM:
#include<stdio.
h>
#include<string
.h> main()
{
int
i,j,k,l,count=0,n;
char
s[100],cs[50];
clrscr();
printf("\n ENTER THE BIT STRING:");
gets(s);
n=strlen(
s);
printf("\nTHE STRING IS\
n"); for(i=0;i<n;)
{
if(s[i]==s[i+1])
{
count=
2; i++;
while(s[i]==s[i+1])
{i
+
+;
count++;
}
if(count>=5)
1
Computer Networks Lab
{
printf("$");
if(count<1
0)
printf("0");
printf("%d
%c",count,s[i]); i++;
2
Computer Networks Lab
}
else
{
for(j=0;j<count;j
++)
printf("%c",s[i]);
i++;
}
}
else
{
printf("%c",s[
i]); i++;
}
}
getch(); }
INPUT/OUTPUT:
123AAAAAAAAAATYKKKPPPPP
THE STRING IS
123$10ATYKKK$05P
3
Computer Networks Lab
PROGRAM:
#include<stdio.
h>
#include<string
.h> main()
{
int i,j,k,l,n,count;
char
s[100],cs[50];
clrscr();
4
Computer Networks Lab
}
else
{
printf("%c",s[i]);
5
Computer Networks Lab
i++;
}
}
getch(); }
INPUT/OUTPUT:
6
Computer Networks Lab
#include<stdio.
h>
#include<conio
.h>
#include<math
.h> main()
{
int b[100],b1[100],l,k,n=0,i,j,z,i1,s[20],f[8]={0,1,1,1,1,1,1,0},j1;
static int
a[100]; char
ch='y',bs[50];
clrscr();
do
{
i1=z=n
=0;
clrscr();
printf("\n Enter the bit string(space for each
byte)"); gets(bs);
for(i=0;bs[i]!='\0';i++)
if(bs[i]!=' ')
b[n++]=bs[i]-'0';
for(i=0;i<n;i++)
{
if(b[i]==
1){ i1+
+;
7
Computer Networks Lab
if(i1==5){s[z++]=i+1;i1=0;}
}
else i1=0;
}
j1=j=
0;
8
Computer Networks Lab
for(i=0;i<z;i++)
{
while(j<s[i])
b1[j1+
+]=b[j++];
b1[j1++]=0;
}
while(j1<n+z
) b1[j1+
+]=b[j++];
l=n/8;
for(i=0;l>0;i
++)
{
a[i]=l
%2;
l=l/2;
}
printf("\nAfter
stuffing :");
for(j=7;j>=0;j--)
printf("%d",a[j]
); printf(" ");
for(k=0;k<8;k
++)
printf("%d",f[k]
); printf("
");
for(k=0;k<j1;k
++)
printf("%d",b1[
k]); printf(" ");
for(k=0;k<8;k
++)
printf("%d",f[k]
);
printf("\n\n Do u want to
continue?"); ch=getch();
}
while(ch=='y' ||
ch=='Y'); getch();
}
INPUT/OUTPUT:
9
Computer Networks Lab
1
Computer Networks Lab
include<stdio.h
>
#include<conio
.h>
#include<math
.h> main()
{
int i,n,n1,k,j,ni,len;
char f[8]={'0','1','1','1','1','1','1','0'},st[100];
static int
ds[100];
clrscr();
printf("\n\nEnter the stuffed
data"); gets(st);
n=strlen(s
t);
ni=k=0;
for(i=8;i<16;i
++) if(st[i]!
=f[k++])
{
printf("\nError in
flag"); exit(1);
}
k=
0;
for(i=n-8;i<n;i+
+) if(f[k++]!
=st[i])
{
printf("\nError in
flag"); exit(1);
}
for(i=0;i<n;i
++)
st[i]=st[i]-'0'
; len=0;
j=7;
for(i=0;i<8;i++)
len+=pow(2,i)*st
[j--]; k=ni=j=0;
for(i=16;i<n-8;i+
1
Computer Networks Lab
+)
{
if(st[i]==1
){ ni++;
if(ni==5)
{ ds[j+
+]=1; i+
+;
k++;
ni=0;}
1
Computer Networks Lab
else
ds[j+
+]=1;
}
else
ds[j+
+]=0;
}
n1=n-24-k;
if(len*8!
=n1){
printf("\n Error in data
length"); exit(1);
}
printf("\n After
destuffing ");
for(i=0;i<n1;i++)
printf("%d",ds[i]);
getch();
}
INPUT/OUTPUT:
Exercise:
a) Implement K-Bit run length code on given data
b) Decode the K-Bit run length code
1
Computer Networks Lab
frame Algorithm
PROGRAM:
#include<stdio
.h>
#include<math
.h> main()
{
int i,j,k,m,n,cl;
char a[10],b[100],c[100];
clrscr();
printf("\n ENTER POLYNANOMIAL:");
scanf("%s",a);
printf("\n ENTER THE FRAME:");
scanf("%s",
b);
m=strlen(a)
;
n=strlen(b);
1
Computer Networks Lab
if(a[i]=='1')
{
m=m-
i;
break
;
}
1
Computer Networks Lab
cl=m+n-1;
for(i=0;i<n;i++) /* To copy the original
frame to c[]*/ c[i]=b[i];
for(i=n;i<cl;i++) /* To add n-1 zeros at the end
of frame */ c[i]='0';
c[i]='\0'; /*To make it as a string */
INPUT/OUTPUT:
ENTER POLYNANOMIAL:1011
10011101011
ENTER POLYNANOMIAL:00101
1
Computer Networks Lab
#include<stdio
.h>
#include<math
.h> main()
{
int i,j,k,m,n,cl;
char
a[10],c[100];
clrscr();
printf("\n ENTER POLYNANOMIAL:");
scanf("%s",a);
printf("\n ENTER THE CRC FRAME:");
scanf("%s",
c);
m=strlen(a)
;
cl=strlen(c);
n=cl-m+1;
{
printf("\n THERE IS SOME ERROR IN MESSAGE :");
break;
}
if(i==cl)
printf("\n MESSAGE IS CORRECT");
1
Computer Networks Lab
getch();
}
INPUT/OUTPUT :
MESSAGE IS CORRECT
Exercise:
1
Computer Networks Lab
Algorithm:
1. Assign to every node a tentative distance value: set it to zero for our initial node
and to infinity for all other nodes.
2. Mark all nodes unvisited. Set the initial node as current. Create a set of the
unvisited nodes called the unvisited set consisting of all the nodes.
3. For the current node, consider all of its unvisited neighbors and calculate their
tentative distances. For example, if the current node A is marked with a distance of
6, and the edge connecting it with a neighbor B has length 2, then the distance to B
(through A) will be 6 + 2 = 8. If this distance is less than the previously recorded
tentative distance of B, then overwrite that distance. Even though a neighbor has
been examined, it is not marked as "visited" at this time, and it remains in the
unvisited set.
4. When we are done considering all of the neighbors of the current node, mark the
current node as visited and remove it from the unvisited set. A visited node will
never be checked again.
5. If the destination node has been marked visited (when planning a route between
two specific nodes) or if the smallest tentative distance among the nodes in the
unvisited set is infinity (when planning a complete traversal; occurs when there is no
connection between the initial node and remaining unvisited nodes), then stop. The
algorithm has finished.
6. Select the unvisited node that is marked with the smallest tentative distance, and
set it as the new "current node" then go back to step 3.
PROGRAM:
#include<stdio.
h>
#include<string
.h>
#include<math
.h> main()
{
2
Computer Networks Lab
int u,v,num,i,j,l,k,s[10],min,cost[10]
[10],dist[10],path[10],n; clrscr();
printf("\n ENTER VERTECES:");
scanf("%d",&n);
printf("\n ENTER ADJECENCY MATRIX:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
2
Computer Networks Lab
scanf("%d",&cost[i][j]);
}
for(i=1;i<=n;i
++)
for(j=1;j<=n;j
++)
if(i==j)
cost[i][j]=0;
else if(cost[i]
[j]==-1)
cost[i][j]=30000;
for(i=1;i<=n;i++)
{
s[i]=0;
path[i]=v;
dist[i]=cost[v
][i];
}
dist[v]=0;
for(num=2;num<=n;nu
m++)
{ min=300
00; u=0;
for(i=1;i<=n;i++)
{
if(s[i]!=1)
if(min>dist
[i])
{
u=i; min=dist[i];
}
}
s[u]=1;
for(i=1;i<=n;i++)
{ if(s[i]!=1)
if(dist[i]>(min+cost[
u][i]))
2
Computer Networks Lab
{
dist[i]=min+cost[u
][i]; path[i]=u;
}
}
}
printf("\n");
2
Computer Networks Lab
}
getch();
}
INPUT/OUTPUT:
ENTER VERTECES:8
0 2 -1 -1 -1 -1 6 -1
2 0 7 -1 2 -1 -1 -1
-1 7 0 3 -1 3 -1 -1
-1 -1 3 0 -1 -1 -1 2
-1 2 -1 -1 0 2 1 -1
-1 -1 3 -1 2 0 -1 2
6 -1 -1 -1 1 -1 0 4
-1 -1 -1 2 -1 2 4 0
PATH MATRIX:
01 --> 1
22 --> 1
93 --> 2 --> 1
10 4 --> 8 --> 6 --> 5 --> 2 --> 1
45 --> 2 --> 1
Computer Networks Lab
15
Computer Networks Lab
Exercise:
1. Write a program for implementing link state routing using
Dijkstra’s algorithm
2. Write a program for Flooding algorithm
1
Computer Networks Lab
4. Take an example subnet graph with weights indicating delay between nodes
#include<stdio.
h>
#include<conio
.h> struct full
{
char
line[10],dest[10];
int hops;
}f[20];
main()
{
int
nv,min,minver,i;
char sv[2],temp;
clrscr();
printf("\nEnter number of
vertices:"); scanf("%d",&nv);
printf("\n Enter source
vertex: "); scanf("%s",sv);
printf("\n Enter full table for source vertex %s :\n",sv);
for(i=0;i<nv;i++)
scanf("%s %s
%d",f[i].dest,f[i].line,&f[i].hops);
for(i=0;i<nv;)
{
if(sv[0]==f[i].dest[0])
{
printf("\n %s %s
%d",f[i].dest,f[i].line,f[i].hops); i++;
}
else
{
min=1000;
minver=0;
temp=f[i].dest
[0];
while(temp==f[i].dest[0])
{
if(min>f[i].hops)
1
Computer Networks Lab
{
min=f[i].ho
ps;
minver=i;
}
i+
+;
}
printf("\n %c %s %d ",temp,f[minver].line,f[minver].hops);
1
Computer Networks Lab
}
}
getch();
}
INPUT/OUTPUT:
HIERARCHIAL TABLE
1A - 0
1B 1B 1
1C 1C 1
2 1B 1
3 1C 2
4 1C 3
Exercise:
1. Implement path vector routing protocol.
2. Routing Information Protocol
1
Computer Networks Lab
5. Now obtain Routing table for each node using distance vector routing
algorithm Algorithm:
1) This step initializes distances from source to all vertices as infinite and distance
to source itself as 0. Create an array dist[] of size |V| with all values as infinite
except dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the
number of vertices in given graph.
…..a) Do following for each edge v-u
………………If dist[v] > dist[u] + weightof edge uv, then
update dist[v]
………………….dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph. Do following for each
edge u-v
……If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative
weight cycle” The idea of step 3 is, step 2 guarantees shortest distances if graph
doesn’t contain negative weight cycle. If we iterate through all edges one more time
and get a shorter path for any vertex, then there is a negative weight cycle
Program:
#include<stdio.
h>
#include<math
.h>
#include<conio
.h> main()
{
int i,j,k,nv,sn,noadj,edel[20],tdel[20]
[20],min; char sv,adver[20],ch;
2
Computer Networks Lab
clrscr();
2
Computer Networks Lab
flushall();
sv=getchar
();
for(i=0;i<noadj;i++)
{
printf("\n ENTER TIME DELAY and NODE NAME:");
scanf("%d %c",&edel[i],&adver[i]);
}
for(i=0;i<noadj;i++)
{
printf("\n ENTER THE TIME DELAY FROM %c to ALL OTHER
NODES: ",adver[i]);
for(j=0;j<nv;j++)
scanf("%d",&tdel[i]
[j]);
}
INPUT/OUTPUT:
2
Computer Networks Lab
2
Computer Networks Lab
DELAY VIA--VERTEX
8 a
20 a
28 i
20 h
17 i
30 i
18 h
12 h
10 i
0 -
6 k
15 k
Exercise:
1. Routing table for each node using
hierarchical routing algorithm
2
Computer Networks Lab
22 Dept of CI
Computer Networks Lab
Algorithm:
A router creates a data packet and then sends it to each host one by one. In
this case, the router creates multiple copies of single data packet with
different destination addresses. All packets are sent as unicast but because
they are sent to all, it simulates as if router is broadcasting.
This method consumes lots of bandwidth and router must destination address
of each node.
Program:
#include<stdio
.h> int a[10]
[10],n; void
main()
{
int
i,j,root;
clrscr();
printf("Enter no.of
nodes:");
scanf("%d",&n);
printf("Enter adjacent
matrix\n"); for(i=1;i<=n;i+
+) for(j=1;j<=n;j++)
{
printf("Enter connecting of %d-->
%d::",i,j); scanf("%d",&a[i][j]);
}
printf("Enter root
node:");
scanf("%d",&root);
adj(root);
11 Dept of
Computer Networks Lab
}
adj(int k)
{
int i,j;
11 Dept of
Computer Networks Lab
Enter no.of
nodes:5 Enter
adjacent matrix
Enter connecting of 1--
>1::0 Enter connecting
of 1-->2::1 Enter
connecting of 1-->3::1
Enter connecting of 1--
>4::0 Enter connecting
of 1-->5::0 Enter
connecting of 2-->1::1
Enter connecting of 2--
>2::0 Enter connecting
of 2-->3::1 Enter
connecting of 2-->4::1
Enter connecting of 2--
>5::0 Enter connecting
of 3-->1::1 Enter
connecting of 3-->2::1
Enter connecting of 3--
>3::0 Enter connecting
of 3-->4::0 Enter
connecting of 3-->5::0
Enter connecting of 4--
>1::0 Enter connecting
of 4-->2::1 Enter
connecting of 4-->3::0
11 Dept of
Computer Networks Lab
12 Dept of
Computer Networks Lab
134
5
Exercise: 1. Implement Core-Based Tree(CBT) protocol obtain broadcast tree for it.
12 Dept of
Computer Networks Lab
7. Take a 64 bit plain text and encrypt the same using DES algorithm.
Algorithm:
12 Dept of
Computer Networks Lab
2.4.4 Substitute the values found in the S-boxes for all B[j]. Start with j = 1. All
values in the S- boxes should be considered 4 bits wide.
2.4.4.1 Take the 1st and 6th bits of B[j] together as a 2-bit value (call it m)
indicating the row in S[j] to look in for the substitution.
2.4.4.2 Take the 2nd through 5th bits of B[j] together as a 4-bit value (call it n)
indicating the column in S[j] to find the substitution.
2.4.4.3 Replace B[j] with S[j]
[m][n]. Substitution Box 1
(S[1])
2.4.4.4 Loop back to 2.4.4.1 until all 8 blocks have been replaced.
2.4.5 Permute the concatenation of B[1] through B[8] as
indicated below. Permutation P
2.4.6 Exclusive-or the resulting value with L[i-1]. Thus, all together, your R[i] =
L[i-1] xor P(S[1](B[1])...S[8](B[8])), where B[j] is a 6-bit block of E(R[i-1]) xor K[i].
(The function for R[i] is written as, R[i] = L[i-1] xor f(R[i-1], K[i]).)
2.4.7 L[i] = R[i-1].
2.4.8 Loop back to 2.4.1 until K[16] has been applied.
2.5 Perform the following permutation on the block
R[16]L[16]. Final Permutation (IP**-1)
Program:
#include<stdio.
h>
#include<conio
12 Dept of
Computer Networks Lab
int
k[15],k1[15],k2[15],i,j,p[15],p1[15],p2
[15]; int
p10[10]={3,5,2,7,4,10,1,9,8,6};
int p8[10]={6,3,7,4,8,5,10,9};
12 Dept of
Computer Networks Lab
int t1,t2,t3,t4,t[4],b[10],s,h,a;
int ip[8]={2,6,3,1,4,8,5,7};
int ep[8]={4,1,2,3,2,3,4,1};
int
ip1[8]={4,1,3,5,7,2,8,6
}; int p4[4]={2,3,4,1};
int s0[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};
int s1[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
clrscr();
printf("\n\tSimplified-DES\
n"); printf("\n");
length::\n"); for(i=0;i<8;i++)
scanf("%d",&p2[i]);
length::\n"); for(i=0;i<10;i++)
scanf("%d",&k[i]); printf("\
n\nKey Generation::\n");
for(i=0;i<10;i++)
j=p10[i]
p[i]=k[j-
12 Dept of
Computer Networks Lab
1];
t1=p[
0];
12 Dept of
Computer Networks Lab
t2=p[5];
for(i=0;i<4;i
++)
p[i]=p[i+1];
p[i]=t1;
for(i=5;i<9;i
++)
p[i]=p[i+1];
p[i]=t2;
for(i=0;i<8;i
++)
j=p8[i];
k1[i]=p[j-
1];
t1=p[
0];
t2=p[1];
t3=p[5];
t4=p[6];
for(i=0;i<3;i++)
12 Dept of
Computer Networks Lab
p[i]=p[i+2];
p[i]=t
1; i+
+;
p[i]=t2;
12 Dept of
Computer Networks Lab
for(i=5;i<8;i
++)
p[i]=p[i+2];
p[i]=t3;
i++;
p[i]=t4;
for(i=0;i<8;i
++)
j=p8[i];
k2[i]=p[j-
1];
printf("\nkey
k1::");
for(i=0;i<8;i+
+)
printf("%d",k1[i]
);
printf("\nkey
k2::");
for(i=0;i<8;i+
+)
12 Dept of
Computer Networks Lab
printf("%d",k2[i]
); for(i=0;i<8;i+
+) p[i]=p2[i];
for(a=0;a<2;a+
+)
if(a==0)
for(i=0;i<8;i++)
13 Dept of
Computer Networks Lab
j=ip[i];
p1[i]=p[j-
1];
for(i=0;i<4;i++)
if(a==0)
k[i]=p1[i+4];
if(a==1)
k[i]=p[i+4];
for(i=0;i<8;i
++)
b[i]=p[i];
for(i=0;i<8;i++)
j=ep[i];
p[i]=k[j-
1];
13 Dept of
Computer Networks Lab
for(i=0;i<8;i++)
13 Dept of
Computer Networks Lab
if(a==0)
if(p[i]==k1[i]
) k[i]=0;
else
k[i]=
1;
if(a==1)
if(p[i]==k2[i]
) k[i]=0;
else
k[i]=
1;
j=
0;
for(i=0;i<8;i=i+4)
if(k[i]==0&&k[i+3]==0)
{ t[j]
13 Dept of
Computer Networks Lab
=0;
j++;
13 Dept of
Computer Networks Lab
if(k[i]==0&&k[i+3]==1)
{ t[j]
=1;
j++;
if(k[i]==1&&k[i+3]==0)
{ t[j]
=2;
j++;
if(k[i]==1&&k[i+3]==1)
{ t[j]
=3;
j++;
if(k[i+1]==0&&k[i+2]==0)
{ t[j]
=0;
j++;
if(k[i+1]==0&&k[i+2]==1)
{ t[j]
=1;
13 Dept of
Computer Networks Lab
j++;
if(k[i+1]==1&&k[i+2]==0)
{ t[j]
=2;
j++;
if(k[i+1]==1&&k[i+2]==1)
{ t[j]
=3;
j++;
} s=s0[t[0]]
[t[1]];
h=s1[t[2]][t[3]];
if(s==0)
{ k[0]
=0;
k[1]=0;
if(s==1)
{ k[0]
=0;
13 Dept of
Computer Networks Lab
k[1]=1;
if(s==2)
{ k[0]
=1;
k[1]=0;
if(s==3)
{ k[0]
=1;
k[1]=1;
if(h==0)
{ k[2]
=0;
k[3]=0;
if(h==1)
{ k[2]
=0;
k[3]=1;
if(h==2)
13 Dept of
Computer Networks Lab
{ k[2]
=1;
k[3]=0;
if(h==3)
{ k[2]
=1;
k[3]=1;
for(i=0;i<4;i++)
j=p4[i];
p[i]=k[j-
1];
for(i=0;i<4;i++)
if(a==0)
if(p1[i]==
p[i])
k[i]=0;
else
13 Dept of
Computer Networks Lab
k[i]=
1;
13 Dept of
Computer Networks Lab
if(a==1)
if(b[i]==p[i])
k[i]=0;
else
k[i]=
1;
if(a==0)
for(i=0;i<4;i
++)
p[i]=p1[i+4]
for(i=0;i<4;i
++)
p[i+4]=k[i];
if(a==1)
for(i=4;i<8;i
++)
14 Dept of
Computer Networks Lab
k[i]=b[i];
for(i=0;i<8;i
++)
j=ip1[i];
p[i]=k[j-
1];
14 Dept of
Computer Networks Lab
printf("\n\nThe cipher
text::"); for(i=0;i<8;i++)
printf("%d",p[i]);
getch(); }
OUTPUT
******************
Simplified-DES
length:: 1
14 Dept of
Computer Networks Lab
Key Generation::
key k1::11100100
key k2::01010011
Exercise:
#include<stdio.
h>
14 Dept of
Computer Networks Lab
#include<conio
.h>
14 Dept of
Computer Networks Lab
#include<string.h>
int p10[]={3,5,2,7,4,10,1,9,8,6},
p8[]={6,3,7,4,8,5,1
0,9},
p4[]={2,4,3,1};
int ip[]={2,6,3,1,4,8,5,7},
ipinv[]={4,1,3,5,7,2,8,6},
ep[]={4,1,2,3,2,3,4,1};
int s0[][4]={{1,0,3,2,},{3,2,1,0},{0,2,1,3,},{3,1,3,2}};
int s1[][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int i;
for(i=0;i<n;i
++)
op[i]=ip[p[i]
-1]; op[i]='\
0';
int i;
char
14 Dept of
Computer Networks Lab
ch=pr[0];
for(i=0;i<n-
1;i++)
pr[i]=pr[i+1];
pr[i]=ch;
14 Dept of
Computer Networks Lab
char keytemp[11];
permute(keytemp,key,p10,10);
circularls(keytemp,5);
circularls(keytemp+5,5);
permute(k1,keytemp,p8,8);
circularls(keytemp,5);
circularls(keytemp,5);
circularls(keytemp+5,5);
circularls(keytemp+5,5);
permute(k2,keytemp,p8,8);
int i;
for(i=0;i<strlen(op)&&i<strlen(i
p);i++)
op[i]=(op[i]-'0')^(ip[i]-'0')+'0';
14 Dept of
Computer Networks Lab
int value;
14 Dept of
Computer Networks Lab
value=s[(ip[0]-'0')*2+(ip[3]-'0')][(ip[1]-'0')*2+(ip[2]-'0')];
op[0]=value/
2+'0'; op[1]=value
%2+'0'; op[2]='\
0';
char
l[5],r[5],tmp[9],tmp1[9],tmp2[
9]; strncpy(l,ip,4);
l[4]='\0';
strncpy(r,ip+4,
4); r[4]='\0';
permute(tmp,r,ep,8);
xor(tmp,k);
sbox(tmp1,tmp,s0);
sbox(tmp2,tmp+4,s1)
; strcat(tmp1,tmp2);
permute(tmp,tmp1,p4
,4); xor(tmp,l);
strcat(tmp,r);
strcpy(op,tm
14 Dept of
Computer Networks Lab
p);
15 Dept of
Computer Networks Lab
char tmp[9];
strncpy(tmp,pr+4,
4);
strncpy(tmp+4,pr,
4); tmp[8]='\0';
strcpy(pr,tmp);
void main()
char
key[11],k1[9],k2[9],plain[9],cipher[9],tmp[9
]; clrscr();
printf("enter 10 bit
key:"); gets(key);
keygen(k1,k2,key
); printf("sub key
k1::"); puts(k1);
printf("subkey
k2::"); puts(k2);
15 Dept of
Computer Networks Lab
text:"); gets(plain);
15 Dept of
Computer Networks Lab
fk(cipher,tmp,k1);
sw(cipher);
fk(tmp,cipher,k2);
permute(cipher,tmp,ipin
is::"); puts(cipher);
/* decryption
process*/
permute(tmp,cipher,i
p,8); fk(plain,tmp,k2);
sw(plain);
fk(tmp,plain,k1);
permute(plain,tmp,ipin
v,8); printf("decrypted
getch();
15 Dept of
Computer Networks Lab
15 Dept of
Computer Networks Lab
9. Using RSA algorithm encrypt a text data and Decrypt the same.
Algorithm:
RSA encrypts messages through the following algorithm, which is divided into 3 steps:
1. Key Generation
ϕ(n) ϕ(n)=(p-1)(q-1).
IV. Choose an e such that 1 < e < ϕ(n), and such that e and ϕ(n) share no divisors
other than 1(e and ϕ(n) are relatively prime).
e is kept as the public key exponent.
In other words, pick d such that de - 1 can be evenly divided by (p-1)(q-1), the
totient, or ϕ(n).This is often computed using the Extended Euclidean Algorithm, since
e and ϕ(n) are relativelyprime and d is to be the modular multiplicative inverse of e.
d is kept as the private key exponent.
The public key has modulus n and the public (or encryption) exponent e. The
private key has modulus n and the private (or decryption) exponent d, which is
kept secret.
2. Encryption
II. When Person B wishes to send the message "M" to Person A, he first
converts M to an integer such that 0 < m < n by using agreed upon reversible
15 Dept of
Computer Networks Lab
15 Dept of
Computer Networks Lab
III. Person B computes, with Person A's public key information, the ciphertext c
3. Decryption
II. Given m, Person A can recover the original message "M" by reversing the padding
scheme.
From Euler's Theorem and the Chinese Remainder Theorem, we can show that this is
true for all m and the original message
Program:
#include<stdio
.h> main()
{
int
k,b,bin[20];
15 Dept of
Computer Networks Lab
int i;
long int
c,m,e,d,n; char
ch;
char
in_file[20],out_file[20];
FILE *in,*out;
15 Dept of
Computer Networks Lab
clrscr();
printf("\n Enter any input text file name
: "); gets(in_file);
printf("\n Enter file name to store enc output
: "); gets(out_file);
in =
fopen(in_file,"r"); out
= fopen(out_file,"w");
i=-1;
b=e;
while(b>
0)
{
bin[++i] = b
%2; b=b/2;
}
k=
i;
do
{
m=
fgetc(in); d
= 1;
fputc(d,out);
}
while(!feof(in));
printf("\n File is encrfypted
successfully... "); getch();
}
15 Dept of
Computer Networks Lab
INPUT/OUTPUT:
16 Dept of
Computer Networks Lab
C:\TURBOC2>type inp.txt
abcdefghijklmnop123&*()
C:\TURBOC2>type out.txt
\§░ÉTwë│`òp0âB¢I↓v◄/☼t.
#include<stdio
.h> main()
{
int
k,b,bin[20];
int i;
long int
c,m,e,d,n; char
ch;
char
in_file[20],out_file[20];
FILE *in,*out;
clrscr();
printf("\n Enter any ciphertext file
name : "); gets(in_file);
printf("\n Enter file name to store dec output
: "); gets(out_file);
in =
fopen(in_file,"r"); out
= fopen(out_file,"w");
i=-1;
b=e;
while(b>
0)
{
bin[++i] = b
%2; b=b/2;
16 Dept of
Computer Networks Lab
}
k=
i;
do
{
m=
fgetc(in); d
= 1;
16 Dept of
Computer Networks Lab
INPUT/OUTPUT:
C:\TURBOC2>type out.txt
\§░ÉTwë│`òp0âB¢I↓v◄/☼t.
C:\TURBOC2>type inp1.txt
abcdefghijklmnop123&*()U
Exercise:
1. Implement Diffie- Hellman cryptosystem using RSA algorithm.
2. Implement SHA-512 algorithm
16 Dept of