Sei sulla pagina 1di 17

COMPUTER NETWORKS LAB MANUAL

Program1: Implement the data link layer framing methods such as character stuffing and bit stuffing.
AIM: Write program that implements character stuffing & bit stuffing
Code:
/* PROGRAM FOR CHARACTER 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 DLE ");
for(j=0;j<l;j++)
{
t=j;
if(os[t++]=='d'&&os[t++]=='l'&&os[t]=='e')
{
printf(" DLE dle ");
j=j+3;
}
else
printf("%c",os[j]);
}
printf(" ETX DLE");
getch();
}
Input:
Enter the text: a dle bc
Output: STX DLE a DLE dle bc DLE ETX
/* IMPEMENT THE DATALINK LAYER FRAMING METHODS SUCH AS BIT STUFFING.*/
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<n;i++)
scanf("%d",&os[i]);
for(j=0;j<8;j++)
{
ns[k]=s[j];
k++;
}
for(j=0;j<n;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]=1;
k++;
}
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<k;j++)
printf("%d",ns[j]);
getch();
}

Input:
enter size of the string: 12
Enter the string<max 12>: 100111111011
Output:
The stuffed data is: 01111110100111110101101111110

Program2: Implement on a data set of characters the three CRC polynomials- CRC 12, CRC 16 and
CRC CCIP.
Aim: Write a program to implement CRC Polynomials
Code:
/* IMPLEMENT ON A DATA SET OF CHARECTERS THE CRC POLYNOMIALS-CRC12, CRC16 AND CCIP. */
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<e;i++)
scanf(%d,&b[i]);
printf(\n enter the length of polynomial ::);
scanf(%d,&f);
printf(\n the polynomial ::);
for(i=0;i<f;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<n;i++)

printf(%d,a[i]);
p++;
printf(\n crc division on sender side);
else
{ printf(\n enter the remainder ::);
for(i=f;i<n;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<n;i++)
{
a[i]=d[i];
printf(%d,a[i]);
} }
for(q=0;q<f;q++)
{
for(k=0;j=c;j<e+c;k++,j++)
{
if(a[j]!=b[k])
a[j]=1;
else
a[j]=0;
}
j=c;
for(j=c;j<e+c;j++)
{
if(a[j]==0)
c++;
else
break;
}
q=c;
}
printf(\n the remainder is ::);
co=0;
for(i=f;f<n;i++)
{
printf(%d,a[i]);
if(a[i]==1)
co++;
}
return co;
}

Input:
Enter your choice: CRC-12(12),CRC-16(16),CRC-CCITT(16): 12
Enter the divisor:
CRC12: 1100000001111
CRC16: 11000000000000101
CRC CCITT: 10001000000100001
110000001111
Enter number of bits :4
Enter message of 4 bits: 1 1 0 1\
Output:
Divisor:1100000001111
Dividend:1101000000000000
Remainder:100001110111
Resultant string is: 1101100001110111
Program3: Implement Dijkstras algorithm to compute the shortest path through a graph
Aim: Write a program to implement Dijkstras algorithm to find shortest path through a graph
Code:
/* PROGRAM FOR DIJIKSTRA'S ALGORITHM */
#include<stdio.h>
#define INFINITY 5000
int n,dist[10][10],path[10];
struct state
{
int predecessor;
int length;
enum {permanent,tentative} label;
}state[10];
struct state *p;
main()
{
int s,t,i,j;
clrscr();
printf("Enter the no.of vertices");
scanf("%d",&n);
printf("Enter the adjacency matrix");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&dist[i][j]);

printf("Enter the source vertex and destination vertex");


scanf("%d%d",&s,&t);
shortest_path(s,t);
getch();
}
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->label=tentative;
}
state[t].length=0;
state[t].label=permanent;
k=t;
do{
for(i=0;i<n;i++)
if(dist[k][i]!=0 && state[i].label==tentative){
if(state[k].length+dist[k][i]<state[i].length){
state[i].predecessor=k;
state[i].length=state[k].length+dist[k][i];
}
}
k=0;
min=INFINITY;
for(i=0;i<n;i++)
if(state[i].label==tentative && state[i].length<min){
min=state[i].length;
k=i;
}
state[k].label=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("\nThe shortest distance is %d",state[s].length);


}
Input:
Enter number of vertices: 8
Enter adjacency matrix:
02000060
20702000
07030300
00300002
02000210
00302002
60001004
00020240
Enter source vertex & destination vertex: 0 3
Output:
The shortest path is: 0 1 4 5 7 3
Shortest path length: 10
Program4: Take an example subnet graph with weights indicating delay between nodes. Now obtain
routing table art each node using distance vector routing algorithm
Aim: Write a program to obtain routing table art using distance vector routing algorithm.
Code:
/* PROGRAM FOR SUBNET */
#include<stdio.h>
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; /*expected j delays*/
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("\nThe delays from j are :");
for(i=0;i<4;i++)
printf("\t %d",ejd[i]);
printf("\nThe new routing table for j is:");

