Network Security Record
Network Security Record
LABORATORY
Sub Code: EC3401
BACHELOR OF ENGINEERING
IN ELECTRONICS AND COMMUNICATION
Kanchipuram-631502
P.T.Lee Chengalvaraya Naicker College of Engineering and Technology
Kanchipuram-631502
REGISTER NUMBER
…………………………………….. …………………………………
…………………………………….. …………………………………
OBJECTIVE:
i) Bit stuffing
ii) Character stuffing
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
Framing is function of Data Link Layer that is used to separate message from source or
sender to destination
Character Stuffing
o Character stuffing is also known as byte stuffing.
o In byte stuffing, special byte that is basically known as ESC (Escape Character)
that has predefined pattern is generally added to data section of the data stream or frame
when there is message or character that has same pattern as that of flag byte.
Bit Stuffing
o Bit stuffing is a process of inserting non-information bits into the data to be
transferred.
o It breaks the data sequence and helps in synchronization.
o Most protocols use the 8-bit pattern 01111110 as flag.
o In order to differentiate the message from the flag in case of same sequence, a single
bit is stuffed in the message. Whenever a 0 bit is followed by five consecutive 1bits in the
message, an extra 0 bit is stuffed at the end of the five 1s.
ALGORITHM:
BIT STUFFING
1. Start.
2. Initialize the array for transmitted stream with the special bit pattern 0111 1110 which indicates the
beginning of the frame.
4. Check for five consecutive ones and if they occur, stuff a bit 0.
5. Display the data transmitted as it appears on the data line after appending 0111 1110 at the end.
6. For de−stuffing, copy the transmitted data to another array after detecting the stuffed bits.
CHARACTER STUFFING
1. Start.
2. Initialize the array for transmitted stream.
3. Get the characters to be transmitted in to the array.
4. Get the character that represents starting and ending delimiter.
5. Display the characters after stuffing.
6. Stop.
PROGRAM:
BIT STUFFING
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int x[50], y[50], n;
int i, j, k, count = 1;
i = k;
}
}
else
{
y[j] = x[i];
}
i++;
j++;
}
//Displaying final result
printf("Result of Bit Stuffing:");
for (i = 0; i < j; i++)
{
printf("%d", y[i]);
}
getch();
}
OUTPUT:
CHARACTER STUFFING
#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;
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);
}
OUTPUT:
RESULT:
Thus the Data Link Layer framing methods have been implemented and the output was verified.
EX: 2 DATE: ERROR DETECTION / CORRECTION TECHNIQUES
RESOURCE: 1. Pentium – PC
2. C Compiler
THEORY:
Data-link layer uses some error control mechanism to ensure that frames (data bit streams) are
transmitted with certain level of accuracy. But to understand how errors is controlled, it is essential to
know what types of errors may occur.
Error detection
Error correction
Error Detection
Longitudinal redundancy check (LRC) is an error-detection method for determining the correctness of
transmitted and stored data. LRC verifies the accuracy of stored and transmitted data using parity bits.
CRC is a different approach to detect if the received frame contains valid data. This technique involves
binary division of the data bits being sent. The divisor is generated using polynomials. The sender
performs a division operation on the bits being sent and calculates the remainder. Before sending the
actual bits, the sender adds the remainder at the end of the actual bits. Actual data bits plus the
remainder is called a codeword. The sender transmits data bits as codewords.
Error Correction
Hamming code is used for the set of error-correction codes which may occur when the data is moved
from the sender to the receiver. The hamming method corrects the error by finding the state at which
the error has occurred. The hamming method uses the extra parity bits to allow the identification of a
single-bit error.
ALGORITHM:
CRC:
1. Multiply M(x) by highest power in G(x). i.e. Add so much zeros to M(x).
1. First write the bit positions starting from 1 in a binary form (1, 10, 11,100, etc.).
2. Mark all the bit positions that are powers of two as parity bits (1, 2, 4, 8, 16, 32, 64, etc.).
3. All other bit positions are for the data to be encoded using (3, 5, 6, 7, 9, 10 and 11, etc.).
PROGRAM:
CRC
#include<stdio.h>
#include<string.h>
#define N strlen(gen_poly)
char data[30];
char check_value[30];
char gen_poly[10];
int data_length,i,j;
void XOR()
void receiver()
scanf("%s", data);
printf("\n-----------------------------\n");
crc();
if(i<N-1)
printf("\nError detected\n\n");
else
void crc()
{
for(i=0;i<N;i++)
check_value[i]=data[i];
do{
if(check_value[0]=='1')
XOR();
for(j=0;j<N-1;j++)
check_value[j]=check_value[j+1];
check_value[j]=data[i++];
}while(i<=data_length+N-1);
int main()
scanf("%s",data);
scanf("%s",gen_poly);
data_length=strlen(data);
for(i=data_length;i<data_length+N-1;i++)
data[i]='0';
printf("\n----------------------------------------");
printf("\n----------------------------------------");
crc();
for(i=data_length;i<data_length+N-1;i++)
data[i]=check_value[i-data_length];
printf("\n----------------------------------------");
printf("\n----------------------------------------\n");
receiver();
return 0;
}
Output:
----------------------------------------
----------------------------------------
----------------------------------------
----------------------------------------
-----------------------------
No error detected
-----------------------------
Error detected
HAMMING CODES
#include<stdio.h>
void main() {
int data[7],rec[7],i,c1,c2,c3,c;
printf("this works for message of 4bits in size \nenter message bit one by one: ");
scanf("%d%d%d%d",&data[0],&data[1],&data[2],&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("\nthe encoded bits are given below: \n");
for (i=0;i<7;i++) {
printf("%d ",data[i]);
for (i=0;i<7;i++) {
scanf("%d",&rec[i]);
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
} else {
if(rec[7-c]==0)
rec[7-c]=1; else
rec[7-c]=0;
for (i=0;i<7;i++) {
printf("%d ",rec[i]);
}
OUTPUT:
0101101
RESULT:
Thus various error detection and correction techniques have been implemented using C
Compiler and the output was verified successfully.
Ex. 3. DATE: Go back-N and Selective Repeat Protocols
OBJECTIVE:
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
Go-Back-N protocol, also called Go-Back-N Automatic Repeat reQuest, is a data link layer protocol
that uses a sliding window method for reliable and sequential delivery of data frames. It is a case of sliding
window protocol having to send window size of N and receiving window size of 1
Go – Back – N ARQ provides for sending multiple frames before receiving the acknowledgment for the
first frame. The frames are sequentially numbered and a finite number of frames. The maximum number of
frames that can be sent depends upon the size of the sending window. If the acknowledgment of a frame is not
received within an agreed upon time period, all frames starting from that frame are retransmitted.
In the selective repeat, the sender sends several frames specified by a window size even without the need
to wait for individual acknowledgement from the receiver as in Go-Back-N ARQ. In selective repeat protocol,
the retransmitted frame is received out of sequence.
In Selective Repeat ARQ only the lost or error frames are retransmitted, whereas correct frames are
received and buffered.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
int n,r;
struct frame
char ack;
int data;
}frm[10];
int sender(void);
void recvack(void);
void resend(void);
void resend1(void);
void goback(void);
void selective(void);
int main()
int c;
do
scanf("%d",&c);
switch(c)
case 1:selective();
break;
case 2:goback();
break;
case 3:exit(0);
break;
}while(c>=4);
void goback()
sender();
recvack();
resend1();
printf("\n all packets sent successfully\n");
void selective()
sender();
recvack();
resend();
int sender()
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&frm[i].data);
frm[i].ack='y';
return 0;
void recvack()
int i;
rand();
r=rand()%n;
frm[r].ack='n';
for(i=1;i<=n;i++)
if(frm[i].ack=='n')
}
}
sleep(2);
frm[r].ack='y';
int i;
for(i=r;i<=n;i++)
sleep(2);
frm[i].ack='y';
OUTPUT:
2.Goback ARQ
3.exit
2.Goback ARQ
3.exit
resending packet 4
RESULT:
Thus Go back-N and Selective Repeat Protocols were implemented using C and the output was
verified.
Ex: 4. DATE: Distance Vector Routing
OBJECTIVE:
Obtain Routing table at each node using distance vector routing algorithm for a given subnet.
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
Distance vector routing algorithm is also called as Bellman-Ford algorithm or Ford Fulkerson
algorithm as this algorithm is used to find the shortest route from one node to another node in the
network.
The Distance Vector routing algorithm (DVR) shares the information of the routing table with
the other routers in the network and keeps the information up-to-date to select an optimal path from
source to destination.
The distance vector routing algorithm works by having each router maintain a routing table,
giving the best-known distance from source to destination and which route is used to get there.
These tables are updated by exchanging the information with the neighbor having a direct link.
Tables contain one entry for each route; this entry contains two-part, the preferred outgoing line use to
reach the destination or an estimate of the time or distance to that destination.
The router exchanges the network topology information periodically with its neighboring node
and updates the information in the routing table.
ALGORITHM:
1. Start
2. Accept the input distance matrix from the user that represents the distance between each node in the
network.
3. Store the distance between nodes in a suitable variable.
4. Calculate the minimum distance between two nodes by iterating.
If the distance between two nodes is larger than the calculated alternate available path, replace
the existing distance with the calculated distance.
5. Print the shortest path calculated.
6. Stop.
PROGRAM:
#include<stdio.h>
struct node {
unsigned dist[20];
unsigned from[20];
}rt[10];
int main() {
int dmat[20][20],n,i,j,k,count=0;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++ ) {
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j; }
do { count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j]) {
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<n;i++) {
for(j=0;j<n;j++) {
printf("\n");
}}
OUTPUT:
4210
2134
2461
2141
RESULT:
Thus the Routing table was obtained at each node using distance vector routing algorithm for
the given subnet.
Ex: 5 DATE: Link State Routing Algorithm
OBJECTIVE:
To implement Link State Routing algorithm (Open Shortest Path First) with 5 nodes
(Dijkstra's).
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
In link state routing, each router shares its knowledge of its neighborhood with every other router in the
internet work.
(i) Knowledge about Neighborhood: Instead of sending its entire routing table a router sends
info about its neighborhood only.
(ii) (ii) To all Routers: each router sends this information to every other router on the internet
work not just to its neighbor .It does so by a process called flooding.
(iii) (iii)Information sharing when there is a change: Each router sends out information about
the neighbors when there is change.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
int count,src_router,i,j,k,w,v,min;
int cost_matrix[100][100],dist[100],last[100];
int flag[100];
scanf("%d",&count);
printf("\n Enter the cost matrix values:");
for(i=0;i<count;i++)
for(j=0;j<count;j++)
printf("\n%d->%d:",i,j);
scanf("%d",&cost_matrix[i][j]);
if(cost_matrix[i][j]<0)cost_matrix[i][j]=1000;
scanf("%d",&src_router);
for(v=0;v<count;v++)
flag[v]=0;
last[v]=src_router;
dist[v]=cost_matrix[src_router][v];
flag[src_router]=1;
for(i=0;i<count;i++)
min=1000;
for(w=0;w<count;w++)
if(!flag[w])
if(dist[w]<min)
v=w;
min=dist[w];
flag[v]=1;
for(w=0;w<count;w++)
if(!flag[w])
if(min+cost_matrix[v][w]<dist[w])
dist[w]=min+cost_matrix[v][w];
last[w]=v;
for(i=0;i<count;i++)
printf("\n%d==>%d:Path taken:%d",src_router,i,i);
w=i;
while(w!=src_router)
printf("\n<--%d",last[w]);w=last[w];
}
OUTPUT:
0->0:5
0->1:2
0->2:3
0->3:4
0->4:1
1->0:2
1->1:3
1->2:1
1->3:8
1->4:4
2->0:2
2->1:1
2->2:3
2->3:5
2->4:6
3->0:1
3->1:0
3->2:1
3->3:0
3->4:3
4->0:5
4->1:1
4->2:6
4->3:2
4->4:0
2==>0:Path taken:0
<--2
<--2
<--2
<--0
<--2
RESULT:
Thus the Link State Routing algorithm (Open Shortest Path First) with 5 nodes
(Dijkstra's) was implemented and the shortest path was found.
Ex: 6 DATE: DATA ENCRYPTION AND DECRYPTION
OBJECTIVE:
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
DES is a block cipher and encrypts data in blocks of size of 64 bits each, which means 64 bits of plain text
go as the input to DES, which produces 64 bits of ciphertext.
Discarding of every 8th bit of the key produces a 56-bit key from the original 64-bit key.
DES is based on the two fundamental attributes of cryptography: substitution (also called confusion) and
transposition
In the first step, the 64-bit plain text block is handed over to an initial Permutation (IP) function.
The initial permutation is performed on plain text.
Next, the initial permutation (IP) produces two halves of the permuted block; saying Left Plain Text
(LPT) and Right Plain Text (RPT).
Now each LPT and RPT go through 16 rounds of the encryption process.
In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed on the combined
block
The result of this process produces 64-bit ciphertext.
ALGORITHM:
ENCRYPTION
The resulting value will be the encrypted format (cipher text) of the plain text.
DECRYPTION
The resulting value will be the decrypted format (original plain text) of the cipher plain text.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
int IP[] =
{
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
int E[] =
{
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1
};
int P[] =
{
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};
int FP[] =
{
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
int S1[4][16] =
{
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
};
int S2[4][16] =
{
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
};
int S3[4][16] =
{
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
};
int S4[4][16] =
{
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
};
int S5[4][16] =
{
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
};
int S6[4][16] =
{
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
};
int S7[4][16]=
{
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
};
int S8[4][16]=
{
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};
int PC1[] =
{
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
int PC2[] =
{
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
int SHIFTS[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
FILE* out;
int LEFT[17][32], RIGHT[17][32];
int IPtext[64];
int EXPtext[48];
int XORtext[48];
int X[8][6];
int X2[32];
int R[32];
int key56bit[56];
int key48bit[17][48];
int CIPHER[64];
int ENCRYPTED[64];
void expansion_function(int pos, int text)
{
for (int i = 0; i < 48; i++)
{
if (E[i] == pos + 1) {
EXPtext[i] = text;
}
}
}
if (i == 0) {
return S1[r][c];
}
else if (i == 1) {
return S2[r][c];
}
else if (i == 2) {
return S3[r][c];
}
else if (i == 3) {
return S4[r][c];
}
else if (i == 4) {
return S5[r][c];
}
else if (i == 5) {
return S6[r][c];
}
else if (i == 6) {
return S7[r][c];
}
else if (i == 7) {
return S8[r][c];
}
}
int XOR(int a, int b) {
return (a ^ b);
}
int ToBits(int value)
{
int k, j, m;
static int i;
if (i % 32 == 0) {
i = 0;
}
if (k == 0) {
fprintf(out, "0");
}
else {
fprintf(out, "1");
}
}
}
int convertCharToBit(long int n)
{
FILE* inp = fopen("input.txt", "rb");
out = fopen("bits.txt", "wb+");
char ch;
int i = n * 8;
while (i)
{
ch = fgetc(inp);
if (ch == -1) {
break;
}
i--;
convertToBinary(ch);
}
fclose(out);
fclose(inp);
}
void Encryption(long int plain[])
{
out = fopen("cipher.txt", "ab+");
for (int i = 0; i < 64; i++) {
initialPermutation(i, plain[i]);
}
for (int i = 0; i < 32; i++) {
LEFT[0][i] = IPtext[i];
}
for (int i = 32; i < 64; i++) {
RIGHT[0][i – 32] = IPtext[i];
}
for (int k = 1; k < 17; k++)
{
cipher(k, 0);
for (int i = 0; i < 32; i++)
LEFT[k][i] = RIGHT[k – 1][i];
}
for (int i = 0; i < 64; i++)
{
if (i < 32) {
CIPHER[i] = RIGHT[16][i];
}
else {
CIPHER[i] = LEFT[16][i – 32];
}
finalPermutation(i, CIPHER[i]);
}
for (int i = 0; i < 64; i++) {
fprintf(out, "%d", ENCRYPTED[i]);
}
fclose(out);
}
void Decryption(long int plain[])
{
out = fopen("decrypted.txt", "ab+");
for (int i = 0; i < 64; i++) {
initialPermutation(i, plain[i]);
}
for (int i = 0; i < 32; i++) {
LEFT[0][i] = IPtext[i];
}
for (int i = 32; i < 64; i++) {
RIGHT[0][i – 32] = IPtext[i];
}
for (int k = 1; k < 17; k++)
{
cipher(k, 1);
Thus Data encryption and decryption using Data Encryption Standard(DES) algorithm was
implemented.
Ex: 7 DATE: DATA ENCRYPTION AND DECRYPTION
OBJECTIVE:
To implement Data encryption and decryption using RSA (Rivest, Shamir and Adleman)
algorithm.
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
RSA algorithm is an asymmetric cryptography algorithm. Asymmetric actually means that it works
on two different keys i.e. Public Key and Private Key.
The Public key is used for encryption, and the Private Key is used for decryption. Decryption cannot be
done using a public key. The two keys are linked, but the private key cannot be derived from the public key.
The public key is well known, but the private key is secret and it is known only to the user who owns the key. It
means that everybody can send a message to the user using user's public key. But only the user can decrypt the
message using his private key.
o The data to be sent is encrypted by sender A using the public key of the intended receiver
o B decrypts the received cipher text using its private key, which is known only to B. B replies to A
encrypting its message using A's public key.
o A decrypts the received cipher text using its private key, which is known only to him.
ALGORITHM:
Ask the user to enter two prime numbers and validate them.
Store the prime numbers in variables.
Compute n = pq.
Compute λ(n) = (p – 1)(q – 1).
Choose a random number e as a relatively prime number to λ(n) and 1 < e < λ(n).
Compute d = e-1 mod λ(n).
Print the public and private keys.
Ask the user to enter a message and store it in a variable.
Encrypt the message using the public key.
Decrypt the message using the private key.
Print the encrypted and decrypted message.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int x, y, n, t, i, flag;
long int e[50], d[50], temp[50], j, m[50], en[50];
char msg[100];
int prime(long int);
void encryption_key();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d", &x);
flag = prime(x);
if(flag == 0)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER SECOND PRIME NUMBER\n");
scanf("%d", &y);
flag = prime(y);
if(flag == 0 || x == y)
{
printf("\nINVALID INPUT\n");
exit(0);
}
printf("\nENTER MESSAGE OR STRING TO ENCRYPT\n");
scanf("%s",msg);
for(i = 0; msg[i] != NULL; i++)
m[i] = msg[i];
n = x * y;
t = (x-1) * (y-1);
encryption_key();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i = 0; i < j-1; i++)
printf("\n%ld\t%ld", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
j = sqrt(pr);
for(i = 2; i <= j; i++)
{
if(pr % i == 0)
return 0;
}
return 1;
}
//function to generate encryption key
void encryption_key()
{
int k;
k = 0;
for(i = 2; i < t; i++)
{
if(t % i == 0)
continue;
flag = prime(i);
if(flag == 1 && i != x && i != y)
{
e[k] = i;
flag = cd(e[k]);
if(flag > 0)
{
d[k] = flag;
k++;
}
if(k == 99)
break;
}
}
}
long int cd(long int a)
{
long int k = 1;
while(1)
{
k = k + t;
if(k % a == 0)
return(k / a);
}
}
//function to encrypt the message
void encrypt()
{
long int pt, ct, key = e[0], k, len;
i = 0;
len = strlen(msg);
while(i != len)
{
pt = m[i];
pt = pt - 96;
k = 1;
for(j = 0; j < key; j++)
{
k = k * pt;
k = k % n;
}
temp[i] = k;
ct = k + 96;
en[i] = ct;
i++;
}
en[i] = -1;
printf("\n\nTHE ENCRYPTED MESSAGE IS\n");
for(i = 0; en[i] != -1; i++)
printf("%c", en[i]);
}
//function to decrypt the message
void decrypt()
{
long int pt, ct, key = d[0], k;
i = 0;
while(en[i] != -1)
{
ct = temp[i];
k = 1;
for(j = 0; j < key; j++)
{
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\n\nTHE DECRYPTED MESSAGE IS\n");
for(i = 0; m[i] != -1; i++)
printf("%c", m[i]);
printf("\n");
}
OUTPUT:
11
Haveaniceday
3 107
7 23
13 37
19 59
23 7
29 149
31 31
37 13
41 121
43 67
47 143
bsozcMqX0REWfuO2LfIJTw==
Haveaniceday
RESULT:
Thus encryption and decryption using RSA (Rivest, Shamir and Adleman) algorithm was
implemented and the output was verified.
Ex: 8 DATE: Client Server Model using FTP
OBJECTIVE:
RESOURCE:
1. Pentium – PC
2. C Compiler
THEORY:
FTP works on a client-server model. The FTP client is a program that runs on the user’s
computer to enable the user to talk to and get files from remote computers. It is a set of commands
that establishes the connection between two hosts, helps to transfer the files, and then closes the
connection.
When an FTP connection is established, there are two types of communication channels are
also established and they are known as command channel and data channel. The command channel
is used to transfer the commands and responses from client to server and server to client.
The FTP client using the URL gives the FTP command along with the FTP server address.
As soon as the server and the client get connected to the network, the user logins using User ID and
password. If the user is not registered with the server, then also he/she can access the files by using
the anonymous login where the password is the client’s email address. The server verifies the user
login and allows the client to access the files. The client transfers the desired files and exits the
connection. The figure below shows the working of FTP.
ALGORITHM:
Client
PROGRAM:
CLIENT:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#define SIZE 1024
void send_file(FILE *fp, int sockfd){
int n;
char data[SIZE] = {0};
while(fgets(data, SIZE, fp) != NULL) {
if (send(sockfd, data, sizeof(data), 0) == -1) {
perror("[-]Error in sending file.");
exit(1);
}
bzero(data, SIZE);
}
}
int main(){
char *ip = "127.0.0.1";
int port = 8080;
int e;
int sockfd;
struct sockaddr_in server_addr;
FILE *fp;
char *filename = "send.txt";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("[-]Error in socket");
exit(1);
}
printf("[+]Server socket created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
e = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(e == -1) {
perror("[-]Error in socket");
exit(1);
}
printf("[+]Connected to Server.\n");
fp = fopen(filename, "r");
if (fp == NULL) {
perror("[-]Error in reading file.");
exit(1);
}
send_file(fp, sockfd);
printf("[+]File data sent successfully.\n");
printf("[+]Closing the connection.\n");
close(sockfd);
return 0;
}
SERVER:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#define SIZE 1024
fp = fopen(filename, "w");
while (1) {
n = recv(sockfd, buffer, SIZE, 0);
if (n <= 0){
break;
return;
}
fprintf(fp, "%s", buffer);
bzero(buffer, SIZE);
}
return;
}
int main(){
char *ip = "127.0.0.1";
int port = 8080;
int e;
int sockfd, new_sock;
struct sockaddr_in server_addr, new_addr;
socklen_t addr_size;
char buffer[SIZE];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("[-]Error in socket");
exit(1);
}
printf("[+]Server socket created successfully.\n");
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
e = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(e < 0) {
perror("[-]Error in bind");
exit(1);
}
printf("[+]Binding successfull.\n");
if(listen(sockfd, 10) == 0){
printf("[+]Listening....\n");
}else{
perror("[-]Error in listening");
exit(1);
}
addr_size = sizeof(new_addr);
new_sock = accept(sockfd, (struct sockaddr*)&new_addr, &addr_size);
write_file(new_sock);
printf("[+]Data written in the file successfully.\n");
return 0;
}
RESULT:
OBJECTIVE:
To study the various NS2 commands.
THEORY :
What is NS2 ?
It is a network simulator with a package of tools that simulates behaviour of networks, create
network topologies,Log events that happen under any load, analyse events to understand the network
behaviour.
What is NS ?
It is Object-oriented, discrete event-driven network simulator, Written in C++ and Tcl languages.
Creating Topologies:
Nodes
Links
Set types of link – Simplex, duplex, wireless, satellite Set bandwidth, latency etc.Done through TCL
Scripts.
NS-2 Generic Script Structure:
2. [Turn on tracing]
3. Create topology
8. Start simulation
Step 2: Tracing
Nodes
set n0 [$ns node] set n1 [$ns node]
Add a 'finish' procedure that closes the trace file and starts nam.
Run Simulation
Schedule Events
Call ‘finish’
$ns run
NAM Interface:
Nodes:
Color
Links:
Color
$ns duplex-link-op $n0 $n1 color "green"
Label
Topology Layout:
$ns flush-trace
close $nf
$ns run
Open ‘CONTEXT’ -> Select file->new file-> write the TCL code ->
Click save -> open program files-> ns-allinone-2.33-> create a new folder -> save withfilename.tcl
RESULT :
OBJECTIVE:
Pentium – PC
NS-2
THEORY:
Star networks are one of the most common computer network topologies. In its simplest form, a star
network consists of one central switch, hub or computer, which acts as a conduit to transmit messages. This
consists of a central node, to which all other nodes are connected; this central node provides a common
connection point for all nodes through a hub. In star topology, every node (computer workstation or any other
peripheral) is connected to a central node called a hub or switch. The switch is the server and the peripherals are
the clients. Thus, the hub and leaf nodes, and the transmission lines between them, form a graph with the
topology of a star. If the central node is passive, the originating node must be able to tolerate the reception of
an echo of its own transmission, delayed by the two-way transmission time (i.e. to and from the central node)
plus any delay generated in the central node. An active star network has an active central node that usually has
the means to prevent echo-related problems.
The star topology reduces the damage caused by line failure by connecting all of the systems to a central
node. When applied to a bus-based network, this central hub rebroadcasts all transmissions received from any
peripheral node to all peripheral nodes on the network, sometimes including the originating node. All peripheral
nodes may thus communicate with all others by transmitting to, and receiving from, the central node only. The
failure of a transmission line linking any peripheral node to the central node will result in the isolation of that
peripheral node from all others, but the rest of the systems will be unaffected.
ALGORITHM:
OUTPUT:
RESULT:
OBJECTIVE:
1. Pentium – PC
2. NS-2
THEORY:
Bus topology definition is, this is one of the simplest physical topology used for the network.
This topology is famously used for the Local Area Network. In this topology, all the nodes are
connected through a single cable known as ‘Backbone’. If this Backbone cable is damaged the entire
network breakdowns.
The node that transmits data is known as Host. All the computers connected in the network
will receive all the network traffic. Each node is given equal priority for data transmission. The nodes
use Media Access Technology such as a bus master to share the bus.
ALGORITHM:
#Create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4 $n5" 0.5Mb 80ms LL Queue/DropTail MAC/Csma/Cd
Channel]
#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3
set sink0 [new Agent/TCPSink]
$ns attach-agent $n3 $sink0
OUTPUT:
RESULT:
OBJECTIVE:
1. Pentium – PC
2. NS-2
THEORY:
In a ring network, packets of data travel from one device to the next until they reach their
destination. Most ring topologies allow packets to travel only in one direction, called
a unidirectional ring network. Others permit data to move in either direction, called bidirectional.
ALGORITHM:
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &exit 0
}
OUTPUT:
RESULT:
OBJECTIVE:
To implement and perform the operation of CSMA/CD and CSMA/CA using NS2.
1. Pentium – PC
2. NS-2
THEORY:
Ethernet is a LAN (Local area Network) protocol operating at the MAC (Medium Access Control) layer.
Ethernet has been standardized as per IEEE 802.3. The underlying protocol in Ethernet is known as the CSMA
/ CD – Carrier Sense Multiple Access / Collision Detection. The working of the Ethernet protocol is as explained
below, a node which has data to transmit senses the channel. If the channel is idle then, the data is transmitted.
If the channel is busy then, the station defers transmission until the channel is sensed to be idle and then
immediately transmitted. If more than one node starts data transmission at the same time, the data collides.
This collision is heard by the transmitting nodes which enter into contention phase. The contending nodes
resolve contention using an algorithm called truncated binary exponential back off.
ALGORITHM:
PROGRAM:
CSMA/CA
OUTPUT:
CSMA/CD
OUTPUT:
RESULT:
Thus CSMA/CD and CSMA/CA was implemented and its operation was performed.
EX: 12. DATE: STOP N WAIT PROTOCOL
OBJECTIVE:
To write a TCL simulation script to generate a simple 2 node network with a stop-n-wait protocol on
lossless and lossy TCP and UDP connections.
1. Pentium – PC
2. NS-2
THEORY:
In stop-n-wait protocol the sender sends frame 0 and then waits for the acknowledgement before
proceeding for thenext frame. The receiver sends the acknowledgment 1 (ACK1) and so on.
ALGORITHM:
Step 6: Add traffic [set traffic parameters & insert error mode]
PROGRAM:
$ns trace-all $f
$ns at 3.0 "$ns detach-agent $n0 $tcp; $ns detach-agent $n1 $sink"
$ns at 0.0 "$ns trace-annotate \"stop and wait with Packet Loss\""
proc finish {}
global ns nf
$ns run
OUTPUT :
RESULT:
Thus TCL simulation script to generate a simple network with stop-n-wait protocol on lossless and lossy
TCP andUDP connection is done.