COMPUTER NETWORKS LAB
S.No.
Name of the experiment
1
Implement the data link layer framing methods such as character stuffing and bit stuffing.
2
Implement on a data set of characters the three CRC polynomials-CRC 12,CRC 16 and CRC CCIP.
3
Implement Dijkstra’s algorithm to compute the Shortest path through a graph.
4
Take an example subnet graph with weights indicating delay between nodes. Now obtain Routing table art each node using distance vector routing algorithm.
5
Take an example subnet of hosts. Obtain broadcast tree for it.
6
Take a 64 bit plain text and encrypt the same using DES algorithm.
7
Write a program to break the above DES coding.
8
Using RSA algorithm Encrypt a text data and Decrypt the same.
/* CHARACTER STUFFING AND BIT STUFFING. */
Aim:-To implement the data link layer framing methods- character stuffing and bit stuffing.
Program:-
main()
{
int j,n,k=0,c=0,i,t;
int os[80],ns[100],s[]={0,1,1,1,1,1,1,0};
clrscr();
printf(“\n enter the number of bits:”);
scanf(“%d”,&n);
printf(“\n enter the bits:”);
for(i=0;i
scanf(“%d”,&os[i]);
for(j=0;j<8;j++)
{ ns[k]=s[j];
k++;
}
for(j=0;j
{ t=j;
if(os[t]==1&&os[t++]==1&&os[t++]==1&&os[t++]==1&&os[t++]==1)
{
for(i=0;i<5;i++)
{ ns[k]=0;
k++;
j=j+5;
else
{ ns[k]=os[j];
k++;
} }
for(j=0;j<8;j++)
{
ns[k]=s[j];
k++; }
for(j=0;j
printf(“%d”,ns[j]);
getch(); }
/*1. IMPLEMENT THE DATALINK LAYER FRAMING METHODS SUCH AS CHARACTER STUFFING AND BIT STUFFING. */
main()
{
int i, a[50],n,k=1,j=1,m=1,p=0,q=1,c=0,b[10],po,h;
clrscr();
printf(“\n enter total length of the frame::”);
scanf(“%d”,&n);
printf(“\n now enter the string::”);
for(i=0;i
scanf(“%d”,&a[i]);
i=0;
while(p
{
printf(“\n the number of characters in the %d frame is %d”,k,a[i]);
b[q]=a[i];
for(m=1;m
printf(“\n the frame data is:: %d”,a[j]);
q++;
c++;
p=p+a[i];
j++;
i=i+a[i];
k++;
}
printf(“\n enter the frame number and the frame header”);
scanf(“%d%d”,&po,&h);
for(q=1;q<=c;q++)
{
if(po==q)
{
if(b[q]==h)
printf(“\n there is no error in the frame”);
else
printf(“\n there is an error in the frame”);
}
}
getch();
}
/*1. IMPLEMENT THE DATALINK LAYER FRAMING METHODS SUCH AS CHARACTER STUFFING AND BIT STUFFING. */
main()
{
int j,n,i,t,l,p;
char os[40];
clrscr();
printf(“\n enter the text”);
gets(os);
l=strlen(os);
printf(“STX DEL”);
for(j=0;j<1;j++)
{
t=j;
if(os[t++]==’d’&&os[t++]==’I’&&os[t]==’e’)
{
printf(“DEL del”);
j=j+3;
}
else
printf(“%c”,os[j]);
}
printf(“ETX DEL”);
getch();
}
/* 2.IMPLEMENT ON A DATA SET OF CHARECTERS THE CRC POLYNOMIALS-CRC12,CRC16 AND CCIP. */
Aim:-To implement CRC polynomials.
Program:-
int n,a[50],I,co,p,d[40],b[40],e,f;
main()
{
clrscr();
printf(“\n enter the generator length::”);
scanf(“%d”,&e);
printf(“\n enter the generator ::”);
for(i=0;i
scanf(“%d”,&b[i]);
printf(“\n enter the length of polynomial ::”);
scanf(“%d”,&f);
printf(“\n the polynomial ::”);
for(i=0;i
{ scanf(“%d”,&a[i]);
d[i]=a[i];
}
printf(“\n the polynomial at sender side is ::”);
func();
func();
if(co>0)
printf(“\n error occurred in transmission”);
else
printf(“\n no error occurred in transmission”);
getch();
}
int func()
{ int k,c=0,q=0,j,m,l,x;
if(p==0)
{ n=f+e-1;
for(i=0;i
printf(“%d”,a[i]);
p++;
printf(“\n crc division on sender side”); }
else
{ printf(“\n enter the remainder ::”);
for(i=f;i
scanf(“%d”,&d[i]);
printf(“\n now append this remainder to polynomial”);
printf(“\n now the polynomial at receiver ::”);
printf(“\n crc division on receiver side”);
for(i=0;i
{ a[i]=d[i];
printf(“%d”,a[i]);
} }
for(q=0;q
{
for(k=0;j=c;j
{
if(a[j]!=b[k])
a[j]=1;
else
a[j]=0;
}
j=c;
for(j=c;j
{
if(a[j]==0)
c++;
else
break;
}
q=c;
}
printf(“\n the remainder is ::”);
co=0;
for(i=f;f
{
printf(“%d,a[i]”);
if(a[i]==1)
co++;
}
return co;
}
/* 3. IMPLEMENT DIJKSTRA’S ALGORITHM TO COMPUTE A SHORTEST PATH THROUGH GRAPH. */
Aim:-To implement Dijkstra’s algorithm to compute shortest path through graph.
Program:-
#include
#define INFINITY 5000
int n,dist[10][10],path[10];
struct state
{
int predecessor;
int length;
enum {permanent,tentative} lable;
}state[10];
struct state *p;
main()
{
int s,t,i,j;
clrscr();
printf(“Enter the no of vertices”);
scanf(“%d”,&n);
printf(“Enter adjacency matrix”);
for(i=0;i
for(j=0;j
scanf(“%d”,&dist[i][j]);
printf(“enter the source vertex and destination vertex”);
scanf(“%d%d”,&s,&t);
shortest_path(int s,int t)
{
int i,k,min,j;
for(p=&state[0];p<&state[n];p++)
{
p->predecessor=-1;
p->length=INFINITY;
p->lable=tentative;
}
state[t].length=0;
state[t].lable=permanent;
k=t;
do
{
for(i=0;i
if(dist[k][i]!=0&&state[i].lable==tentative)
{
if(state[k].length+dist[k][i]
{
state[i].predecessor=k;
state[i].length=state[k].length+dist[k][i];
}
}
k=0;
min=INFINITY;
for(i=0;i
if(state[i].lable==tentative&&state[i].length
{
min=state[i].length;
k=i;
}
state[k].lable=permanent;
}while(k!=s);
i=0;
k=s;
do{
path[i++]=k;
k=state[k].predecessor;
}while(k.=0);
printf(“the shortest path is :”);
for(j=i-1;j>=0;j--)
printf(“\t %d\t”,path[j]);
printf(“\n the shortest distance is %d”,state[s].length);
}
4. TAKE AN EXAMPLE SUBNET GRAPH WITH WEIGHTS INCLUDING DELAY BETWEEN NODES.
Aim:-To obtain Routing table for a given subnet graph using distance vector routing algorithm.
Program:-
#include
int A[12]={0,12,25,40,14,23,18,17,21,9,24,29};
int I[12]={24,36,18,27,7,20,31,20,0,11,22,33};
int H[12]={20,31,19,8,30,19,6,0,14,7,21,9};
int k[12]={21,28,36,24,22,40,31,19,22,10,0,9};
char s[12]={‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’};
char s1[4]={‘A’,’I’,’H’,’K’};
int ejd[4]={8,10,12,6},i,j,k,l,temp,b[4],a[4],f;
main()
{
clrscr();
printf(“\n the estimated hops for the nodes A,I,H,K are:”);
for(i=0;i<12;i++)
printf(“\n%d\t%d\t%d\t%d”,A[i],I[i],H[i],K[i]);
printf(“\n the delays from j are :”);
for(i=0;i<4;i++)
printf(“\t%d”,ejd[i]);
printf(“\n the new routing table for j is:”);
for(i=0;i<12;i++)
{
if(i==9)
printf(“\n jà j\t0\t-“);
else
{
k=0;
j=0;
b[j++]=A[i]+ejd[k];
b[j++]=I[i]+ejd[k+1];
b[j++]=H[i]+ejd[k+1];
b[j]=k[i]+ejd[k+1];
for(k=0;k<4;k++)
for(j=k+1;j<4;j++)
{
if(b[k]
f=0;
else
{
temp=b[j];
b[j]=b[k];
b[k]=temp;
}
}
printf(“\n”);
for(j=0;j<4;j++)
if(b[0]==a[j])
printf(“JÃ %c\t%d\t%c”,s[i],b[0],s1[j]);
}
}
getch();
}
5. TAKE AN EXAMPLE SUBNET OF HOSTS.OBTAIN BROABCAST TREE FOR IT.
Aim:-To obtain broadcast tree for a given sunet.
Program:-
main()
{
int a[4][4]={{99,3,5,99},{3,99,4,6},{5,4,99,6},{99,6,7,99}};
int i,j,b[4],k,n,flag,temp,l;
clrscr();
n=4;
printf(“\n broadcast routing edges are: \n”);
for(i=0;i
{ k=0;
for(j=0;j
{ b[k]=a[i][j];
k++;
} }
for(k=0;k
for(j=k+1;j
{
if(b[k]
flag=0;
else
{
temp=b[j];
b[j]=b[k];
b[k]=temp;
} }
for(j=0;j
{ if(b[0]==a[i][j])
{ if(i
{ printf(“\n %dà %d\t%d”,i,j,a[i][j]);
a[i][j]=a[j][i]=99; }
else
l=1;
} } }
getch(); }
6. TAKE A 64-BIT PLAIN TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.
Aim:-To encrypt a 64-bit plain text using DES algorithm.
Program:-
int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1;
int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1};
int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8];
int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8];
int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5};
main()
{
clrscr();
printf(“\n Enter the 8 bits of plain text:”);
for(i=0;i<8;i++)
scanf(“%d”,&p1[i]);
printf(“\n enter the 10-bit key:”);
scanf(“%d”, &k[i]);
for(i=0;i<8;i++)
ip[i]=p1[ipi[i]];
fn();
c++;
fn();
for(i=0;i<4;i++)
ip[i]=r[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=f2[i];
for(i=0;i<8;i++)
cp[i]=ip[ipii[i]];
printf(“\n The cipher text is \n”);
for(i=0;i<8;i++)
printf(“%d,\t”,cp[i]);
getch();
}
int fn()
{
for(i=0,j=0;i<4;i++,j++)
f1[j]=ip[i];
for(i=4,j=0;i<8;i++,j++)
f2[j]=ip[i];
for(i=0;i<8;i++)
ep[i]=f2[epi[i]];
if(c==0)
{
for(i=0;i<8;i++)
{
if(k1[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
else
{
for(i=0;i<8;i++)
{
if(k2[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
r1=rs[0]*2+rs[3]*1;
c1=rs[1]*2+rs[2]*1;
r2=rs[4]*2+rs[7]*1;
c2=rs[5]*2+rs[6]*1;
s0=s00[r1][c1];
s1=s11[r2][c2];
i=0;
p[i]=s0/2;i++;
p[i]=s0%2;i++;
p[i]=s1/2;i++;
p[i]=s1%2;
for(i=0;i<4;i++)
p4[i]=p[p44[i]];
for(i=0;i<4;i++)
{
if(p4[i]==f1[i])
r[i]=0;
else
r[i]=1;
}
for(i=0;i<4;i++)
ip[i]=f2[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=r[i];
}
7.TAKE A 64-BIT PLAIN TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.
Aim:- To encrypt a 64-bit plain text using DES algorithm.
Program:-
int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1;
int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1};
int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8];
int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8];
int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5};
main()
{
clrscr();
printf(“\n Enter the 8 bits of plain text:”);
for(i=0;i<8;i++)
scanf(“%d”,&p1[i]);
printf(“\n enter the 10-bit key:”);
scanf(“%d”, &k[i]);
for(i=0;i<8;i++)
ip[i]=p1[ipi[i]];
fn();
c++;
fn();
for(i=0;i<4;i++)
ip[i]=r[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=f2[i];
for(i=0;i<8;i++)
cp[i]=ip[ipii[i]];
printf(“\n The cipher text is \n”);
for(i=0;i<8;i++)
printf(“%d,\t”,cp[i]);
getch();
}
int fn()
{
for(i=0,j=0;i<4;i++,j++)
f1[j]=ip[i];
for(i=4,j=0;i<8;i++,j++)
f2[j]=ip[i];
for(i=0;i<8;i++)
ep[i]=f2[epi[i]];
if(c==0)
{
for(i=0;i<8;i++)
{
if(k1[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
else
{
for(i=0;i<8;i++)
{
if(k2[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
r1=rs[0]*2+rs[3]*1;
c1=rs[1]*2+rs[2]*1;
r2=rs[4]*2+rs[7]*1;
c2=rs[5]*2+rs[6]*1;
s0=s00[r1][c1];
s1=s11[r2][c2];
i=0;
p[i]=s0/2;i++;
p[i]=s0%2;i++;
p[i]=s1/2;i++;
p[i]=s1%2;
for(i=0;i<4;i++)
p4[i]=p[p44[i]];
for(i=0;i<4;i++)
{
if(p4[i]==f1[i])
r[i]=0;
else
r[i]=1;
}
for(i=0;i<4;i++)
ip[i]=f2[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=r[i];
}
8. USING RSA ALGORITHM ENCRYPT A TEXT DATA AND DECRYPT THE SAME.
Aim:- To encrypt and decrypt a text RSA algorithm.
Program:-
#include
#include
char *r,en[10],de[10],b[10],g;
char a[26]={‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’I’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’};
main()
{
int q,l=0,j,cop[50],flag=0,f;
int temp[10],p,e,d,I,n,z,m,g;
unsigned long c;
clrscr();
printf(“\n Enter the plaintext at the end type”);
while((b[i]=getchar())!=’@’)
i++;
r=b;
printf(“\n Enter two prime numbers”);
scanf(“%d%d”,&p&q);
n=p*q;
z=(p-1)*(q-1);
i=0;j=0;
for(f=3;f
{
if(f%2==0)
flag=0;
else
{
cop[i]=f;
i++;j++;
}
}
printf(“\n The coprimes for %d are”,z);
for(i=0;i
printf(“%d\t”,cop[i]);
printf(“\n enter one coprime”);
scanf(“%d”,&e);
j=0;
while(*r!=’@’)
{
for(i=0;i!=25&&flag==0;i++)
{
if(*r==a[i])
{
c=pow(I,e)
g=c%n;
temp[j]=g;
flag=1;
en[j]=toupper(a[g]);
j++;
} }
r++;
flag=0; }
printf(“\n The cipher text is:”);
for(i=0;i
printf(“%c”,en[i]);
for(d=1;;d++)
{
if((e*d)%z==1)
break;
}
printf(“\n The decryption key is %d”,d);
printf(“\n Enter decryption key”);
scanf(“%d”,&d);
printf(“\n the decrypted text is:”);
for(i=0;i
{
c=pow(temp[i],d);
m=c%n;
printf(“%c”,a[m]);
}
getch();
}
S.No.
Name of the experiment
1
Implement the data link layer framing methods such as character stuffing and bit stuffing.
2
Implement on a data set of characters the three CRC polynomials-CRC 12,CRC 16 and CRC CCIP.
3
Implement Dijkstra’s algorithm to compute the Shortest path through a graph.
4
Take an example subnet graph with weights indicating delay between nodes. Now obtain Routing table art each node using distance vector routing algorithm.
5
Take an example subnet of hosts. Obtain broadcast tree for it.
6
Take a 64 bit plain text and encrypt the same using DES algorithm.
7
Write a program to break the above DES coding.
8
Using RSA algorithm Encrypt a text data and Decrypt the same.
/* CHARACTER STUFFING AND BIT STUFFING. */
Aim:-To implement the data link layer framing methods- character stuffing and bit stuffing.
Program:-
main()
{
int j,n,k=0,c=0,i,t;
int os[80],ns[100],s[]={0,1,1,1,1,1,1,0};
clrscr();
printf(“\n enter the number of bits:”);
scanf(“%d”,&n);
printf(“\n enter the bits:”);
for(i=0;i
scanf(“%d”,&os[i]);
for(j=0;j<8;j++)
{ ns[k]=s[j];
k++;
}
for(j=0;j
{ t=j;
if(os[t]==1&&os[t++]==1&&os[t++]==1&&os[t++]==1&&os[t++]==1)
{
for(i=0;i<5;i++)
{ ns[k]=0;
k++;
j=j+5;
else
{ ns[k]=os[j];
k++;
} }
for(j=0;j<8;j++)
{
ns[k]=s[j];
k++; }
for(j=0;j
printf(“%d”,ns[j]);
getch(); }
/*1. IMPLEMENT THE DATALINK LAYER FRAMING METHODS SUCH AS CHARACTER STUFFING AND BIT STUFFING. */
main()
{
int i, a[50],n,k=1,j=1,m=1,p=0,q=1,c=0,b[10],po,h;
clrscr();
printf(“\n enter total length of the frame::”);
scanf(“%d”,&n);
printf(“\n now enter the string::”);
for(i=0;i
scanf(“%d”,&a[i]);
i=0;
while(p
{
printf(“\n the number of characters in the %d frame is %d”,k,a[i]);
b[q]=a[i];
for(m=1;m
printf(“\n the frame data is:: %d”,a[j]);
q++;
c++;
p=p+a[i];
j++;
i=i+a[i];
k++;
}
printf(“\n enter the frame number and the frame header”);
scanf(“%d%d”,&po,&h);
for(q=1;q<=c;q++)
{
if(po==q)
{
if(b[q]==h)
printf(“\n there is no error in the frame”);
else
printf(“\n there is an error in the frame”);
}
}
getch();
}
/*1. IMPLEMENT THE DATALINK LAYER FRAMING METHODS SUCH AS CHARACTER STUFFING AND BIT STUFFING. */
main()
{
int j,n,i,t,l,p;
char os[40];
clrscr();
printf(“\n enter the text”);
gets(os);
l=strlen(os);
printf(“STX DEL”);
for(j=0;j<1;j++)
{
t=j;
if(os[t++]==’d’&&os[t++]==’I’&&os[t]==’e’)
{
printf(“DEL del”);
j=j+3;
}
else
printf(“%c”,os[j]);
}
printf(“ETX DEL”);
getch();
}
/* 2.IMPLEMENT ON A DATA SET OF CHARECTERS THE CRC POLYNOMIALS-CRC12,CRC16 AND CCIP. */
Aim:-To implement CRC polynomials.
Program:-
int n,a[50],I,co,p,d[40],b[40],e,f;
main()
{
clrscr();
printf(“\n enter the generator length::”);
scanf(“%d”,&e);
printf(“\n enter the generator ::”);
for(i=0;i
scanf(“%d”,&b[i]);
printf(“\n enter the length of polynomial ::”);
scanf(“%d”,&f);
printf(“\n the polynomial ::”);
for(i=0;i
{ scanf(“%d”,&a[i]);
d[i]=a[i];
}
printf(“\n the polynomial at sender side is ::”);
func();
func();
if(co>0)
printf(“\n error occurred in transmission”);
else
printf(“\n no error occurred in transmission”);
getch();
}
int func()
{ int k,c=0,q=0,j,m,l,x;
if(p==0)
{ n=f+e-1;
for(i=0;i
printf(“%d”,a[i]);
p++;
printf(“\n crc division on sender side”); }
else
{ printf(“\n enter the remainder ::”);
for(i=f;i
scanf(“%d”,&d[i]);
printf(“\n now append this remainder to polynomial”);
printf(“\n now the polynomial at receiver ::”);
printf(“\n crc division on receiver side”);
for(i=0;i
{ a[i]=d[i];
printf(“%d”,a[i]);
} }
for(q=0;q
{
for(k=0;j=c;j
{
if(a[j]!=b[k])
a[j]=1;
else
a[j]=0;
}
j=c;
for(j=c;j
{
if(a[j]==0)
c++;
else
break;
}
q=c;
}
printf(“\n the remainder is ::”);
co=0;
for(i=f;f
{
printf(“%d,a[i]”);
if(a[i]==1)
co++;
}
return co;
}
/* 3. IMPLEMENT DIJKSTRA’S ALGORITHM TO COMPUTE A SHORTEST PATH THROUGH GRAPH. */
Aim:-To implement Dijkstra’s algorithm to compute shortest path through graph.
Program:-
#include
#define INFINITY 5000
int n,dist[10][10],path[10];
struct state
{
int predecessor;
int length;
enum {permanent,tentative} lable;
}state[10];
struct state *p;
main()
{
int s,t,i,j;
clrscr();
printf(“Enter the no of vertices”);
scanf(“%d”,&n);
printf(“Enter adjacency matrix”);
for(i=0;i
for(j=0;j
scanf(“%d”,&dist[i][j]);
printf(“enter the source vertex and destination vertex”);
scanf(“%d%d”,&s,&t);
shortest_path(int s,int t)
{
int i,k,min,j;
for(p=&state[0];p<&state[n];p++)
{
p->predecessor=-1;
p->length=INFINITY;
p->lable=tentative;
}
state[t].length=0;
state[t].lable=permanent;
k=t;
do
{
for(i=0;i
if(dist[k][i]!=0&&state[i].lable==tentative)
{
if(state[k].length+dist[k][i]
{
state[i].predecessor=k;
state[i].length=state[k].length+dist[k][i];
}
}
k=0;
min=INFINITY;
for(i=0;i
if(state[i].lable==tentative&&state[i].length
{
min=state[i].length;
k=i;
}
state[k].lable=permanent;
}while(k!=s);
i=0;
k=s;
do{
path[i++]=k;
k=state[k].predecessor;
}while(k.=0);
printf(“the shortest path is :”);
for(j=i-1;j>=0;j--)
printf(“\t %d\t”,path[j]);
printf(“\n the shortest distance is %d”,state[s].length);
}
4. TAKE AN EXAMPLE SUBNET GRAPH WITH WEIGHTS INCLUDING DELAY BETWEEN NODES.
Aim:-To obtain Routing table for a given subnet graph using distance vector routing algorithm.
Program:-
#include
int A[12]={0,12,25,40,14,23,18,17,21,9,24,29};
int I[12]={24,36,18,27,7,20,31,20,0,11,22,33};
int H[12]={20,31,19,8,30,19,6,0,14,7,21,9};
int k[12]={21,28,36,24,22,40,31,19,22,10,0,9};
char s[12]={‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’};
char s1[4]={‘A’,’I’,’H’,’K’};
int ejd[4]={8,10,12,6},i,j,k,l,temp,b[4],a[4],f;
main()
{
clrscr();
printf(“\n the estimated hops for the nodes A,I,H,K are:”);
for(i=0;i<12;i++)
printf(“\n%d\t%d\t%d\t%d”,A[i],I[i],H[i],K[i]);
printf(“\n the delays from j are :”);
for(i=0;i<4;i++)
printf(“\t%d”,ejd[i]);
printf(“\n the new routing table for j is:”);
for(i=0;i<12;i++)
{
if(i==9)
printf(“\n jà j\t0\t-“);
else
{
k=0;
j=0;
b[j++]=A[i]+ejd[k];
b[j++]=I[i]+ejd[k+1];
b[j++]=H[i]+ejd[k+1];
b[j]=k[i]+ejd[k+1];
for(k=0;k<4;k++)
for(j=k+1;j<4;j++)
{
if(b[k]
f=0;
else
{
temp=b[j];
b[j]=b[k];
b[k]=temp;
}
}
printf(“\n”);
for(j=0;j<4;j++)
if(b[0]==a[j])
printf(“JÃ %c\t%d\t%c”,s[i],b[0],s1[j]);
}
}
getch();
}
5. TAKE AN EXAMPLE SUBNET OF HOSTS.OBTAIN BROABCAST TREE FOR IT.
Aim:-To obtain broadcast tree for a given sunet.
Program:-
main()
{
int a[4][4]={{99,3,5,99},{3,99,4,6},{5,4,99,6},{99,6,7,99}};
int i,j,b[4],k,n,flag,temp,l;
clrscr();
n=4;
printf(“\n broadcast routing edges are: \n”);
for(i=0;i
{ k=0;
for(j=0;j
{ b[k]=a[i][j];
k++;
} }
for(k=0;k
for(j=k+1;j
{
if(b[k]
flag=0;
else
{
temp=b[j];
b[j]=b[k];
b[k]=temp;
} }
for(j=0;j
{ if(b[0]==a[i][j])
{ if(i
{ printf(“\n %dà %d\t%d”,i,j,a[i][j]);
a[i][j]=a[j][i]=99; }
else
l=1;
} } }
getch(); }
6. TAKE A 64-BIT PLAIN TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.
Aim:-To encrypt a 64-bit plain text using DES algorithm.
Program:-
int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1;
int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1};
int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8];
int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8];
int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5};
main()
{
clrscr();
printf(“\n Enter the 8 bits of plain text:”);
for(i=0;i<8;i++)
scanf(“%d”,&p1[i]);
printf(“\n enter the 10-bit key:”);
scanf(“%d”, &k[i]);
for(i=0;i<8;i++)
ip[i]=p1[ipi[i]];
fn();
c++;
fn();
for(i=0;i<4;i++)
ip[i]=r[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=f2[i];
for(i=0;i<8;i++)
cp[i]=ip[ipii[i]];
printf(“\n The cipher text is \n”);
for(i=0;i<8;i++)
printf(“%d,\t”,cp[i]);
getch();
}
int fn()
{
for(i=0,j=0;i<4;i++,j++)
f1[j]=ip[i];
for(i=4,j=0;i<8;i++,j++)
f2[j]=ip[i];
for(i=0;i<8;i++)
ep[i]=f2[epi[i]];
if(c==0)
{
for(i=0;i<8;i++)
{
if(k1[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
else
{
for(i=0;i<8;i++)
{
if(k2[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
r1=rs[0]*2+rs[3]*1;
c1=rs[1]*2+rs[2]*1;
r2=rs[4]*2+rs[7]*1;
c2=rs[5]*2+rs[6]*1;
s0=s00[r1][c1];
s1=s11[r2][c2];
i=0;
p[i]=s0/2;i++;
p[i]=s0%2;i++;
p[i]=s1/2;i++;
p[i]=s1%2;
for(i=0;i<4;i++)
p4[i]=p[p44[i]];
for(i=0;i<4;i++)
{
if(p4[i]==f1[i])
r[i]=0;
else
r[i]=1;
}
for(i=0;i<4;i++)
ip[i]=f2[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=r[i];
}
7.TAKE A 64-BIT PLAIN TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.
Aim:- To encrypt a 64-bit plain text using DES algorithm.
Program:-
int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1;
int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1};
int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8];
int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8];
int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5};
main()
{
clrscr();
printf(“\n Enter the 8 bits of plain text:”);
for(i=0;i<8;i++)
scanf(“%d”,&p1[i]);
printf(“\n enter the 10-bit key:”);
scanf(“%d”, &k[i]);
for(i=0;i<8;i++)
ip[i]=p1[ipi[i]];
fn();
c++;
fn();
for(i=0;i<4;i++)
ip[i]=r[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=f2[i];
for(i=0;i<8;i++)
cp[i]=ip[ipii[i]];
printf(“\n The cipher text is \n”);
for(i=0;i<8;i++)
printf(“%d,\t”,cp[i]);
getch();
}
int fn()
{
for(i=0,j=0;i<4;i++,j++)
f1[j]=ip[i];
for(i=4,j=0;i<8;i++,j++)
f2[j]=ip[i];
for(i=0;i<8;i++)
ep[i]=f2[epi[i]];
if(c==0)
{
for(i=0;i<8;i++)
{
if(k1[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
else
{
for(i=0;i<8;i++)
{
if(k2[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
r1=rs[0]*2+rs[3]*1;
c1=rs[1]*2+rs[2]*1;
r2=rs[4]*2+rs[7]*1;
c2=rs[5]*2+rs[6]*1;
s0=s00[r1][c1];
s1=s11[r2][c2];
i=0;
p[i]=s0/2;i++;
p[i]=s0%2;i++;
p[i]=s1/2;i++;
p[i]=s1%2;
for(i=0;i<4;i++)
p4[i]=p[p44[i]];
for(i=0;i<4;i++)
{
if(p4[i]==f1[i])
r[i]=0;
else
r[i]=1;
}
for(i=0;i<4;i++)
ip[i]=f2[i];
for(i=0,j=4;i<4;i++,j++)
ip[j]=r[i];
}
8. USING RSA ALGORITHM ENCRYPT A TEXT DATA AND DECRYPT THE SAME.
Aim:- To encrypt and decrypt a text RSA algorithm.
Program:-
#include
#include
char *r,en[10],de[10],b[10],g;
char a[26]={‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’I’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’};
main()
{
int q,l=0,j,cop[50],flag=0,f;
int temp[10],p,e,d,I,n,z,m,g;
unsigned long c;
clrscr();
printf(“\n Enter the plaintext at the end type”);
while((b[i]=getchar())!=’@’)
i++;
r=b;
printf(“\n Enter two prime numbers”);
scanf(“%d%d”,&p&q);
n=p*q;
z=(p-1)*(q-1);
i=0;j=0;
for(f=3;f
{
if(f%2==0)
flag=0;
else
{
cop[i]=f;
i++;j++;
}
}
printf(“\n The coprimes for %d are”,z);
for(i=0;i
printf(“%d\t”,cop[i]);
printf(“\n enter one coprime”);
scanf(“%d”,&e);
j=0;
while(*r!=’@’)
{
for(i=0;i!=25&&flag==0;i++)
{
if(*r==a[i])
{
c=pow(I,e)
g=c%n;
temp[j]=g;
flag=1;
en[j]=toupper(a[g]);
j++;
} }
r++;
flag=0; }
printf(“\n The cipher text is:”);
for(i=0;i
printf(“%c”,en[i]);
for(d=1;;d++)
{
if((e*d)%z==1)
break;
}
printf(“\n The decryption key is %d”,d);
printf(“\n Enter decryption key”);
scanf(“%d”,&d);
printf(“\n the decrypted text is:”);
for(i=0;i
{
c=pow(temp[i],d);
m=c%n;
printf(“%c”,a[m]);
}
getch();
}
0 comments:
Post a Comment