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

LAB FILE

COMPUTER NETWORK LAB


(KIT-653)
Bachelor of Technology
(Department of Information Technology)
SUBMITTED BY: Utkarsh (1809713114)

UNDER THE GUIDANCE OF: Mr. Hariharan

GALGOTIAS COLLEGE OF ENGINEEING AND


TECHNOLOGY
GREATER NOIDA
2020-21

Affiliated to:
DR. A.P.J. Abdul Kalam Technical University(A.K.T.U)
Lucknow, Uttar Pradesh

Experiment-1
Aim: Study of different types of Network cables and practically implements the cross-wired

Cable and straight through cable using clamping tool.

Apparatus (Components): RJ-45 connector, Crimping Tool, Twisted pair Cable

Procedure: To do these practical following steps should be done:

1. Start by stripping off about 2 inches of the plastic jacket off the end of the cable. Be very
careful at this point, as to not nick or cut into the wires, which are inside. Doing so could alter
the characteristics of your cable, or even worse render is useless. Check the wires, one more
time for nicks or cuts. If there are any, just whack the whole end off, and start over.
2. Spread the wires apart, but be sure to hold onto the base of the jacket with your other
hand.You do not want the wires to become untwisted down inside the jacket. Category 5 cable
must only have 1/2 of an inch of 'untwisted' wire at the end; otherwise it will be 'out of spec'.
At this point, you obviously have ALOT more than 1/2 of an inch of un-twisted wire.
3. You have 2 end jacks, which must be installed on your cable. If you are using a pre-made
cable, with one of the ends whacked off, you only have one end to install - the crossed over
end.

Below are two diagrams, which show how you need to arrange the cables for each type of cable
end.Decide at this point which end you are making and examine the associated picture below.
Diagram shows you how to prepare Cross wired connection

Diagram shows you how to prepare straight through wired connection

1
2
Experiment - 2

Aim: Study of following Network Devices in Detail

• Repeater

• Hub

• Switch

• Bridge

• Router

• Gate Way

Procedure: Following should be done to understand this practical.

1. Repeater: Functioning at Physical Layer. A repeater is an electronic device that receives


a signal and retransmits it at a higher level and/or higher power, or onto the other side of
an obstruction, so that the signal can cover longer distances. Repeater have two ports ,so
cannot be use to connect for more than two devices
2. Hub: An Ethernet hub, active hub, network hub, repeater hub, hub or concentrator is a
device for connecting multiple twisted pair or fiber optic Ethernet devices together and
making them act as a single network segment. Hubs work at the physical layer (layer 1)
of the OSI model. The device is a form of multiport repeater. Repeater hubs also
participate in collision detection, forwarding a jam signal to all ports if it detects a
collision.
3. Switch: A network switch or switching hub is a computer networking device that
connects network segments. The term commonly refers to a network bridge that
processes and routes data at the data link layer (layer 2) of the OSI model. Switches that
additionally process data at the network layer (layer 3 and above) are often referred to as
Layer 3 switches or multilayer switches.
4. Bridge: A network bridge connects multiple network segments at the data link layer
(Layer 2) of the OSI model. In Ethernet networks, the term bridge formally means a
device that behaves according to the IEEE 802.1D standard. A bridge and switch are
very much alike; a switch being a bridge with numerous ports. Switch or Layer 2 switch
is often used interchangeably with bridge . Bridges can analyze incoming data packets to
determine if the bridge is able to send the given packet to another segment of the
network.
5. Router: A router is an electronic device that interconnects two or more computer
networks, and selectively interchanges packets of data between them. Each data packet
contains address information that a router can use to determine if the source and
destination are on the same network, or if the data packet must be transferred from one
network to another. Where multiple routers are used in a large collection of
interconnected networks, the routers exchange information about target system
addresses, so that each router can build up a table showing the preferred paths between
any two systems on the interconnected networks.
6. Gate Way: In a communications network, a network node equipped for interfacing with
another network that uses different protocols.

3
• A gateway may contain devices such as protocol translators, impedance matching devices, rate
converters, fault isolators, or signal translators as necessary to provide system
interoperability. It also requires the establishment of mutually acceptable administrative
procedures between both networks.

• A protocol translation/mapping gateway interconnects networks with different network protocol


technologies by performing the required protocol conversions.

4
Experiment – 3

Aim: Study of network IP

• Classification of IP address

• Sub netting

• Super netting

Apparatus (Software): NA

Procedure: Following is required to be study under this practical.

• Classification of IP address

Class Address Range Supports

Class A 1.0.0.1 to 126.255.255.254 Supports 16 million hosts on


