CNS Lab Manual For End Sem Exam

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

VELAMMAL ENGINEERING COLLEGE

(An Autonomous Institution)


Chennai-600 066.

DEPARTMENT OF ELECTRONICS AND


COMMUNICATION ENGINEERING

Communication Network Security Lab


INDEX

EXP.NO DATE EXPERIMENT NAME

1 Implement the Cyclic Redundancy Check ,error detection technique in C

2 Implement Bit stuffing (High level data link control)

3 Implement Distance Vector Routing algorithm

4 Implement Link state routing algorithm(OSPF)

5 Implementation of Caesar cipher using C Program

6 Write a C program to display the class of the given IP address

7 Implement Go Back-n ARQ protocol

8 Implement Selective Repeat protocol

9 Token Ring Topology

10 Token Bus Topology

11 Implementation of RSA algorithm using C Program


DATE:

EXPT. NO. 1 CYCLIC REDUNDANCY CHECK

AIM: Write a C program to implement the Cyclic Redundancy Check, error detection technique.

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int crc[10],i,j;
int s=0;
int data[20],divisor[10],data_size,div_size;
printf("\t\t.......CRC............\n");
printf("\n enter the data size:");
scanf("%d",&data_size);
printf("\n enter the data:");
for(i=0;i<data_size;i++)
scanf("%d",&data[i]);
printf("\n enter the value divisor size:");
scanf("%d",&div_size);
printf("\n enter the divisor:");
for(i=0;i<div_size;i++)
scanf("%d",&divisor[i]);
for(i=0;i<div_size-1;i++)
data[data_size+i]=0;
for(i=0;i<div_size;i++)
crc[i]=data[i];
for(i=0;i<data_size;i++)
{
if(crc[0]==1)
{
for(j=0;j<div_size;j++)
{
if(crc[j]==divisor[j])
crc[j]=0;
else
crc[j]=1;
}
}
for(j=0;j<div_size;j++)
crc[j]=crc[j+1];
crc[div_size-1]=data[div_size+i];
}
printf("\n crc codes:");
for(i=0;i<div_size-1;i++)
printf("%d",crc[i]);
for(i=0;i<div_size-1;i++)
data[data_size+i]=crc[i];
printf("\n\n enter the dta size:");
scanf("%d",&data_size);
printf("\nenter the data");
for(i=0;i<data_size;i++)
scanf("%d",&data[i]);
printf("\n enter the divisor size:");
scanf("%d",&div_size);
printf("\n enter the
divisor(same):");
for(i=0;i<=div_size-1;i++)
scanf("%d",&divisor[i]);
for(i=0;i<div_size-1;i++)
data[data_size+i]=0;
for(i=0;i<data_size;i++)
crc[i]=data[i];
for(i=0;i<data_size;i++)
{
if(crc[0]==1)
{
for(j=0;j<div_size;j++)
{
if(crc[j]==divisor[j])
crc[j]=0;
else
crc[j]=1;
}
}
}
for(j=0;j<div_size-1;j++)
{crc[j]=crc[j+1];
crc[div_size-1]=data[div_size+i];
}
printf("\n the remainder is:");
for(i=0;i<div_size-1;i++)
{
printf("%d",crc[i]);
s=s+crc[i];
}
if(s==0)
printf("\n no error");
else
printf("\n error in your value");
getch();
return 0;
}
OUTPUT:

RESULT:

Thus, cyclic redundancy code error detection technique has been implemented successfully.
DATE:

EXPT. NO. 2 BIT STUFFING

AIM: Implement data link layer framing methods that uses bit stuffing

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:
#include "stdio.h"
#include "conio.h"
int main()
{
int i=0,count=0;
char a[100];
printf("enter the bits : ");
scanf("%s",a);

printf("\nAfter bit stuffing \n");


printf("01111110");

for(i=0;a[i]; i++)
{
if(a[i]=='1')
count++;
else
count=0;
printf("%c",a[i]);
if(count==5)
{
printf("0");
count=0;
}
}
printf("01111110");
getch();
return 1;
}
OUTPUT:

RESULT:

Thus, data link layer framing method that uses bit stuffing has been implemented successfully.
DATE:

EXPT. NO. 3 DISTANCE VECTOR ALGORITHM

AIM: Implementation of Distance vector algorithm using C program

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:

/*
Distance Vector Routing in this program is implemented using Bellman Ford Algorithm:-
*/
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,I,j,k,count=0;
printf(“\nEnter the number of nodes : “);
scanf(“%d”,&nodes);//Enter the nodes
printf(“\nEnter the cost matrix :\n”);
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf(“%d”,&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//10rbitrary10 the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose 10rbitrary vertex k and we calculate the direct distance from the node I
to k using the cost matrix
//and add the distance from k to node j
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculate the minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0); for(i=0;i<nodes;i+
+)
{
printf(“\n\n For router %d\n”,i+1);
for(j=0;j<nodes;j++)
{
printf(“\t\nnode %d via %d Distance %d “,j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf(“\n\n”);
getch();
}

OUTPUT:

RESULT:

Thus, Distance vector algorithm using C program has been implemented successfully.
DATE:

EXPT. NO. 4 Open Shortest Path First(OSPF)

AIM: Implement an interior routing protocol based on link state routing to find the shortest path using C

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10

void dijikstra(int G[MAX][MAX], int n, int startnode);

void main(){
int G[MAX][MAX], i, j, n, u;
clrscr();
printf("\nEnter the no. of vertices:: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix::\n");
for(i=0;i < n;i++)
for(j=0;j < n;j++)
scanf("%d", &G[i][j]);
printf("\nEnter the starting node:: ");
scanf("%d", &u);
dijikstra(G,n,u);
getch();
}

void dijikstra(int G[MAX][MAX], int n, int startnode)


{
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i,j;
for(i=0;i < n;i++)
for(j=0;j < n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];

for(i=0;i< n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count < n-1){
mindistance=INFINITY;
for(i=0;i < n;i++)
if(distance[i] < mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i < n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i] < distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}

for(i=0;i < n;i++)


if(i!=startnode)
{
printf("\nDistance of %d = %d", i, distance[i]); printf("\
nPath = %d", i);
j=i;
do
{
j=pred[j];
printf(" <-%d", j);
}
while(j!=startnode);
}
}
OUTPUT:

RESULT:

Thus, an interior routing protocol based on link state routing to find the shortest path using C has been
implemented successfully.
EXP No:5 IMPLEMENTATION OF CAESAR CIPHER USING C PROGRAM

AIM: To Implement Caesar cipher using C Program

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM
#include <stdio.h>
int main()
{
int i, x;
char str[100];
printf("\nPlease enter a string:\t");
gets(str);

printf("\nPlease choose following options:\n");


printf("1 = Encrypt the string.\n");
printf("2 = Decrypt the string.\n");
scanf("%d", &x);

//using switch case statements


switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 3; //the key for encryption is 3 that is added to ASCII value

printf("\nEncrypted string: %s\n", str);


break;

case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 3; //the key for encryption is 3 that is subtracted to ASCII value

printf("\nDecrypted string: %s\n", str);


break;
default:
printf("\nError\n");
}
return 0;
}
OUTPUT:

For K=3
Plain text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher text : DEFGHIJKLMNOPQRSTUVWXYZABC

Encryption
Plain Text : HELLO
Cipher text: LHOOR

Decryption:
Cipher text: LHOOR
Plain Text : HELLO

RESULT

Thus the program to implement Ceaser cipher was written in C language and verified.
DATE:

EXPT. NO. 6 IP ADDRESS CONFIGURATION

AIM: Write a C program to print the class of the IP address given as input

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:

#include <stdio.h>
#include <string.h>
/*
Function : extractIpAddress Arguments :
sourceString - String pointer that contains ip address
ipAddress - Target variable short type array pointer that will store ip address octets
*/

void extractIpAddress(unsigned char *sourceString,short *ipAddress)


{

unsigned short len=0;


unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5];
len=strlen(sourceString);
for(i=0;i<len;i++)
{
if(sourceString[i]!='.')
{ buf[cnt++]
=sourceString[i];
}
if(sourceString[i]=='.' || i==len-1)
{ buf[cnt]='\0';
cnt=0; oct[cnt1+
+]=atoi(buf);
}

}
ipAddress[0]=oct[0];
ipAddress[1]=oct[1];
ipAddress[2]=oct[2];
ipAddress[3]=oct[3];
}

int main()
{
unsigned char ip[20]={0};
short ipAddress[4];
printf("Enter IP Address (xxx.xxx.xxx.xxx format): ");
scanf("%s",ip);
extractIpAddress(ip,&ipAddress[0]);
printf("\nIp Address: %03d. %03d. %03d. %03d\n",ipAddress[0],ipAddress[1],ipAddress[2],ipAddress[3]);
if(ipAddress[0]>=0 && ipAddress[0]<=127)
printf("Class A Ip Address.\n");
if(ipAddress[0]>127 &&
ipAddress[0]<191)
printf("Class B Ip Address.\n");
if(ipAddress[0]>191 &&
ipAddress[0]<224)
printf("Class C Ip Address.\n");
if(ipAddress[0]>224 && ipAddress[0]<=239)
printf("Class D Ip Address.\n");
if(ipAddress[0]>239)
printf("Class E Ip Address.\n");

return 0;
}
OUTPUT:

RESULT:

Thus, a C program to print the class of the IP address given as input has been implemented successfully.
DATE:

EXPT. NO. 7 GO BACK N PROTOCOL

AIM: Implement Go Back-n sliding window protocol using c

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:

#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);

if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
OUTPUT:

RESULT:

Thus, Go Back-n sliding window protocol using C has been implemented successfully.
DATE:

EXPT. NO. 8 SELECTIVE REPEAT PROTOCOL

