[go: up one dir, main page]

0% found this document useful (0 votes)
52 views91 pages

Networks and Security Lab Manual

Uploaded by

kayalvizhi.1285
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views91 pages

Networks and Security Lab Manual

Uploaded by

kayalvizhi.1285
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

VAIGAI COLLEGE OF ENGINEERING

Approved by AICTE, New Delhi ,

Therkutheru, Melur.

DEPARTMENT OF CSE

EC3401 – NETWORKS AND SECURITY LAB

RECORD

STUDENT NAME:

REGISTER.NO:

YEAR/SEM:
VAIGAI COLLEGE OF ENGINEERING
MADURAI – 625 122
https://vaigai.org/

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING


BONAFIDE CERTIFICATE

This is to certify that this Practical work titled


is

the bonafide work of

Register Number _ during the year 20 – 20 .

Faculty Incharge Head of the Department

Submitted for the Anna University Practical Examination held on


at Vaigai College of Engineering, Madurai - 625 122.

Internal Examiner External Examiner


EC3401 NETWORKS AND SECURITY LAB

PRACTICAL EXERCISES: 30 PERIODS


Experiments using C
1. Implement the Data Link Layer framing methods,
i) Bit stuffing, (ii) Character stuffing
2. Implementation of Error Detection / Correction Techniques
i) LRC, (ii) CRC, (iii) Hamming code
3. Implementation of Stop and Wait, and Sliding Window Protocols
4. Implementation of Go back-N and Selective Repeat Protocols.
5. Implementation of Distance Vector Routing algorithm (Routing Information Protocol)(Bellman-
Ford).
6. Implementation of Link State Routing algorithm (Open Shortest Path First) with 5 nodes(Dijkstra's).
7. Data encryption and decryption using Data Encryption Standard algorithm.
8. Data encryption and decryption using RSA (Rivest, Shamir and Adleman) algorithm.
9. Implement Client Server model using FTP protocol.

Experiments using Tool Command Language


1. Implement and realize the Network Topology - Star, Bus and Ring using NS2.
2. Implement and perform the operation of CSMA/CD and CSMA/CA using NS2.
INDEX
S.NO. DATE TITLE OF THE EXPERIMENT PAGE MARK SIGNATURE
S.NO. DATE TITLE OF THE EXPERIMENT PAGE MARK SIGNATURE
Implementation of Data Link Layer Methods Bit Stuffing

AIM:-
To implement the data link layer of bit stuffing.

Software Required:-
Turbo C-programming.

Algorithm:-

STEP-1 - Start.

STEP-2 - Initialize the array for transmitted stream with the special bit
pattern 0111 1110 which indicates the beginning of the frame.

STEP-3 - Get the bit stream to be transmitted in to the array.

STEP-4 - Check for five consecutive ones and if they occur, stuff a bit 0.

STEP-5 - Display the data transmitted as it appears on the data line after
appending 0111 1110 at the end.

STEP-6 - For de−stuffing, copy the transmitted data to another array


after detecting the stuffed bits.

STEP-7 - Display the received bit

stream. STEP-8 - Stop.

1
PROCEDURE:-

Bit stuffing refers to the insertion of one or more bits into a data
transmission as a way to provide signaling information to a receiver. The
receiver knows how to detect, remove or disregard the stuffed bits.Bit stuffing
is a process of inserting an extra bit as 0, once the frame sequence encountered
5 consecutive 1's.During the traversal of the array, 5 consecutive 1’s are
encountered after the 4th index of the given array. Hence, a zero bit has been
inserted into the stuffed array after the 4th index.If arr[i] is 1 then check for
the next 4 bits if they are set bits as well. If they are, then insert a 0 bit after
inserting all the 5 set bits into the array brr[].Otherwise, insert the value
of arr[i] into the array brr[].

PROGRAM:-

#include<stdio.h>
#include<string.h>
int main()
{
int x[20],y[30],i,j,k,count,n;
printf("Enter frame size: ");
scanf("%d",&n);
printf("Enter the frame in the form of 0 and 1: ");
for(i=0; i<n; i++)
scanf("%d",&x[i]);
i=0;
count=1; j=0;
while(i<n)
{
if(x[i]==1)
{
y[j]=x[i];
for(k=i+1; x[k]==1 && k<5; k++)
{
j++;

2
y[j]=x[k];
count++;
if(count==5)
{
j++;
y[j]=0
;
}
i=k;
}
}
else
{
y[j]=x[i];
}
i++
;
j++
;
}
printf("After Bit Stuffing: ");
for(i=0; i<j; i++)
printf("%d",y[i]);
return 0;
}

3
OUTPUT:-
Enter frame size: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

4
Implement The Data Link Layer Of Character Stuffing

AIM:-
To implement the data link layer of character stuffing.

Software Required:-
Turbo C-programming.

Algorithm:-

STEP-1 - Start.

STEP-2 - Append DLE STX at the beginning of the string.

STEP-3 - Check the data if character is present; if character DLE is present


in the string (example DOODLE) insert another DLE in the string (ex:
DOODLEDLE)

STEP-4 - Transmit DLE ETX at the end of the

string. STEP-5 - Display the string.

STEP-6 - Stop.

Procedure:-
The character stuffing method gets around the problem of re
synchronization after an error by having each frame start and end with special
bytes. Character Stuffing / Byte Stuffing: Character stuffing or byte stuffing is
which an escape byte (ESC) is stuffed character stream before a flag byte in the
data.Since this can interfere with the framing, a technique called character
stuffing is used. The sender's data link layer inserts an ASCII DLE character just
before the DLE character in the data. The receiver's data link layer removes this
DLE before this data is given to the network layer. However character stuffing is
closely associated with 8-bit characters and this is a major hurdle in transmitting
arbitrary sized character.

5
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();
}