for(i=0;i<12;i++)
{
if(i==9)/* J position */
printf("\nj-->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(j=0;j<4;j++)
a[j]=b[j];
for(k=0;k<4;k++)
for(j=k+1;j<4;j++)
{
if(b[k]<b[j])
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();
}
Output:
0
24
12
36
25
18
40
27
14
7
23
20

20
31
19
8
30
19

21
28
36
24
22
40

18
17
21
9
24
29

31
20
0
11
22
33

6
0
14
7
22
9

31
19
22
10
0
9

The delays are: 8

10

JA
JB
JC
J D
J E
JF
J G
J H
JI
JJ
JK
JL

A
A
I
H
H
I
H
H
I
K
K

8
20
28
20
17
30
18
12
10
0
6
15

12

Program 5: Take an example subnet of hosts .Obtain broadcast tree for it.
Aim: Write a program to obtain broadcast tree for a subnet of hosts
Code:
/* PROGRAM FOR BROAD CAST ROUTING ROUTING (OR) DISTANCE VECTOR ROUTING */
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();
/*
printf("\n enter nodes :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d node connectivity:",i);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}*/
n=4;
printf("\n broadcast routing edges are: \n");
for(i=0;i<n;i++)

{
k=0;
for(j=0;j<n;j++)
{
b[k]=a[i][j];
k++;
}
for(k=0;k<n;k++)
for(j=k+1;j<n;j++)
{
if(b[k]<b[j])
flag=0;
else
{
temp=b[j];
b[j]=b[k];
b[k]=temp;
}
}
for(j=0;j<n;j++)
{
if(b[0]==a[i][j])
{
if(i<j||l==1)
{
printf("\n %d-->%d \t %d",i,j,a[i][j]);
a[i][j]=a[j][i]=99;
}
else
l=1;
}
}
}
getch();
}
Input:
Enter number of nodes:
6
Enter the adjacency matrix:
1
2
3
4

1
2
3

0
0
15

0
0
0

0
28
0

28
0
30

0
30
0

0
17
0

4
0
17
0
0
5
0
0
15
0
6
10
0
0
14
Output:
Broadcast tree of Network is:
16
w:10
65
w:12
64
w:14
53
w:15
42
w:17

0
0
12

14
12
0

Program6: Take a plain text and encrypt the same using DES algorithm.
Aim: Write a program that take 8 bit plaintext and encrypt using DES algorithm
Code:
/* PROGRAM FOR DES ALGORITHM */
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 pl[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",&pl[i]);
printf("\n enter the 10-bit key:");
scanf("%d",&k[i]);
for(i=0;i<8;i++)
ip[i]=pl[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];
}
Input:
Enter 8 bit plain text: 1000 1000
Enter the 10 bit key : 10100 10100
Output:
K1:10001101
K2:11100001
The cipher text is: 01011000
The plain text is: 10001000
Program7: Write a program to break the above DES Coding
Aim: Implement a program that breaks DES coding
Code:
/* PROGRAM FOR DES BREAKING ALGORITHM*/
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 pl[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",&pl[i]);

printf("\n enter the 10-bit key:");


scanf("%d",&k[i]);
for(i=0;i<8;i++)
ip[i]=pl[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(k2[i]==ep[i])
rs[i]=0;
else
rs[i]=1;
}
}
else
{
for(i=0;i<8;i++)
{
if(k1[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];
}
Program 8: Using RSA algorithm encrypt a text data and decrypt the same.
Aim: Write a program to encrypt & decrypt a text data using RSA algorithm
Code:
/* PROGRAM FOR RSA ENCRYPTION AND DECRYPTION ALGORITHM */
#include<stdio.h>
#include<math.h>
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[l]=getchar())!='@')
l++;
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<z;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<j;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<=j;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<j;i++)
{
c=pow(temp[i],d);
m=c%n;
printf("%c",a[m]);
}
getch();
}
Input:
Enter two prime numbers: 3 5
Enter plain text: sreedhar
Public key:3 15
Output:
Cipher text: 4 12 5
5
4
The plain text is:dceedhac

12

Potrebbero piacerti anche