each of 127 networks.

Class B 128.1.0.1 to 191.255.255.254 Supports 65,000 hosts on each


of 16,000 networks.

Class C 192.0.1.1 to 223.255.255.254 Supports 254 hosts on each of


2 million networks.

Class D 224.0.0.0 to 239.255.255.255 Reserved for multicast groups.

Class E 240.0.0.0 to 254.255.255.254 Reserved

As show in figure we teach how the ip addresses are classified and when they are used.

• Sub netting is the practice of dividing a network into two or smaller networks. It increases routing
efficiency, which helps to enhance the security of the network and reduces the size of the broadcast
domain.

IP Subnetting designates high-order bits from the host as part of the network prefix. This method
divides a network into smaller subnets.

It also helps you to reduce the size of the routing tables, which is stored in routers. This method also
helps you to extend the existing IP address base & restructures the IP address.

5
Supernetting is the opposite of Subnetting. In subnetting, a single big network is divided
into multiple smaller subnetworks. In Supernetting, multiple networks are combined into a
bigger network termed as a Supernetwork or Supernet.
Supernetting is mainly used in Route Summarization, where routes to multiple networks with
similar network prefixes are combined into a single routing entry, with the routing entry
pointing to a Super network, encompassing all the networks. This in turn significantly
reduces the size of routing tables and also the size of routing updates exchanged by routing
protocols.
More specifically,
● When multiple networks are combined to form a bigger network, it is termed as super-
netting
● Super netting is used in route aggregation to reduce the size of routing tables and routing
table updates
There are some points which should be kept in mind while supernetting:
1. All the Networks should be contiguous.
2. The block size of every networks should be equal and must be in form of 2n.
3. First Network id should be exactly divisible by whole size of supernet.

6
Experiment-4

Aim: Connect the computers in Local Area Network.

Procedure: On the host computer

On the host computer, follow these steps to share the Internet connection:

1. Log on to the host computer as Administrator or as Owner.

2. Click Start, and then click Control Panel. 3. Click Network and Internet Connections. 4. Click
Network Connections. 5. Right-click the connection that you use to connect to the Internet. For
example, if you connect to the Internet by using a modem, right-click the connection that you want
under Dial-up/ other network available.

6. Click Properties.

7. Click the Advanced tab.

8. Under Internet Connection Sharing, select the Allow other network users to connect through this
computer's Internet connection check box.

9. If you are sharing a dial-up Internet connection, select the Establish a dial-up connection whenever
a computer on my network attempts to access the Internet check box if you want to permit your
computer to automatically connect to the Internet.

10. Click OK. You receive the following message: When Internet Connection Sharing is enabled,
your LAN adapter will be set to use IP address 192.168.0. 1. Your computer may lose connectivity
with other computers on your network. If these other computers have static IP addresses, it is a
good idea to set them to obtain their IP addresses automatically. Are you sure you want to enable
Internet Connection Sharing?

11. Click Yes. The connection to the Internet is shared to other computers on the local area network
(LAN). The network adapter that is connected to the LAN is configured with a static IP address
of192.168.0. 1 and a subnet mask of 255.255.255.0

On the client computer

To connect to the Internet by using the shared connection, you must confirm the LAN adapter
IPconfiguration, and then configure the client computer. To confirm the LAN adapter IP
configuration, follow these steps:

1. Log on to the client computer as Administrator or as Owner.

7
2. Click Start, and then click Control Panel.

3. Click Network and Internet Connections.

4. Click Network Connections.

5. Right-click Local Area Connection and then click Properties.

6. Click the General tab, click Internet Protocol (TCP/IP) in the connection uses the following items
list, and then click Properties.

7. In the Internet Protocol (TCP/IP) Properties dialog box, click Obtain an IP address automatically (if
it is not already selected), and then click OK

8. IP Address 192.168.31.202

9. Subnet mask 255.255.255.0

10. Default gateway 192.168.31.1

11. In the Local Area Connection Properties dialog box, click OK. 12. Quit Control Panel.

8
Experiment No: 5

Aim: Write a Program for ERROR detecting code using CRC-CCITT (16bit).

Whenever digital data is stored or interfaced, data corruption might occur. Since the beginning of
computer science, developers have been thinking of ways to deal with this type of problem. For serial
data they came up with the solution to attach a parity bit to each sent byte. This simple detection
mechanism works if an odd number of bits in a byte changes, but an even number of false bits in one
byte will not be detected by the parity check. To overcome this problem developers have searched for
mathematical sound mechanisms to detect multiple false bits. The CRC calculation or cyclic
redundancy check was the result of this. Nowadays CRC calculations are used in all types of
communications. All packets sent over a network connection are checked with a CRC. Also each data
block on your hard disk has a CRC value attached to it. Modern computer world cannot do without
these CRC calculations. So let's see why they are so widely used. The answer is simple; they are
powerful, detect many types of errors and are extremely fast to calculate especially when dedicated
hardware chips are used.