6
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 implementation of data link layer of bit stuffing and
character stuffing was verified sucessfully.

7
Implementation Of Error Detection & Correction CRC

AIM:-
To implement the error correction and detection by using the CRC technique.

Software Reqired:-
Turbo C-Programming.

Algorithm:-
STEP- 1 - Start.

STEP-2 - Load a 16-bit register with FFFF hex (all 1's). Call this the CRC
register.

STEP-3 - Exclusive OR the first eight-bit byte of the message with the low
order byte of the 16-bit CRC register.

STEP-4 - Shift the CRC register one bit to the right (toward the LSB),
zerofilling the MSB. Extract and examine the LSB.

STEP-5 - If the LSB is 0, repeat Step 3 (another shift). If the LSB is 1,


Exclusive OR the CRC register with the polynomial value A001 hex (1010 0000
0000 0001)

STEP-6 -Repeat Steps 3 and 4 until eight shifts have been performed.
When this is done, a complete eight-bit byte will have been processed..

STEP-7 - The final contents of the CRC register is the CRC

value STEP-8 - Stop.


Procedure:-
The Cyclical Redundancy Check (CRC) field is two bytes, containing a 16-
bit binary value. The CRC value is calculated by the transmitting device, which
appends the CRC to the message. The receiving device recalculates a CRC
during receipt of the message, and compares the calculated value to the actual
value it received in the CRC field. If the two values are not equal, an error
results.The CRC is started by first preloading a 16-bit register to all 1's. Then a
process begins of applying successive eight-bit bytes of the message to the
current contents of the register. Only the eight bits of data in each character are
used for generating the CRC. Start and stop bits, and the parity bit, do not
apply tothe CRC.

Program:-
#include<stdio.h>
char text[100];
char key[100];
char rem[100];
void crc()
{
int i,j;
int keylen,textlen;
char temp[100];
strcpy(temp,text);
keylen=strlen(key);
for(i=0;i<keylen-
1;i++)
strcat(temp,"0");
textlen=strlen(temp);
strncpy(rem,temp,keylen);
while(i!=textlen)
{
if(rem[0]=='0')
{
strcpy(rem,&rem[1]);
rem[keylen-
1]=temp[++i];
rem[keylen]='\0';
continue;
}
for(j=0;j<keylen;j++)
rem[j]=((rem[j]-'0')^(key[j]-
'0'))+'0';
}
}
main()
{
int i;
int choice;
while(1)
{
printf("\n1.find crc\t2.check crc\t3.exit crc\nyour choice\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the input string\n");
scanf("%s",text);
printf("Enter the key\n");
scanf("%s",key);
crc();
printf("the transmitted message is %s\n",strcat(text,rem));
break;
case 2:
printf("Enter the input string\n");
scanf("%s",&text);
printf("Enter the key\n");
scanf("%s",key);
crc();
for(i=0;i<strlen(key)-
1;i++) if(rem[i]=='1')
break;
if(i==strlen(key)-
1)
printf("There is no error in the message\n");
else
printf("There is error in the message\n");
break;
case 3:
exit(0;
}
}
Output:-
1. find crc 2.check crc 3.exit
crc your choice 1
Enter the input string
110111
Enter the key
111
the transmitted message is 11011111

Result:-
Thus the implementation of error correction and detection technique CRC
was verified.
Implementation of Error Correction and Detection Technique
Hamming code

Aim:-
To implement the error correction and detection technique using hamming
code.

Software Required:-
Turbo C-Programming.

Algorithm:-
STEP-1 - Start.

STEP-2 - First write the bit positions starting from 1 in a binary form.

STEP-3 - Mark all the bit positions that are powers of two as parity bits.

STEP-4 - All other bit positions are for the data to be encoded using (3, 5,
6, 7, 9, 10 and 11, etc.)

STEP-5 - Apply FTP Traffic over TCP.Setup UDP Connection between


n(1) and n(5).

STEP-6 - Apply CBR Traffic over UDP.

STEP-7 - Apply CSMA/CA and CSMA/CD mechanisms and study their


performance.

STEP-8 - Schedule events and run the program.


Procedure:-
The amount of parity data added to Hamming code is given by the
formula 2p ≥ d + p + 1, where p is the number of parity bits and d is the number
of data bits. For example, if you wanted to transmit 7 data bits, the formula
would be 2 4 ≥ 7 + 4 + 1, so 4 parity bits are required.The r redundant bits placed
at bit positions of powers of 2, i.e. 1, 2, 4, 8, 16 etc. They are referred in the rest
of this text as r1 (at position 1), r2 (at position 2), r3 (at position 4), r4 (at
position 8) and so on.

Program:-
#include<stdio.h>
#include<conio.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]);
}
printf("\nenter the received data bits one by one: ");
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) {
printf("\ncongratulations there is no error: ");
} else {
printf("\nerron on the postion: %d\nthe correct message is \n",c);
if(rec[7-c]==0)
rec[7-c]=1;
elserec[7-c]=0;
for (i=0;i<7;i++) {
printf("%d ",rec[i]);
}
}
getch();
}

Output:-

Result:-
Thus implementation of error detection and correction using
hamming code was verified.
Implementation of Error Detection & Correction LRC

