EC3401-NETWORKS AND SECURITY Lab Manual - 2021 REG
EC3401-NETWORKS AND SECURITY Lab Manual - 2021 REG
Regulation 2021
LAB MANUAL
Year / Semester: II / IV
MARCH 2024 - JUN
FEB 2023 – MAY 2023
2024
PREPARED BY
Mr.D.RAJ THILAK.,
ASSISTANT PROFESSOR /CSE
EC 3401 – NETWORKS AND SECURITY LABORATORY
OBJECTIVES:
PRACTICAL: 30 PERIODS
TABLE OF CONTENTS
PAGE
S.NO. DATE EXPERIMENT TITLE SIGN.
NO
i) Bit stuffing
Aim::
To write a c program using Implement the Data Link Layer framing methods in Bit
Stuffing.
Algorithm::
STEP-1: Read the Frame Size from the user.
STEP-2: Read the key value from the user.
STEP-3: If the size convert add the loop
values. STEP-4: Values convert the frames
STEP-5: Display the Bit Stuffing obtained above
Program::
#include<stdio.h>
#include<string.h>
int main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example:
8):"); scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
return 0;
}
Output:
Enter frame size (Example: 8):12
Enter the frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1
After Bit Stuffing :0101111101001
Result::
Thus, the above program was Successfully completed and verified.
(ii) Character stuffing
Aim::
To write a c program using Implement the Data Link Layer framing methods in Character
Stuffing.
Algorithm::
STEP-1: Read the Frame Size from the user.
STEP-2: Read the Character from the user.
STEP-3: convert the Starting and ending delimiter
STEP-4: Values convert the frames
STEP-5: Display the Character Stuffing obtained above
Program:
#include<stdio.h>
#include<string.h>
main()
{
char a[30], fs[50] = " ", t[3], sd, ed, x[3], s[3], d[3], y[3];
int i, j, p = 0, q = 0;
clrscr();
printf("Enter characters to be stuffed:");
scanf("%s", a);
printf("\nEnter a character that represents starting delimiter:");
scanf(" %c", &sd);
printf("\nEnter a character that represents ending delimiter:");
scanf(" %c", &ed);
x[0] = s[0] = s[1] = sd;
x[1] = s[2] = '\0';
y[0] = d[0] = d[1] = ed;
d[2] = y[1] = '\0';
strcat(fs, x);
for(i = 0; i < strlen(a); i++)
{
t[0] = a[i];
t[1] = '\0';
if(t[0] == sd)
strcat(fs, s);
else if(t[0] ==
ed)
strcat(fs, d);
else
strcat(fs, t);
}
strcat(fs, y);
printf("\n After stuffing:%s", fs);
getch();
}
Output:-
Enter characters to be stuffed: goodday
Enter a character that represents starting delimiter:
d Enter a character that represents ending delimiter:
g After stuffing: dggooddddayg.
Result::
Thus, the above program was Successfully completed and verified.
2. Implementation of Error Detection / Correction Techniques
Aim::
To write a c program using Implement of Error Detection / Correction Techniques in
LRC,CRC.
Algorithm::
STEP 1 :Get the data and generator polynomial.
STEP 2 :Let n be the length of the generator polynomial.
STEP 3 :Append n-1 zeros to data.
STEP 4 :Call the CRC function.
STEP 5 :
STEP 6 :If the first bit is 1, then perform a xor operation with the first n bits of data and the
generator polynomial.
STEP 7 :Shift the bits by 1 position to leave the first bit.
STEP 8 :Append a bit from the data. Repeat the process until all the bits in the data are
appended.
i) LRC,
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int l1,bit[100],count=0,i,choice;
clrscr();
printf("Enter the length of data stream: ");
scanf("%d",&l1);
printf("\nEnter the data stream ");
for(i=0;i<l1;i++)
{
scanf("%d",&bit[i]);
if(bit[i]==1)
count=count+1;
}
printf("Number of 1's are %d",count); printf("\
nEnter the choice to implement parity bit");
printf("\n1-Sender side\n2-Receiver side\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(count%2==0)
bit[l1]=0;
else
bit[l1]=1;
case 2:
if(count%2==0)
printf("There is no error in the received data stream");
else
Program::
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for (i=0;i<keylen-1;i++) {
input[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=input[i];
for (i=0;i<msglen;i++) {
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0'; else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--) {
if(temp[j]==key[j])
rem[j-1]='0'; else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for (i=0;i<msglen;i++)
printf("%c",input[i]);
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}
Output::
Enter the Number::
1
0
1
0
0
0
0
1
Enter the divisor
1
0
0
1
The quotient is 10110111 and the remainder is 0111
Result::
Thus, the above program was Successfully completed and verified.
3. Implementation of Stop and Wait, and Sliding Window Protocols
Aim::
To write a c program using Implementation of Stop and Wait, and Sliding Window
Protocols
Algorithm::
Program::
#include<stdio.h>
#include<conio.h>
void main()
{
char sender[50],receiver[50];
int i,winsize;
clrscr();
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d",&winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n");
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
gets(sender);
for(i=0;i<winsize;i++)
receiver[i]=sender[i];
receiver[i]=NULL;
printf("\n MESSAGE SEND BY THE SENDER:\n");
puts(sender);
printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
for(i=0;i<winsize;i++);
printf("\n ACK:%d",i);
printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
puts(receiver);
printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
getch();
}
Output::
Enter the windows size : 10
Sender window is expanded to store message or window
Enter the data to be sent: forgetcode.com
Message send by the sender:
forgetcode.com
Window size of receiver is expanded
Acknowledgement from receiver
Ack:5
Message received by receiver is : forgetcode
Window size of receiver is shrinked
Result::
Thus, the above program was Successfully completed and verified.
4. Implementation of Go back-N and Selective Repeat Protocols.
Aim::
To write a c program using Implementation of Go back-N and Selective Repeat Protocols
Algorithm::
STEP 1. Start.
STEP 2. Establish connection (recommended UDP)
STEP 3. Accept the window size from the client(should be <=40)
STEP 4. Accept the packets from the network layer.
STEP 5. Calculate the total frames/windows required.
STEP 6. Send the details to the client(totalpackets,totalframes.)
STEP 7. Initialise the transmit buffer.
STEP 8. Built the frame/window depending on the windowsize.
STEP 9. Transmit the frame.
STEP 10. Wait for the acknowledgement frame.
STEP 11. Close the connection.
Program::
#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:-
enter window size
8
Frame 0 has been transmitted.
Frame 1 has been transmitted.
Frame 2 has been transmitted.
Frame 3 has been transmitted.
Frame 4 has been transmitted.
Frame 5 has been transmitted.
Frame 6 has been transmitted.
Frame 7 has been transmitted.
Result::
Thus, the above program was Successfully completed and verified.
5. Implementation of Distance Vector Routing algorithm (Routing Information Protocol)
(Bellman-Ford).
Aim::
To write a c program using Implementation of Distance Vector Routing algorithm
(Routing Information Protocol) (Bellman-Ford)
Algorithm::
STEP 1. Open VI-RTSIM software from desktop
STEP 2. Click the Simulation menu bar
STEP 3. Select the “Distance – Vector Routing Algorithm” option from Routing
algorithm menu bar.
STEP Network with routers connected through link is drawn by using option in
editor(add router, join link, delete router, delete link, Add caption to link, add caption to
router)
STEP 5. Select any two nodes to find the shortest distance between them.
STEP 6:Click the Find path Button to run the program.
STEP 7. Now the shortest paths between the two nodes are calculated.
Program::
#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];//initialise the distance equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)//We choose arbitary 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, the above program was Successfully completed and verified.
6. Implementation of Link State Routing algorithm (Open Shortest Path First) with 5 nodes
(Dijkstra's).
Aim::
To write a c program using Implementation of Link State Routing algorithm (Open
Shortest Path First) with 5 nodes (Dijkstra's).
Algorithm::
STEP 1. Create a simulator object
STEP 2. Define different colors for different data flows
STEP 3. Open a nam trace file and define finish procedure then close the trace file, and execute
nam on trace file.
STEP 4. Create n number of nodes using for loop
STEP 5. Create duplex links between the nodes
STEP 6. Setup UDP Connection between n(0) and n(5)
STEP 7. Setup another UDP connection between n(1) and n(5)
STEP 8. Apply CBR Traffic over both UDP connections
STEP 9. Choose Link state routing protocol to transmit data from sender to receiver.
STEP 10. Schedule events and run the program.
Program::
#include <limits.h>
#include <stdio.h>
#define V 9
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n)
{ printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t %d\n", i, dist[i]);
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist, V);
}
int main()
{
int graph[V][V] = { { 0, 6, 0, 0, 0, 0, 0, 8, 0 },
{ 6, 0, 8, 0, 0, 0, 0, 13, 0 },
{ 0, 8, 0, 7, 0, 6, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 6, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 13, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 }
};
dijkstra(graph, 0);
return 0;
}
Output::
Result::
Thus, the above program was Successfully completed and verified.
7. Data encryption and decryption using Data Encryption Standard algorithm.
Aim::
To write a c program using Data encryption and decryption using Data Encryption
Standard algorithm.
Algorithm::
STEP-1: Read the 64-bit plain text.
STEP-2: Split it into two 32-bit blocks and store it in two different
arrays. STEP-3: Perform XOR operation between these two arrays.
STEP-4: The output obtained is stored as the second 32-bit sequence and the original second 32-
bit sequence forms the first part.
STEP-5: Thus the encrypted 64-bit cipher text is obtained in this way. Repeat the same process
for the remaining plain text characters.
Program::
#include <stdio.h>
int main()
{
int i, x;
char str[100];
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
default: printf("\nError\
n");
}
return 0;
}
Output:
#Encryption
#Decryption
Result::
Thus, the above program was Successfully completed and verified.
8. Data encryption and decryption using RSA (Rivest, Shamir and Adleman) algorithm.
Aim::
To write a c program using Data encryption and decryption using RSA (Rivest, Shamir
and Adleman) algorithm
Algorithm::
STEP-1: Select two co-prime numbers as p and q.
STEP-2: Compute n as the product of p and q.
STEP-3: Compute (p-1)*(q-1) and store it in z.
STEP-4: Select a random prime number e that is less than that of
z. STEP-5: Compute the private key, d as e * mod-1(z).
STEP-6: The cipher text is computed as messagee * mod n.
STEP-7: Decryption is done as cipherdmod n.
Program::
#include<stdio.h>
#include<math.h>
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;
//private key
//d stands for decrypt
double d;
return 0;
}
Output:
Result::
Thus, the above program was Successfully completed and verified.
9. Implement Client Server model using FTP protocol.
Aim::
To write a c program using Implement Client Server model using FTP protocol.
Algorithm::
STEP 1. The server starts and waits for filename.
STEP 2. The client sends a filename.
STEP 3. The server receives filename.
If file is present,
server starts reading file
and continues to send a buffer filled with
file contents encrypted until file-end is reached.
STEP 4. End is marked by EOF.
STEP 5. File is received as buffers until EOF is
received. Then it is decrypted.
STEP 6. If Not present, a file not found is sent.
Program::
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
return 0;
}
Output:
Result::
Thus, the above program was Successfully completed and verified.
Ex.No. : 10 (CONTENT ) RSA ALGORITHM
Date :
AIM:
To implement a RSA algorithm using HTML and Javascript.
ALGORITHM:
1. Choose two prime number p and q.
2. Compute the value of n and t.
3. Find the value of public key e.
4. Compute the value of private key d.
5. Do the encryption and decryption
a. Encryption is given as,
c = te mod n
b. Decryption
is given
as, t = cd
mod n
PROGRAM:
rsa.html
<html>
<head>
<title>RSA Encryption</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>RSA Algorithm</h1>
<h2>Implemented Using HTML & Javascript</h2>
<hr>
<table>
<tr>
<td>Enter First Prime Number:</td>
<td><input type="number" value="53" id="p"></td>
</tr>
<tr>
<td>Enter Second Prime Number:</td>
<td><input type="number" value="59" id="q"></p> </td>
</tr>
<tr>
<td>Enter the Message(cipher text):<br>[A=1, B=2,...]</td>
<td><input type="number" value="89" id="msg"></p> </td>
</tr>
<tr>
<td>Public Key:</td>
<td><p id="publickey"></p> </td>
</tr>
<tr>
<td>Exponent:</td>
<td><p id="exponent"></p> </td>
</tr>
<tr>
<td>Private Key:</td>
<td><p id="privatekey"></p></td>
</tr>
<tr>
<td>Cipher Text:</td>
<td><p id="ciphertext"></p> </td>
</tr>
<tr>
<td><button onclick="RSA();">Apply RSA</button></td>
</tr>
</table> </center>
</body>
<script type="text/javascript">
function RSA()
{
var gcd, p, q, no, n, t, e, i, x;
gcd = function (a, b) { return (!b) ? a : gcd(b, a % b); };
p = document.getElementById('p' ).value;
q = document.getElementById('q').value;
no = document.getElementById('msg').value;
n = p * q;
t = (p - 1) * (q - 1);
for (e = 2; e < t; e++)
{
if (gcd(e, t) == 1)
{
break;
}
}
for (i = 0; i < 10; i++)
{
x=1+i*t
if (x % e == 0)
{
d = x / e;
break;
}
}
ctt = Math.pow(no, e).toFixed(0);
ct = ctt % n;
dtt = Math.pow(ct, d).toFixed(0);
dt = dtt % n;
document.getElementById('publickey').innerHTML = n;
document.getElementById('exponent').innerHTML = e;
document.getElementById('privatekey').innerHTML =
d; document.getElementById('ciphertext').innerHTML =
ct;
}
</script>
</html>
OUTPUT:
RESULT:
Thus the RSA algorithm was implemented using HTML and Javascript and executed
successfully.
Ex.No 11 (CONTENT ) DIFFIE-HELLMAN KEY EXCHANGE ALGORITHM
Date :
AIM:
To implement a Diffie-Hellman Key Exchange algorithm.
ALGORITHM:
1. Sender and receiver publicly agree to use a modulus p and base g which is a
primitive root modulo p.
2. Sender chooses a secret integer x then sends Bob R1 = gx mod p
3. Receiver chooses a secret integer y, then sends Alice R2 = gy mod p
4. Sender computes k1 = Bx mod p
5. Receiver computes k2 = Ay mod p
6. Sender and Receiver now share a secret key.
PROGRAM:
import java.io.*;
import java.math.BigInteger;
class dh
{
public static void main(String[]args)throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in)); System.out.println("Enter prime
number:");
BigInteger p=new BigInteger(br.readLine());
BigInteger k1=R2.modPow(x,p);
System.out.println("Key calculated at Sender's side:"+k1);
BigInteger k2=R1.modPow(y,p);
System.out.println("Key calculated at Receiver's side:"+k2);
System.out.println("Diffie-Hellman secret key was
calculated.");
}
}
OUTPUT
C:\Security Lab New\programs>javac dh.java
RESULT:
Thus the Diffie-Hellman key exchange algorithm was implemented and executed
successfully.