The idea behind CRC calculation is to look at the data as one large binary number. This number is
divided by a certain value and the remainder of the calculation is called the CRC. Dividing in the
CRC calculation at first looks to cost a lot of computing power, but it can be performed very quickly
if we use a method similar to the one learned at school. We will as an example calculate the remainder
for the character 'm'—which is 1101101 in binary notation—by dividing it by 19 or 10011. Please
note that 19 is an odd number. This is necessary as we will see further on. Please refer to your
schoolbooks as the binary calculation method here is not very different from the decimal method you
learned when you were young. It might only look a little bit strange. Also notations differ between
countries, but the method is similar. With decimal calculations you can quickly check that 109 divided
by 19 gives a quotient of 5 with 14 as the remainder. But what we also see in the scheme is that every
bit extra to check only costs one binary comparison and in 50% of the cases one binary subtraction.
You can easily increase the number of bits of the test data string—for example to 56 bits if we use our
example value "Lammert"—and the result can be calculated with 56 binary comparisons and an
average of 28 binary subtractions. This can be implemented in hardware directly with only very few
transistors involved. Also software algorithms can be very efficient.

All of the CRC formulas you will encounter are simply checksum algorithms based on modulo-2
binary division where we ignore carry bits and in effect the subtraction will be equal to an exclusive
or operation. Though some differences exist in the specifics acrossdifferent CRC formulas, the basic
mathematical process is always the same:

● The message bits are appended with c zero bits; this augmented message is the dividend

9
● A predetermined c+1-bit binary sequence, called the generator polynomial, is the divisor
● The checksum is the c-bit remainder that results from the division operation

Steps:

1. Multiply M(x) by highest power in G(x). i.e. Add So much zeros to M(x).

2. Divide the result by G(x). The remainder = C(x). Special case: This won't work if bitstring =all
zeros. We don't allow such an M(x).But M(x) bitstring = 1 will work, for example. Can divide
1101 into 1000.

3. If: x div y gives remainder c that means: x = n y + c Hence (x-c) = n y (x-c) div y gives remainder 0
Here (x-c) = (x+c) Hence (x+c) div y gives remainder 0

4. Transmit: T(x) = M(x) + C(x)

5. Receiver end: Receive T(x). Divide by G(x), should have remainder 0.

Source Code:

import java.util.*;
class crc
{ void div(int a[],int k)
{ int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int count=0;
for(int i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(int j=i;j<17+i;j++)
{
a[j]=a[j]^gp[count++];
}
count=0;
}
}
}
public static void main(String args[])
{
int a[]=new int[100];
int b[]=new int[100];
int len,k;
crc ob=new crc();
System.out.println("Enter the length of Data Frame:");
Scanner sc=new Scanner(System.in);
len=sc.nextInt();
int flag=0;
System.out.println("Enter the Message:");
for(int i=0;i<len;i++)
{ a[i]=sc.nextInt();
}
for(int i=0;i<16;i++)
{ a[len++]=0;
}
k=len-16;

10
for(int i=0;i<len;i++)
{ b[i]=a[i];
}
ob.div(a,k);
for(int i=0;i<len;i++)
a[i]=a[i]^b[i];
System.out.println("Data to be transmitted: ");
for(int i=0;i<len;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter the Reveived Data: ");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
ob.div(a, k);
for(int i=0;i<len;i++)
{
if(a[i]!=0)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("error in data");
else
System.out.println("no error");
}
}
Output:
Enter the length of Data Frame: 4
Enter the Message: 1 0 1 1
Data to be transmitted: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the Reveived Data: 1 0 1 1 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 1
ERROR in Recived Data

11
Experiment No: 6

Aim: Using TCP/IP Sockets, write a client-server program to make client sending the file name and
the server to send back the contents of the requested file if present. Implement the above program
using as message queues or FIFOs as IPC channels.

Socket is an interface which enables the client and the server to communicate and pass oninformation
from one another. Sockets provide the communication mechanism between two computers using
TCP. A client program creates a socket on its end of the communication and attempts to connect that
socket to a server. When the connection is made, the server creates a socket object on its end of the
communication. The client and the server can now communicate by writing to and reading from the
socket.

Source Code:

TCP Client

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
class Client
{
public static void main(String args[])throws Exception
{
String address = "";
Scanner sc=new Scanner(System.in);
System.out.println("Enter Server Address: ");
address=sc.nextLine();
//create the socket on port 5000
Socket s=new Socket(address,5000);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Send Get to start...");
String str="",filename="";
try

12
{
while(!str.equals("start"))
str=br.readLine();
dout.writeUTF(str);
dout.flush();
filename=din.readUTF();
System.out.println("Receving file: "+filename);
filename="client"+filename;
System.out.println("Saving as file: "+filename);
long sz=Long.parseLong(din.readUTF());
System.out.println ("File Size: "+(sz/(1024*1024))+" MB");
byte b[]=new byte [1024];
System.out.println("Receving file..");
FileOutputStream fos=new FileOutputStream(new File(filename),true);
long bytesRead;
do
{
bytesRead = din.read(b, 0, b.length);
fos.write(b,0,b.length);
}
while(!(bytesRead<1024));
System.out.println("Comleted");
fos.close();
dout.close();
s.close();
}
catch(EOFException e)
{
//do nothing
}
}
}

TCP Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
class Server
{
public static void main(String args[])throws Exception
{
String filename;
System.out.println("Enter File Name: ");
Scanner sc=new Scanner(System.in);
filename=sc.nextLine();
sc.close();
while(true)
{
//create server socket on port 5000
ServerSocket ss=new ServerSocket(5000);
System.out.println ("Waiting for request");

13
Socket s=ss.accept();
System.out.println ("Connected With "+s.getInetAddress().toString());
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
Computer Network Laboratory 2017-18
Dept. of CSE, CIT, Gubbi Page no 44
try
{
String str="";
str=din.readUTF();
System.out.println("SendGet....Ok");
if(!str.equals("stop"))
{ System.out.println("Sending File: "+filename);
dout.writeUTF(filename);
dout.flush();
File f=new File(filename);
FileInputStream fin=new FileInputStream(f);
long sz=(int) f.length();
byte b[]=new byte [1024];
int read;
dout.writeUTF(Long.toString(sz));
dout.flush();
System.out.println ("Size: "+sz);
System.out.println ("Buf size: "+ss.getReceiveBufferSize()); while((read = fin.read(b)) != -1)
{
dout.write(b, 0, read); dout.flush();
}
fin.close();
System.out.println("..ok"); dout.flush();
}
dout.writeUTF("stop");
System.out.println("Send Complete"); dout.flush();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("An error occured");
}
din.close();
s.close();
ss.close();
}
}
}

14
Experiment No: 7

Aim: Write a program on datagram socket for client/server to display the messages on clientside,
typed at the server side.

A datagram socket is the one for sending or receiving point for a packet delivery service. Each packet
sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from
one machine to another may be routed differently, and may arrive in any order.

Source Code:

UDP Client

import java.io.*;
import java.net.*;
public class UDPC
{
public static void main(String[] args)
{
DatagramSocket skt; try
{
skt=new DatagramSocket();
String msg= "text message ";
byte[] b = msg.getBytes();
InetAddress host=InetAddress.getByName("127.0.0.1");
int serverSocket=6788;
DatagramPacket request =new DatagramPacket (b,b.length,host,serverSocket);
skt.send(request);
byte[] buffer =new byte[1000];
DatagramPacket reply= new DatagramPacket(buffer,buffer.length);
skt.receive(reply);
System.out.println("client received:" +new String(reply.getData()));
skt.close();
}
catch(Exception ex)
{
}
}
}

UDP Server
import java.io.*;

15
import java.net.*;
public class UDPS
{
public static void main(String[] args)
{
DatagramSocket skt=null;
try
{
skt=new DatagramSocket(6788);
byte[] buffer = new byte[1000];
while(true)
{
DatagramPacket request = new DatagramPacket(buffer,buffer.length);
skt.receive(request);
String[] message = (new String(request.getData())).split(" ");
byte[] sendMsg= (message[1]+ " server processed").getBytes();
DatagramPacket reply = new DatagramPacket(sendMsg,sendMsg.length,request.getAddress
(),request.getPort());
skt.send(reply);
}
}
catch(Exception ex)
{
}
}
}

Output:
AT SERVER SIDE
[root@localhost ]# cc prg6s.c
[root@localhost ]# ./a.out
SERVER online!
CLIENT online!
Waiting for request....SERVER: /Test.txt found!
Transfering the contents...
SERVER transfer Completed!
SERVER transfer Completed!
Output:
AT CLIENT SIDE
[root@localhost]# cc prg6c.c
[root@localhost]# ./a.out
Waiting for SERVER...
SERVER online!
CLIENT: Enter the path: /Test.txt
Wating for reply...
File recieved! Displaying the contents:
Sockets are a mechanism for exchanging data between processes. These processes can
either be on the same machine, or on different machines connected via a network. Once a
socket connection is established, data can be sent in both directions until one of the endpoints
closes the connection.

16
Experiment No: 8

Aim: C Program for Congestion control using Leaky Bucket Algorithm

The main concept of the leaky bucket algorithm is that the output data flow remains constant despite
the variant input traffic, such as the water flow in a bucket with a small hole at the bottom. In case the
bucket contains water (or packets) then the output flow follows a constant rate, while if the bucket is
full any additional load will be lost because of spillover.

In a similar way if the bucket is empty the output will be zero. From network perspective, leaky
bucket consists of a finite queue (bucket) where all the incoming packets are stored in case there is
space in the queue, otherwise the packets are discarded. In order to regulate the output flow, leaky
bucket transmits one packet from the queue in a fixed time (e.g. at every clock tick). In the following
figure we can notice the main rationale of leaky bucket algorithm, for both the two approaches (e.g.
leaky bucket with water (a) and with packets (b)).

While leaky bucket eliminates completely bursty traffic by regulating the incoming data flow its main
drawback is that it drops packets if the bucket is full. Also, it doesn’t take into account the idle
process of the sender which means that if the host doesn’t transmit data for some time the bucket
becomes empty without permitting the transmission of any packet.

Implementation Algorithm:

Steps:

1. Read The Data For Packets

2. Read The Queue Size

3. Divide the Data into Packets

4. Assign the random Propagation delays for each packets to input into the bucket (input_packet).

5. wlile((Clock++<5*total_packets) and (out_packets< total_paclets))

a. if (clock == input_packet)

i. insert into Queue

b. if (clock % 5 == 0 )

17
i. Remove paclet from Queue

6. End

Source Code:

import java.util.*;
public class leaky
{
static int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
public static void main(String[] args)
{ int drop=0,mini,nsec,cap,count=0,i,process;
int inp[]=new int[25];
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Bucket Size\n");
cap= sc.nextInt();
System.out.println("Enter The Operation Rate\n");
process= sc.nextInt();
System.out.println("Enter The No. Of Seconds You Want To Stimulate\n");
nsec=sc.nextInt();
for(i=0;i<nsec;i++)
{ System.out.print("Enter The Size Of The Packet Entering At "+ i+1 + "sec");
inp[i] = sc.nextInt();
}
System.out.println("\nSecond | Packet Recieved | Packet Sent | Packet Left |Packet Dropped|\n");
//System.out.println("------------------------------------------------------------------------\n");
for(i=0;i<nsec;i++)
{ count+=inp[i];
if(count>cap)
{ drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t"+inp[i]);
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
drop=0;
System.out.println();
}
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}

18
System.out.print(i+1);
System.out.print("\t\t0");
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}

Output:
Enter The Bucket Size
5
Enter The Operation Rate
2
Enter The No. Of Seconds You Want To Stimulate
3
Enter The Size Of The Packet Entering At 1 sec
5
Enter The Size Of The Packet Entering At 1 sec
4
Enter The Size Of The Packet Entering At 1 sec
3
Second|Packet Recieved|Packet Sent|Packet Left|Packet Dropped|
---------------------------------------------------------------------------------
15230
24232
33231
40210
50100

19
Experiment No: 9

Aim: C Program for Simple RSA Algorithm to encrypt and decrypt the data

The RSA algorithm can be used for both public key encryption and digital signatures. Its security is
based on the difficulty of factoring large integers. The RSA algorithm's efficiency requires a fast
method for performing the modular exponentiation operation. A less efficient, conventional method
includes raising a number (the input) to a power (the secret or public key of the algorithm, denoted e
and d, respectively) and taking the remainder of the division with N. A straight-forward
implementation performs these two steps of the operation sequentially: first, raise it to the power and
second, apply modulo.

A very simple example of RSA encryption This is an extremely simple example using numbers you
can work out on a pocket calculator (those of you over the age of 35 can probably even do it by hand
on paper).

1. Select primes p = 11, q = 3.

2. n = pq = 11.3 = 33 phi = (p-1)(q-1) = 10.2 = 20

3. Choose e=3

Check gcd(e, p-1) = gcd(3, 10) = 1 (i.e. 3 and 10 have no common factors except 1), and check
gcd(e, q-1) = gcd(3, 2) = 1 therefore gcd(e, phi) = gcd(e, (p-1)(q-1)) = gcd(3, 20) = 1

4. Compute d such that ed ≡ 1 (mod phi)

i.e. compute d = e^-1 mod phi = 3^-1 mod 20

i.e. find a value for d such that phi divides (ed-1)

i.e. find d such that 20 divides 3d-1.

Simple testing (d = 1, 2, ...) gives d = 7

Check: ed-1 = 3.7 - 1 = 20, which is divisible by phi.

5. Public key = (n, e) = (33, 3)

Private key = (n, d) = (33, 7).

20
This is actually the smallest possible value for the modulus n for which the RSA algorithm works.

Now say we want to encrypt the message m = 7,

c = m^e mod n = 7^3 mod 33 = 343 mod 33 = 13.


Hence the ciphertext c = 13.
To check decryption we compute
m' = c^d mod n = 13^7 mod 33 = 7.
Note that we don't have to calculate the full value of 13 to the power 7 here. We can make use of the
fact that a = bc mod n = (b mod n).(c mod n) mod n so we can break down a potentially large number
into its components and combine the results of easier, smaller calculations to calculate the final value.

One way of calculating m' is as follows:-

m' = 13^7 mod 33 = 13^(3+3+1) mod 33 = 13^3.13^3.13 mod 33


= (13^3 mod 33).(13^3 mod 33).(13 mod 33) mod 33
= (2197 mod 33).(2197 mod 33).(13 mod 33) mod 33
= 19.19.13 mod 33 = 4693 mod 33
= 7.
Now if we calculate the cipher text c for all the possible values of m (0 to 32), we get
m 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
c 0 1 8 27 31 26 18 13 17 3 10 11 12 19 5 9 4
m 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
c 29 24 28 14 21 22 23 30 16 20 15 7 2 6 25 32

Note that all 33 values of m (0 to 32) map to a unique code c in the same range in a sort of random
manner. In this case we have nine values of m that map to the same value of c - these are known as
unconcealed messages. m = 0 and 1 will always do this for any N, no matter how large. But in
practice, higher values shouldn't be a problem when we use large values for N.

If we wanted to use this system to keep secrets, we could let A=2, B=3, ..., Z=27. (We specifically
avoid 0 and 1 here for the reason given above). Thus the plaintext message "HELLOWORLD" would
be represented by the set of integers m1, m2, ... {9,6,13,13,16,24,16,19,13,5}

Using our table above, we obtain ciphertext integers c1, c2, ... {3,18,19,19,4,30,4,28,19,26}

Note that this example is no more secure than using a simple Caesar substitutioncipher, but it serves
to illustrate a simple example of the mechanics of RSA encryption. Remember that calculating m^e
mod n is easy, but calculating the inverse c^-e mod n is very difficult, well, for large n's anyway.
However, if we can factor n into its prime factors p and q, the solution becomes easy again, even for
large n's. Obviously, if we can get hold of the secret exponent d, the solution is easy, too.

Key Generation Algorithm

1. Generate two large random primes, p and q, of approximately equal size such that their product n =
pq is of the required bit length, e.g. 1024 bits. [See note 1].

2. Compute n = pq and (φ) phi = (p-1)(q-1).

3. Choose an integer e, 1 < e < phi, such that gcd(e, phi) = 1. [See note 2].

4. Compute the secret exponent d, 1 < d < phi, such that ed ≡ 1 (mod phi). [See note 3].

21
5. The public key is (n, e) and the private key is (n, d). The values of p, q, and phi should also be kept
secret.

 n is known as the modulus.

 e is known as the public exponent or encryption exponent.

 d is known as the secret exponent or decryption exponent.

Encryption

Sender A does the following:-

1. Obtains the recipient B's public key (n, e).

2. Represents the plaintext message as a positive integer m [see note 4].

3. Computes the ciphertext c = m^e mod n.

4. Sends the ciphertext c to B.

Decryption

Recipient B does the following:-

1. Uses his private key (n, d) to compute m = c^d mod n.

2. Extracts the plaintext from the integer representative m.

Source Code:

import java.util.*;
import java.io.*;
public class rsa
{ static int gcd(int m,int n)
{ while(n!=0)
{ int r=m%n;
m=n;
n=r;
}
return m;
}
public static void main(String args[])
{
int p=0,q=0,n=0,e=0,d=0,phi=0;
int nummes[]=new int[100];
int encrypted[]=new int[100];
int decrypted[]=new int[100];
int i=0,j=0,nofelem=0;
Scanner sc=new Scanner(System.in);
String message ;
System.out.println("Enter the Message tobe encrypted:");
message= sc.nextLine();
System.out.println("Enter value of p and q\n");
p=sc.nextInt();

22
q=sc.nextInt();
n=p*q;
phi=(p-1)*(q-1);
for(i=2;i<phi;i++)
if(gcd(i,phi)==1) break;
e=i;
for(i=2;i<phi;i++)
if((e*i-1)%phi==0)
break;
d=i;
for(i=0;i<message.length();i++)
{
char c = message.charAt(i);
int a =(int)c;
nummes[i]=c-96;
}
nofelem=message.length();
for(i=0;i<nofelem;i++)
{
encrypted[i]=1;
for(j=0;j<e;j++)
encrypted[i] =(encrypted[i]*nummes[i])%n;
}
System.out.println("\n Encrypted message\n");
for(i=0;i<nofelem;i++)
{
System.out.print(encrypted[i]);
System.out.print((char)(encrypted[i]+96));
}
for(i=0;i<nofelem;i++)
{ decrypted[i]=1;
for(j=0;j<d;j++)
decrypted[i]=(decrypted[i]*encrypted[i])%n;
}
System.out.println("\n Decrypted message\n ");
for(i=0;i<nofelem;i++)
System.out.print((char)(decrypted[i]+96));
return;
}
}

OUTPUT
Enter the text:
hello
Enter the value of P and Q :
5
7
Encrypted Text is: 8 h 10 j 17 q 17 q 15 o
Decrypted Text is: hello

23
Experiment No:10

AIM: Write a program for distance vector algorithm to find suitable path for transmission.

Theory

Routing algorithm is a part of network layer software which is responsible for deciding which output
line an incoming packet should be transmitted on. If the subnet uses datagram internally, this decision
must be made anew for every arriving data packet since the best route may have changed since last
time.

If the subnet uses virtual circuits internally, routing decisions are made only when a new established
route is being set up. The latter case is sometimes called session routing, because a rout remains in
force for an entire user session (e.g., login session at a terminal or a file).

Routing algorithms can be grouped into two major classes: adaptive and nonadaptive. Nonadaptive
algorithms do not base their routing decisions on measurement or estimates of current traffic and
topology. Instead, the choice of route to use to get from I to J (for all I and J) is compute in advance,
offline, and downloaded to the routers when the network ids booted. This procedure is sometime
called static routing.

Adaptive algorithms, in contrast, change their routing decisions to reflect changes in the topology, and
usually the traffic as well. Adaptive algorithms differ in where they get information (e.g., locally,
from adjacent routers, or from all routers), when they change the routes (e.g., every ∆T sec, when the
load changes, or when the topology changes), and what metric is used for optimization (e.g., distance,
number of hops, or estimated transit time).

Two algorithms in particular, distance vector routing and link state routing are the most popular.
Distance vector routing algorithms operate by having each router maintain a table (i.e., vector) giving
the best known distance to each destination and which line to get there. These tables are updated by
exchanging information with the neighbors.

The distance vector routing algorithm is sometimes called by other names, including the distributed
Bellman-Ford routing algorithm and the Ford-Fulkerson algorithm, after the researchers who
developed it (Bellman, 1957; and Ford and Fulkerson, 1962). It was the original ARPANET routing
algorithm and was also used in the Internet under the RIP and in early versions of DECnet and
Novell’s IPX. AppleTalk and Cisco routers use improved distance vector protocols.

24
In distance vector routing, each router maintains a routing table indexed by, and containing one entry
for, each router in subnet. This entry contains two parts: the preferred out going line to use for that
destination, and an estimate of the time or distance to that destination. The metric used might be
number of hops, time delay in milliseconds, total number of packets queued along the path, or
something similar. The router is assumed to know the “distance” to each of its neighbor. If the metric
is hops, the distance is just one hop. If the metric is queue length, the router simply examines each
queue. If the metric is delay, the router can measure it directly with special ECHO packets hat the
receiver just time stamps and sends back as fast as possible.

The Count to Infinity Problem.

Distance vector routing algorithm reacts rapidly to good news, but leisurely to bad news. Consider a
router whose best route to destination X is large. If on the next exchange neighbor A suddenly reports
a short delay to X, the router just switches over to using the line to A to send traffic to X. In one
vector exchange, the good news is processed.

To see how fast good news propagates, consider the five node (linear) subnet of following figure,
where the delay metric is the number of hops. Suppose A is down initially and all the other routers
know this. In other words, they have all recorded the delay to A as infinity.

ABCDEABCDE

_____ _____ _____ _____ _____ _____ _____ _____

∞ ∞ ∞ ∞ Initially 1 2 3 4 Initially

1 ∞ ∞ ∞ After 1 exchange 3 2 3 4 After 1 exchange

1 2 ∞ ∞ After 2 exchange 3 3 3 4 After 2 exchange

1 2 3 ∞ After 3 exchange 5 3 5 4 After 3 exchange

1 2 3 4 After 4 exchange 5 6 5 6 After 4 exchange

7 6 7 6 After 5 exchange

7 8 7 8 After 6 exchange

∞∞∞∞

Many ad hoc solutions to the count to infinity problem have been proposed in the literature, each one
more complicated and less useful than the one before it. The split horizon algorithm works the same
way as distance vector routing, except that the distance to X is not reported on line that packets for X
are sent on (actually, it is reported as infinity). In the initial state of right figure, for example, C tells D
the truth about distance to A but C tells B that its distance to A is infinite. Similarly, D tells the truth
to E but lies to C.

Program

#include <conio.h>
#include <iostream.h>

25
#define MAX 10
int n;
class router {
char adj_new[MAX], adj_old[MAX];
int table_new[MAX], table_old[MAX];
public:
router(){
for(int i=0;i<MAX;i++) table_old[i]=table_new[i]=99;
}
void copy(){
for(int i=0;i<n;i++) {
adj_old[i] =adj_new[i];
table_old[i]=table_new[i];
}
}
int equal() {
for(int i=0;i<n;i++)
if(table_old[i]!=table_new[i]||adj_new[i]!=adj_old[i])return 0;
return 1;
}
void input(int j) {
cout<<"Enter 1 if the corresponding router is adjacent to router"
<<(char)('A'+j)<<" else enter 99: "<<endl<<" ";
for(int i=0;i<n;i++)
if(i!=j) cout<<(char)('A'+i)<<" ";
cout<<"\nEnter matrix:";
for(i=0;i<n;i++) {
if(i==j)
table_new[i]=0;
else
cin>>table_new[i];
adj_new[i]= (char)('A'+i);
}
cout<<endl;
}
void display(){
cout<<"\nDestination Router: ";
for(int i=0;i<n;i++) cout<<(char)('A'+i)<<" ";
cout<<"\nOutgoing Line: ";
for(i=0;i<n;i++) cout<<adj_new[i]<<" ";
cout<<"\nHop Count: ";
for(i=0;i<n;i++) cout<<table_new[i]<<" ";
}
void build(int j) {
for(int i=0;i<n;i++)
for(int k=0;(i!=j)&&(k<n);k++)
if(table_old[i]!=99)
if((table_new[i]+r[i].table_new[k])<table_new[k]) {
table_new[k]=table_new[i]+r[i].table_new[k];
adj_new[k]=(char)('A'+i);
}
}
} r[10];
void build_table() {
int i=0, j=0;

26
while(i!=n) {
for(i=j;i<n;i++) {
r[i].copy();
r[i].build(i);
}
for(i=0;i<n;i++)
if(!r[i].equal()) {
j=i;
break;
}

}
}
void main() {
clrscr();
cout<<"Enter the number the routers(<"<<MAX<<"): "; cin>>n;
for(int i=0;i<n;i++) r[i].input(i);
build_table();
for(i=0;i<n;i++) {
cout<<"Router Table entries for router "<<(char)('A'+i)<<":-";
r[i].display();
cout<<endl<<endl;
}
getch();
}

Sample Output
Enter the number the routers: 5
Enter 1 if the corresponding is adjacent to router A else enter 99:
BCDE
Enter matrix:1 1 99 99
Enter 1 if the corresponding is adjacent to router B else enter 99:
ACDE
Enter matrix:1 99 99 99
Enter 1 if the corresponding is adjacent to router C else enter 99:
ABDE
Enter matrix:1 99 1 1
Enter 1 if the corresponding is adjacent to router D else enter 99:
ABCE
Enter matrix:99 99 1 99
Enter 1 if the corresponding is adjacent to router E else enter 99:
ABCD
Enter matrix:99 99 1 99
Router Table entries for router A
Destination Router: A B C D E
Outgoing Line: A B C C C
Hop Count: 0 1 1 2 2
Router Table entries for router B
Destination Router: A B C D E
Outgoing Line: A B A A A
Hop Count: 1 0 2 3 3
Router Table entries for router C
Destination Router: A B C D E
Outgoing Line: A A C D E
Hop Count: 1 2 0 1 1

27
Router Table entries for router D
Destination Router: A B C D E
Outgoing Line: C C C D C
Hop Count: 2 3 1 0 2
Router Table entries for router E
Destination Router: A B C D E
Outgoing Line: C C C C E
Hop Count: 2 3 1 2 0

28

You might also like