AIM:-
To implement the error correction and detection by using the LRC technique.

Software Reqired:-
Turbo C-Programming.

Algorithm:-
STEP- 1 - Start.

STEP-2 - Add all bytes in the message, excluding the starting colon and
ending CRLF. Add them into an eight-bit field, so that carries will be discarded.

STEP-3 - Subtract the final field value from FF hex (all 1's), to produce
the ones-complement.

STEP-4 - Add 1 to produce the two's-complement.

STEP-5 - Sender port number and the frame is input as receiver.

STEP-6 - Sender frame is send to the receiver and display to the receiver.

STEP-7 - Frame is received and displays the acknowledgement and


otherwise display the negative acknowledgement.

STEP-8 - Stop.
Procedure:-
The Longitudinal Redundancy Check (LRC) field is one byte, containing an
eight-bit binary value. The LRC value is calculated by the transmitting device,
which appends the LRC to the message. The receiving device recalculates an
LRC during receipt of the message, and compares the calculated value to the
actual value it received in the LRC field. If the two values are not equal, an error
results.The LRC is calculated by adding together successive eight-bit bytes in the
message, discarding any carries, then two's complementing the result. The LRC
is an eight-bit field, therefore each new addition of a character that would result in
a value higher than 255 decimal simply rolls over the field's value through zero.
Because there is no ninth bit, the carry is discarded automatically.

Program:-
#include<stdio.h>
#include<conio.h

>int binary(int);

void parity(int [],int []);

int arr[8],arr1[8],parityarr[8];

char chr;

int go,temp,temp1,i;

void main()

{ char chr1;
clrscr();

go=0;
Sender:

if(go==0)

printf("Enter Data : \n");

printf("\nEnter a character : ");


scanf("%c %c",&chr,&chr1);

temp=chr;

binary(temp);

printf("\n\nAscii value is : %d\n",temp);


printf("\nBinary Form : ");

for(i=7;i>=0;i--)

arr1[i]=arr[i];

printf("%d

",arr[i]);

printf("\n");

temp1=chr1;

binary(temp1);

printf("\n\nAscii value is : %d\n",temp1);


printf("\nBinary Form : ");
for(i=7;i>=0;i--)

printf("%d ",arr[i]);
}

parity(arr,arr1);

for(i=7;i>=0;i--
)

arr[i]=0;

arr1[i]=0

getch();

void parity(int arr[],int arr1[])

printf(“Receiver Side :\n”);

printf("\n\nLRC :\n");

for(i=7;i>=0;i--)

printf("%d ",arr[i]);

printf("\n");

for(i=7;i>=0;i--)

printf("%d ",arr1[i]);

}
printf("\n \n");
for(i=0;i<8;i++)

if(arr[i]==0 && arr1[i]==0 || arr[i]==1 && arr1[i]==1)

parityarr[i]=0;

else if(arr[i]==1 && arr1[i]==0 || arr[i]==0 && arr1[i]==1)

parityarr[i]=1;

for(i=7;i>=0;i--)

printf("%d ",parityarr[i]);

int binary(int x)

int rem;

int

ctr=0,i=1; do
{

rem=x%2;

arr[i]=rem;

if(rem==1)

ctr++;

x=x/2
;i++;

while(x!=0)

if(ctr%2==0
)

arr[0]=0;

else

arr[0]=1;

return(0);

}
Output:-

Result:-
Thus the error detection and correction technique using LRC was verified.
Implementation of Stop & Wait - Sliding Window Protocols

Aim:-
To implement the stop and wait and sliding window protocols using c-
programming.

Software required:-
Turbo C-programming.

Algorithm:-
STEP-1 - Start.

STEP-2 - Import all the necessary packages.

STEP-3 - Create 2 application sender and receiver.

STEP-4 - Connect both applications using socket.

STEP-5 - Sender port number and the frame is input as receiver.

STEP-6 - Sender frame is send to the receiver and display to the receiver.

STEP-7 - Frame is received and displays the acknowledgement and


otherwise display the negative acknowledgement.

STEP-8 - Receiver receives all the frames automatically and displays the
message.

STEP-9 - Close all the connections and terminate the program.

STEP-10 - Stop.

1
Procedure:
Establish connection to the server. Send the frame to the receiver.If it
receive the acknowledgement and negative acknowledgement from the receiver.
If the receiver negative acknowledgement then once again send the frame to the
receiver.Close the sender side connection.Establish connection to the
server.Receive the frame from the receiver. Send acknowledgement and negative
acknowledgement from the receiver. Close the sender side connection.

Program:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h
>int main()
{
int i, n, ch;
char input[20];
FILE *in;
randomize();
printf(“\n\t\t Stop and Wait Protocol \n”);
printf(“\n 1. Send \n2. Check
ACK\n3.EOT\n”); while(1)
{
printf(“Enter your
choice….”);scanf(“%d”,
&ch);
switch(ch)
{
case 1:
in = fopen(“data.txt”, “w”);
printf(“Enter the Data: “);
scanf(“%s”, input);
n = strlen(input);
for(i=0; i<n+1; i++)
fprintf(in, “%s”,
input); fclose(in);
printf(“——->Data
Sent\n”); break;
getch();

2
}

RECEVIER.C:-
#include<stdio.h>
#include<conio.h>
#include<string.h
> void main()
{
int i, n,;
char output[20];
FILE *out;
clrscr();
out=fopen(“data_parity.txt”, “r”);
fscanf(out, “%s”, output);
n = strlen(output);
for(i=0; i<n; i++)
if(output[i]==’1’)
one++;
if(one%2==0
)
{
printf(“Received Data has Even
Parity\n”); printf(“Data Accepted\n”);
}
else
{
printf(“Received Data has Odd Parity\n”);
printf(“Data Rejected\n”);
}
printf(“Received Data:
“);for(i=0;i<n-1; i++)
printf(“%c”, output[i];
fclose(out);
getch();
}

3
OUTPUT:-

4
Program:-
Sliding window
protocols:- #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 ORWINDOW \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();
}

5
Output:-

ENTER THE WINDOWS SIZE : 10

SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW

ENTER THE DATA TO BE SENT:

ForgetCode.comMESSAGE 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

6
Result:-
Thus the C-programming using stop and wait and sliding window protocols
was implemented and verified successfully.

7
Implementation of Go-Back & Selective Repeat Protocols

Aim:-
To implement the go-back and selective repeat protocols by using C-
programming.

Software Required:-
Turbo C-Programming.
Algorithm:-
STEP-1 - Start.

STEP-2 - In Go-Back-N Protocol, if the sent frame are find suspected then all the
frames are re-transmitted from the lost packet to the last packet transmitted

STEP-3 - In selective Repeat protocol, only those frames are re-transmitted which
are found suspected.

STEP-4 -Particular repetition allows the transmitter to deliver a set number of


frames without waiting for individual ACK from the recipient, as in Go-Back-
N ARQ.

STEP-5 - The size is half of the frame's maximum sequence number. For
example, if the sequence number is from 0–15, the window size will be 8.

STEP-6 - Repeat protocol allows it to send several frames based on the


availability of structures in the sending window.

STEP-7 -The size of the transmitting window determines the maximum number
of frames that can be sent.

STEP-8 - Stop.
Procedure:-
Go-Back-N 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.
Selective Repeat Protocol is also a data link layer protocol that uses sliding
window method for reliable delivery of data frames. Here, only the erroneous or
lost frames are retransmitted, while the good frames are received and buffered.
Program:-
Go Back N protocol
#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.

Please enter the last Acknowledgement received.


2
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.

Please enter the last Acknowledgement received.


8
Selective Repeat Protocols.
#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#define cls() printf(“33[H33[J”)
struct frame
{
int packet[40];
};
struct ack
{
int acknowledge[40];
};
int main()
{
int clientsocket;
sockaddr_in serveraddr;
socklen_t len;
hostent * server;
frame f1;
int
windowsize,totalpackets,totalframes,i=0,j=0,framesreceived=0,k,l,m,repacket[4
0];
ack acknowledgement;
char req[50];
clientsocket=socket(AF_INET,SOCK_DGRAM,
0);
bzero((char*)&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_port=htons(5018);
server=gethostbyname(“127.0.0.1”);
bcopy((char*)server->h_addr,(char*)&serveraddr.sin_addr.s_addr,sizeof(server-
>h_addr));
printf(“\nSending request to the client.\n”);
sendto(clientsocket,”HI I AM CLIENT.”,sizeof(“HI I AM
CLIENT.”),0,(sockaddr*)&serveraddr,sizeof(serveraddr));
printf(“\nWaiting for reply.\n”);
recvfrom(clientsocket,req,sizeof(req),0,(sockaddr*)&serveraddr,&len);
printf(“\nThe server has send:\t%s\n”,req);
printf(“\nEnter the window size:\t”);
scanf(“%d”,&windowsize);
printf(“\n\nSending the window size.\n”);
sendto(clientsocket,(char*)&windowsize,sizeof(windowsize),0,(sockaddr*)&serv
eraddr,sizeof(serveraddr));
cls();
printf(“\nWaiting for the server response.\n”);
recvfrom(clientsocket,(char*)&totalpackets,sizeof(totalpackets),0,(sockaddr*)&
serveraddr,&len);
printf(“\nThe total packets are:\t%d\n”,totalpackets);
sendto(clientsocket,”RECEIVED.”,sizeof(“RECEIVED.”),0,(sockaddr*)&serverad
dr, sizeof(serveraddr));
recvfrom(clientsocket,(char*)&totalframes,sizeof(totalframes),0,(sockaddr*)&se
rveraddr,&len);
printf(“\nThe total frames/windows are:\t%d\n”,totalframes);
sendto(clientsocket,”RECEIVED.”,sizeof(“RECEIVED.”),0,(sockaddr*)&servera
ddr,
sizeof(serveraddr));
printf(“\nStarting the process of receiving.\n”);
j=0;
l=0;
while(l<totalpackets)
{
printf(“\nInitialising the receive buffer.\n”);
printf(“\nThe expected frame is %d with packets: “,framesreceived);
for(m=0;m<j;m++)
{
printf(“%d “,repacket[m]);
}
while(j<windowsize && i<totalpackets)
{
printf(“%d “,i);
i++
;
j++
;
}
printf(“\n\nWaiting for the frame.\n”);
recvfrom(clientsocket,(char*)&f1,sizeof(f1),0,(sockaddr*)&serveraddr,&len);
printf(“\nReceived frame %d\n\nEnter -1 to send negative acknowledgement
for the following packets.\n”,framesreceived);
j=0;
m=0;
k=l;
while(m<windowsize && k<totalpackets)
{
printf(“\nPacket: %d\n”,f1.packet[m]);
scanf(“%d”,&acknowledgement.acknowledge[m]);
if(acknowledgement.acknowledge[m]==-1)
{
repacket[j]=f1.packet[m];
j++;
}
else
{
l++
;
}
m++
;
k++;
}
framesreceived++;
sendto(clientsocket,(char*)&acknowledgement,sizeof(acknowledgement),0,(soc
kaddr*)&serveraddr,sizeof(serveraddr));
cls();
}
printf(“\nAll frames received successfully.\n\nClosing connection with the
server.\n”);
close(clientsocket);
}

Output:-

Result:-
Thus the implementation of go-back n and selective repeat protocols are
verified.
Implementation of Distance Vector Routing algorithm.

AIM:-
To implement the distance vector routing algorithm of bellman Ford.

Software Required:-
Turbo C-Programming.

Algorithm:-

STEP-1 - Start.

STEP-2 - The routers in the network start sharing their information with
the neighboring router in the network.

STEP-3 - After creating the separate local table this information is shared
with the neighboring node having a direct link.

STEP-4 - In the distance vector routing protocol, only distance vector


information is exchanged next-hop values are not exchanged.

STEP-5 - while creating a new routing table, only the distance vector
information sis hared that to its neighbor which has a direct link.

STEP-6 - Distant vector routing algorithm also called as Bellman-Ford


algorithm or Ford Fulkerson algorithm used to calculate the shortest path in
the network.

STEP-7 - Stop.
Procedure:-
As you can see in the above network all the link has been used. In the
routing table of A link AD and AB is used. In the routing table of B only
link BA and BC. In the routing table of C, only links CB and CD are used and in
D'srouting table only links DA and DC are used.

In this sense, we can also check which of the links can be used and which cannot.
In this way we find the shortest path using the distance vector routing algorithm.

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];
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[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<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:-
Enter the number of nodes :
3
Enter the cost matrix :
027
201
710
For router 1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 3
For router 2
node 1 via 1 Distance 2
node 2 via 2 Distance 0
node 3 via 3 Distance 1
For router 3
node 1 via 1 Distance 3
node 2 via 2 Distance 1
node 3 via 3 Distance 0

Result:-
Thus the implementation of Distance Vector Routing algorithm was
implemented and verified successfully.
Implementation of link state routing algorithm

AIM:-
To implement the link state routing algorith of open shortest path.

Software Required:-
Turbo C-Programming.

Algorithm:-
STEP-1 - Start.

STEP-2 - The currently known least cost path from A to its directly
attached neighbors, B, C, D are 2,5,1 respectively.

STEP-3 - The cost from A to B is set to 2, from A to D is set to 1 and


from A to C is set to 5. The cost from A to E and F are set to infinity as they are
not directly linked to A.

STEP-4 - Therefore, it is added in N. Now, we need to determine a least-


cost path through D vertex.

STEP-5 -In the above table, we observe that both E and B have the least
cost path in step 2. Let's consider the E vertex. Now, we determine the least cost
path of remaining vertices through E.

STEP-6 -Therefore, it is added in N. Now, we determine the least cost


path of remaining vertices through B.

STEP-7 - Stop.
Procedure:-
Instead of sending its routing table, a router sends the information
about its neighborhood only. A router broadcast its identities and cost of the
directly attached links to other routers.Each router sends the information to every
other router on the internetwork except its neighbors. This process is known as
Flooding. Every router that receives the packet sends the copies to all its
neighbors. Finally, each and every router receives a copy of the same
information. A router sends the information to every other router only when the
change occurs in the information.

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];
printf("\n Enter the no of routers");
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;
}
}
printf("\n Enter the source router:");
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];
}
printf("\n Shortest path cost:%d",dist[i]);
}
}
Output:-
Enter the no of routers3
Enter the cost matrix values:
0->0:
[exam47@cselinux ~]$ ./a.out
Enter the no of routers2 Enter
the cost matrix values:
0->0:3
0->1:4
1->0:5
1->1:6
Enter the source router:1
1==>0:Path taken:0
<--1
Shortest path cost:5
1==>1:Path taken:1
Shortest path cost:6