AIM: Implement selective repeat sliding window protocol in c

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program
 Observe and verify the output.

PROGRAM CODE:

#include<stdio.h>
int main()
{
int windowsize,sent=0,nack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}

printf("\nNEGATIVE ACK RECEIVED\n");


scanf("%d",&nack);

printf("Frame %d has been transmitted.\n",nack);


break;
}
return 0;
}
OUTPUT:

RESULT:

Thus, selective repeat sliding window protocol in c has been implemented successfully.
DATE:

EXPT. NO. 9 TOKEN RING TOPOLOGY

AIM: Configure a Token ring topology with 4 nodes using LAN trainer kit.
Apparatus Required:
 A PC with a protocol monitoring software
 3 microcontrollers with embedded software
 RS-232 cables

STEPS:
1) The aim is to create the Token ring topology.
2) To perform the experiment follow the below steps
3) Keep the relay switch to the left
4) Token ring set-up consists of two cable one to the PC another to the embedded node
5) Connect the cable labeled “LAN-Token Ring” to the 3 embedded nodes
6) Connect the cable labeled “ LAN-token Ring PC to the PC comm. Port
7) Connect the other end of PC cable labeled “ LAN-token ring to start node” to the cable labeled “ LAN-token
ring start node” on the embedded node side
8) Connect the other of PC cable labeled “LAN-Token ring end node” to the cable labled “LAN-token ring end
node” on the embedded node side.

Experimental Setup

Embedded Network
Node 1 Monitor
2

Network

Embedded
Embedded
Node 4
Node 3
OUTPUT:
The Token ring topology was setup, data transmission and reception was observed across the topology.

RESULT:

Thus, a token ring topology with 4 nodes has been configured.


DATE:

EXPT. NO. 10 TOKEN BUS TOPOLOGY

AIM: Configure a Token Bus topology with 4 nodes using LAN trainer kit.
Apparatus Required:
 A PC with a protocol monitoring software
 3 microcontrollers with embedded software
 PC Interface Card
 Common Channel-BUS
 RS-232 cables

STEPS:
On the Network Monitor:
1. Do the physical connection as shown below
2. Check the jumper J6 for RS485 mode
3. Connect RS232 LAN S cable from PC to the Converter Card
4. Connect RS485-LAN Bus Cable to the converter card
5. Connect the other ends of the Bus to connector J6 on the embedded nodes
6. Run the LAN trainer software
On the Embedded Node:
1. Connect all the 3 boards and a PC Interface card to the respective power supplies
2. Switch on the power supply to all the nodes and the interface card
3. After self test, press “Enter “ key followed by “waiting for SCF” message

Experimental Setup

Network Monitor
Node 2
Node 4

Converter
Card

Node 1 Node 3
OUTPUT:
The Token bus topology was setup, data transmission and reception was observed across the topology.

RESULT:

Thus, a token bus topology with 4 nodes has been configured.


EXP No.11 Implementation of RSA algorithm using C Program

AIM: To Implement Data Encryption Standard(DES) algorithm using C Program

SOFTWARE REQUIRED:
1. PENTIUM –PC
2.C COMPLIER
3. TURBO C

PROCEDURE
 Open Turbo C++ Initial Screen
 Start the Turbo C++ Compiler
 Create a New Source File (prog.C)
 Compile the Program
o Go to Compile tab of Menu bar and press Compile or Build All.
 Execute the Program

PROGRAM CODE:
#include<stdio.h>
#include<math.h>
 
//to find gcd
int gcd(int a, int h)
{
    int temp;
    while(1)
    {
        temp = a%h;
        if(temp==0)
        return h;
        a = h;
        h = temp;
    }
}
 
int main()
{
    //2 random prime numbers
    double p = 3;
    double q = 7;
    double n=p*q;
    double count;
    double totient = (p-1)*(q-1);
 
    //public key
    //e stands for encrypt
    double e=2;
 
    //for checking co-prime which satisfies e>1
    while(e<totient){
    count = gcd(e,totient);
    if(count==1)
        break;
    else
        e++;
    }
 
    //private key
    //d stands for decrypt
    double d;
 
    //k can be any arbitrary value
    double k = 2;
 
    //choosing d such that it satisfies d*e = 1 + k * totient
    d = (1 + (k*totient))/e;
    double msg = 12;
    double c = pow(msg,e);
    double m = pow(c,d);
    c=fmod(c,n);
    m=fmod(m,n);
 
    printf("Message data = %lf",msg);
    printf("\np = %lf",p);
    printf("\nq = %lf",q);
    printf("\nn = pq = %lf",n);
    printf("\ntotient = %lf",totient);
    printf("\ne = %lf",e);
    printf("\nd = %lf",d);
    printf("\nEncrypted data = %lf",c);
    printf("\nOriginal Message Sent = %lf",m);
 
    return 0;
}

RESULT:
Thus the RSA algorithm is implemented using C.

You might also like