Result:-
Thus the implementation of link state routing algorithm was
implemented and successfully verified.
Data Encryption & Decryption Using Data Encryption
Standard Algorithm

AIM:-
To implement the data encryption and decryption using DES.

Software Required:-
Turbo C-Programming.

Algorithm:-
STEP-1 - Start.

STEP-2 - The process begins with the 64-bit plain text block getting
handed over to an initial permutation (IP) function.

STEP-3 - The initial permutation (IP) is then performed on the plain text.

STEP-4 - Next, the initial permutation (IP) creates two halves of the
permuted block, referred to as Left Plain Text (LPT) and Right Plain Text (RPT).

STEP-5 - Each LPT and RPT goes through 16 rounds of the


encryption process.

STEP-6 - Finally, the LPT and RPT are rejoined, and a Final
Permutation (FP) is performed on the newly combined block.

STEP-7 -The result of this process produces the desired 64-bit ciphertext.

STEP-8 - Stop.
Procedure:-
DES stands for Data Encryption Standard. There are certain machines that
can be used to crack the DES algorithm. The DES algorithm uses a key of 56-bit
size. Using this key, the DES takes a block of 64-bit plain text as input and
generates a block of 64-bit cipher text.The DES process has several steps
involved in it, where each step is called a round. Depending upon the size of the
key being used, the number of rounds varies. For example, a 128-bit key requires
10 rounds, a 192-bit key requires 12 rounds, and so on.

Program:-

#include <stdio.h>

int Original_key [64] = { // you can change key if

required0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0,

0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1,

1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0,

1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1

};

int Permutated_Choice1[56] =

{ 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 Permutated_Choice2[48] =

{ 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 Iintial_Permutation [64] =

{ 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 Final_Permutation[] =

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 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 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 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, 6, 13


0,
};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, 1,


15,
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 shifts_for_each_round[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1

}; int _56bit_key[56];

int _48bit_key[17][48];

int text_to_bits[99999], bits_size=0;

int Left32[17][32], Right32[17][32];

int EXPtext[48];
int

XORtext[48];

int X[8][6];

int

X2[32]; int

R[32];

int chiper_text[64];

int encrypted_text[64];

int XOR(int a, int b) {

return (a ^ b);

void Dec_to_Binary(int n)

int binaryNum[1000];

int i = 0;

while (n > 0) {

binaryNum[i] = n %

2;n = n / 2;

i++;

for (int j = i - 1; j >= 0; j--) {

text_to_bits[bits_size++] = binaryNum[j];
}

int F1(int i)

int r, c, b[6];

for (int j = 0; j < 6;

j++) b[j] = X[i][j];

r = b[0] * 2 + b[5];

c = 8 * b[1] + 4 * b[2] + 2 * b[3] + b[4];

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 PBox(int pos, int bit)

int i;

for (i = 0; i < 32;

i++) if (P[i] == pos

+ 1) break;

R[i] = bit;

int ToBits(int value)

int k, j, m;

static int i;

if (i % 32 == 0)

i = 0;
for (j = 3; j >= 0; j--)

m = 1 << j;

k = value &

m; if (k == 0)

X2[3 - j + i] = '0' - 48;

else

X2[3 - j + i] = '1' - 48;

i = i + 4;

int SBox(int XORtext[])

int k = 0;

for (int i = 0; i < 8;

i++) for (int j = 0; j <

6; j++) X[i][j] =

XORtext[k++]; int

value;

for (int i = 0; i < 8; i++)

{
value = F1(i);

ToBits(value)

void expansion_function(int pos, int bit)

for (int i = 0; i < 48;

i++) if (E[i] == pos + 1)

EXPtext[i] = bit;

void cipher(int Round, int mode)

for (int i = 0; i < 32; i++)

expansion_function(i, Right32[Round -

1][i]); for (int i = 0; i < 48; i++)

if (mode == 0)

XORtext[i] = XOR(EXPtext[i],

_48bit_key[Round][i]); else

XORtext[i] = XOR(EXPtext[i], _48bit_key[17 - Round][i]);


}

SBox(XORtext);

for (int i = 0; i < 32;

i++)PBox(i, X2[i]);

for (int i = 0; i < 32; i++)

Right32[Round][i] = XOR(Left32[Round - 1][i], R[i]);

void finalPermutation(int pos, int bit)

int i;

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

if (Final_Permutation[i] == pos +

1) break;

encrypted_text[i] = bit;

void Encrypt_each_64_bit (int plain_bits [])

int IP_result [64] ,

index=0; for (int i = 0; i <

64; i++) {
IP_result [i] = plain_bits[ Iintial_Permutation[i] ];

for (int i = 0; i < 32; i++)

Left32[0][i] =

IP_result[i]; for (int i =

32; i < 64; i++)

Right32[0][i - 32] =

IP_result[i]; for (int k = 1; k <

17; k++)

{ // processing through all 16 rounds

cipher(k, 0);

for (int i = 0; i < 32; i++)

Left32[k][i] = Right32[k - 1][i]; // right part comes as it is to next round left part

for (int i = 0; i < 64; i++)

{ // 32bit swap as well as Final Inverse Permutation

if (i < 32)

chiper_text[i] =

Right32[16][i]; else

chiper_text[i] = Left32[16][i - 32];

finalPermutation(i, chiper_text[i]);

}
for (int i = 0; i < 64; i++)

printf("%d", encrypted_text[i]);

void convert_Text_to_bits(char *plain_text){

for(int i=0;plain_text[i];i++){

int asci =

plain_text[i];

Dec_to_Binary(asci);

void key56to48(int round, int pos, int bit)

int i;

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

if (Permutated_Choice2[i] == pos +

1) break;

_48bit_key[round][i] = bit;

int key64to56(int pos, int bit)

int i;
for (i = 0; i < 56; i++)

if (Permutated_Choice1[i] == pos +

1) break;

_56bit_key[i] = bit;

void key64to48(int key[])

int k, backup[17][2];

int CD[17][56];

int C[17][28], D[17][28];

for (int i = 0; i < 64;

i++) key64to56(i,

key[i]); for (int i = 0; i

< 56; i++) if (i < 28)

C[0][i] =

_56bit_key[i]; else

D[0][i - 28] =

_56bit_key[i]; for (int x =

1; x < 17; x++)

int shift = shifts_for_each_round[x - 1];


for (int i = 0; i < shift; i++)

backup[x - 1][i] = C[x - 1][i];

for (int i = 0; i < (28 - shift);

i++) C[x][i] = C[x - 1][i +

shift];

k = 0;

for (int i = 28 - shift; i < 28;

i++) C[x][i] = backup[x -

1][k++];

for (int i = 0; i < shift; i++)

backup[x - 1][i] = D[x - 1][i];

for (int i = 0; i < (28 - shift);

i++) D[x][i] = D[x - 1][i +

shift];

k = 0;

for (int i = 28 - shift; i < 28;

i++) D[x][i] = backup[x -

1][k++];

for (int j = 0; j < 17; j++)

for (int i = 0; i < 28;


i++) CD[j][i] = C[j][i];

CD[j][i] = D[j][i - 28];

for (int j = 1; j < 17;

j++) for (int i = 0; i <

56; i++) key56to48(j, i,

CD[j][i]);

int main(){

char plain_text[] = "tomarrow we wiil be declaring war";

convert_Text_to_bits(plain_text);

key64to48(Original_key); // it creates all keys for all rounds

int _64bit_sets = bits_size/64;

printf("Decrypted output is\n");

for(int i=0;i<= _64bit_sets ;i++) {

Encrypt_each_64_bit (text_to_bits + 64*i);

return 0;

}
Output:-

Decrypted output is
0000111001101001001100011010111010010110111010111111111000010111
0010111110111111010100110111010110110000001110111001000000101101
0100010101100001100100000010100000101001111010100101100011101001
1001110010110011011110110001101110000000001000001001000110111010

Result:-
Thus the implementation of DES algorithm was verified.
Data Encryption & Decryption Using RSA Algorithm

AIM:-
To implement the encryption and decryption using the RSA algorithm.

Software Required:-
Turbo C-Programming.

Algorithm:-
STEP-1 - Start.

STEP-2 - Select two large prime numbers, p and q.

STEP-3 - Multiply these numbers to find n = p x q, where n is called the


modulus for encryption and decryption.

STEP-4 - Choose a number e less than n, such that n is relatively prime


to (p - 1) x (q -1). It means that e and (p - 1) x (q - 1) have no common factor
except. Choose "e" such that 1<e < φ (n), e is prime to φ (n),
gcd (e,d(n)) =1

STEP-5 - If n = p x q, then the public key is <e, n>. A plain text


message m is encrypted using public key <e, n>. To find ciphertext from the
plain text following formula is used to get ciphertext C.

STEP-6 - Stop.
Procedure:-
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.

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:-

Result:-
Thus the encryption and decryption of the data using the RSA algorithm was
implemented and verified sucessfully.
Implement Client Server Model Using Ftp Protocol

AIM:-
To implement the client server model using the FTP protocol.

Software Required:-
Turbo C-Programming.

Algorithm:-
STEP-1 - Start.

STEP 2: Declare the variables for the socket.

STEP 3: Specify the family, protocol, IP address and port number.

STEP 4: Create a socket using socket() function.

STEP 5: Bind the IP address and Port number.

STEP 6: Listen and accept the client’s request for the connection.

STEP 7: Establish the connection with the client.

STEP 8: Close the

socket. STEP 9: Stop.


Procedure:-
A TCP connection uses a three-way handshake to connect the client
and the server. It is a process that requires both the client and the
server to exchange synchronization (SYN) and acknowledge (ACK)
packets before the data transfer takes place.The client.c file contains the
code for the client-side, which read the text file and sends it to the
server and the server.c file receives the data from the client and
saves it in a text file.
Program:
-
SERVER
:
#include<stdio.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX
60int i, j, tem;
char buff[4096], t;
FILE *f1;
int main(int afg, char *argv)
{
int sockfd, newsockfd, clength;
struct sockaddr_in serv_addr,cli_addr;
char t[MAX], str[MAX];
strcpy(t,"exit");
sockfd=socket(AF_INET, SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
printf("\nBinded");
bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr));
printf("\nListening...");
listen(sockfd, 5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*) &cli_addr,&clength);
close(sockfd);
read(newsockfd, &str, MAX);
printf("\nClient message\n File Name : %s\n", str);
f1=fopen(str, "r");
while(fgets(buff, 4096, f1)!=NULL)
{
write(newsockfd,
buff,MAX); printf("\n");
}
fclose(f1);
printf("\nFile Transferred\n");
return 0;
}
CLIENT:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SERV_TCP_PORT 5035
#define MAX 60
int main(int arg,char*argv[])
{
int sockfd,n;
struct sockaddr_in serv_addr;
struct hostent*server;
char send[MAX],recvline[MAX],s[MAX],name[MAX];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
printf("\nEnter the source file name : \n");
scanf("%s",send);
write(sockfd,send,MAX);
while((n=read(sockfd,recvline,MAX))!=0)
{
printf("%s",recvline);
}
close(sockfd)
;return 0;
}

Output:-
Result:-
Thus the program for FTP client server was executed and the output was
verified.
Performance Analysis Of Csma/Ca And Csma/Cd Protocols

Aim:-
To create scenario and study the performance of CSMA / CD protocol through
simulation.

Software required:-
Ns-2.

Algorithm:-
STEP-1 - Start.

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 duplex links between the nodes and add Orientation to the
nodes for setting a LAN topology.Setup TCP Connection between n(0) and n(4).

STEP-5 - Apply FTP Traffic over TCP.Setup UDP Connection between


n(1) and n(5).

STEP-6 - Apply CBR Traffic over UDP.

STEP-7 - Apply CSMA/CA and CSMA/CD mechanisms and study their


performance.

STEP-8 - Schedule events and run the program.


Procedure:-
Ethernet is a LAN (Local area Network) protocol operating at the MAC
(Medium Access Control) layer. Ethernet has been standardized as per
IEEE
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.

Program:-
CSMA/CA
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace files
set file1 [open out.tr
w]
set winfile [open WinFile w]
$ns trace-all $file1
#Open the NAM trace
file set file2 [open
out.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure
proc finish {} {
global ns file1 file2
$ns flush-trace
close $file1
close $file2
exec nam out.nam &
exit 0
}
#Create six 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]
$n1 color red
$n1 shape box
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail
MAC/Csma/Ca Channel]
Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552
#Setup a FTP over TCP connection set ftp
[new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection set udp
[new Agent/UDP]
$ns attach-agent $n1 $udp set null
[new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection set
cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the #
tcp source node, will be called here "tcp",
# and the name of output file.
proc plotWindow {tcpSource file} { global
nsset time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_] set wnd
[$tcpSource set window_] puts $file "$now
$cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winfile"
$ns at 5 "$ns trace-annotate \"packet drop\""#
PPP
$ns at 125.0 "finish"
$ns run
Output:-
CSMA/CD
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the Trace files set
file1 [open out.tr w]
set winfile [open WinFile w]
$ns trace-all $file1
#Open the NAM trace file set
file2 [open out.nam w]
$ns namtrace-all $file2
#Define a 'finish' procedure proc
finish {} {
global ns file1 file2
$ns flush-trace close
$file1 close $file2
exec nam out.nam & exit 0
}
#Create six 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]
$n1 color red
$n1 shape box
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.3Mb 100ms DropTail
$ns simplex-link $n3 $n2 0.3Mb 100ms DropTail
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail
MAC/Csma/Cd Channel]
Setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
$tcp set window_ 8000
$tcp set packetSize_ 552
#Setup a FTP over TCP connection set ftp
[new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection set udp
[new Agent/UDP]
$ns attach-agent $n1 $udp set null
[new Agent/Null]
$ns attach-agent $n5 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection set
cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 0.01mb
$cbr set random_ false
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 124.0 "$ftp stop"
$ns at 124.5 "$cbr stop"
# next procedure gets two arguments: the name of the #
tcp source node, will be called here "tcp",
# and the name of output file.
proc plotWindow {tcpSource file} { global
nsset time 0.1
set now [$ns now]
set cwnd [$tcpSource set cwnd_] set wnd
[$tcpSource set window_] puts $file "$now
$cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file" }
$ns at 0.1 "plotWindow $tcp $winfile"
$ns at 5 "$ns trace-annotate \"packet drop\""
# PPP
$ns at 125.0 "finish"
$ns run

Output:-

Result:-
Thus, the performance of CSMA / CD protocol was studied through
simulation.
Implementation of Star & Ring Topology

AIM:
To create scenario and study the performance of token ring protocols through
simulation.

SOFTWARE REQUIREMENTS:
NS-2

ALGORITHM:
1. Create a simulator object
2. Define different colors for different data flows
3. Open a nam trace file and define finish procedure then close the trace
file, and execute nam on trace file.
4. Create five nodes that forms a network numbered from 0 to 4
5. Create duplex links between the nodes to form a Ring Topology.
6. Setup TCP Connection between n(1) and n(3)
7. Apply CBR Traffic over TCP
8. Schedule events and run the program.

Procedure:
Token ring is a LAN protocol operating in the MAC layer. Token ring is
standardized as per IEEE 802.5. Token ring can operate at speeds of 4mbps and
16 mbps. The operation of token ring is as follows: Whenthere is no traffic on the
network a simple 3-byte token circulates the ring. If the token is free (no reserved
by a station of higher priority as explained later) then the station may seize the
token and start sending the data frame. As the frame travels around the ring e ach
station examines the destination address and is either forwarded (if the recipient
is another node) or copied. After copying4 bits of the last byte is changed.
The star topology reduces the damage caused by line failure by connecting
all of the systems to a central 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.
Program:-
Star topology:-
#Create a simulator
object set ns [new
Simulator]
#Open the nam trace
file set nf [open
out.nam w]
$ns namtrace-all $nf
#Define a 'finish'
procedure proc finish {}
{
global ns nf
$nsflush-trace
#Close the trace file
close $nf
#Executenam on the trace
file exec nam out.nam &
exit0
}
#Create six 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]
#Change the shape of center node in a star topology
$n0 shape square
#Create links between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n3 1Mb 10ms DropTail
$ns duplex-link $n0 $n4 1Mb 10ms DropTail
$ns duplex-link $n0 $n5 1Mb 10ms DropTail
#Create a TCP agent and attach it to node n0 set tcp0
[new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#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
#Connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0
# Create a CBR traffic source and attach it to tcp0 set cbr0
[new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0
#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

Output:-
Ring topology:-
#Create a simulator
object set ns [new
Simulator]
#Open the nam trace
file set nf [open
out.nam w]
$ns namtrace-all $nf
#Define a 'finish'
procedure proc finish {}
{
global ns nf
$nsflush-trace
#Close the trace
file close $nf
#Executenam on the trace
file exec nam out.nam &
exit0
}
#Create five
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]
#Create links between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n2 $n3 1Mb 10ms DropTail
$ns duplex-link $n3 $n4 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
$ns duplex-link $n5 $n0 1Mb 10ms DropTail
#Create a TCP agent and attach it to node n0 set
tcp0 [new Agent/TCP]
$tcp0 set class_ 1
$ns attach-agent $n1 $tcp0
#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
#Connect the traffic sources with the traffic sink
$ns connect $tcp0 $sink0
# Create a CBR traffic source and attach it to tcp0 set
cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $tcp0
#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run

Output:-

Result:-
Thus the and Star and Ring Topology was simulated and studied.

You might also like