Sie sind auf Seite 1von 77

Syllabus Programs ( JNTU )

S.No

Programs

Page No

Implement the data link layer framing method such as bit stuffing.

Implement the data link layer framing method such as Character


Stuffing.

11

Implement on a data set of characters the CRC Polynomial


CRC 12

14

Implement on a data set of characters the CRC Polynomial


CRC 16

19

Implement Dijkstras algorithm to compute the Shortest path thru


a graph.

25

Take an example subnet graph with weights indicating delay


between nodes.
Now obtain Routing table art each node using distance vector
routing algorithm.
Take an example subnet of hosts. Obtain broadcast tree for it.

33

Take a 64 bit playing text and encrypt the same using DES
algorithm.

49

Using RSA algorithm Encrypt a text data and Decrypt the same.

67

40

SYLLABUS PROGRAMS
AIM
Implement the data link layer framing method such as bit stuffing.
Algorithm:
1.
2.
3.
4.
5.
6.

start
Read a bit string
Trace 5 bits in the string
Check if 5 bits are 11111 then shift a 0 bit after it
Repeat 3 & 4 until end of string
Stop

FLOWCHART
Start

Read a bit
string
Trace five bits in the string

bits are
11111

Stuff a 0 after it

End of
string

Print the stuffed string

stop

PROGRAM
#include<stdio.h>
#include<string.h>

main()
{
char a[20],fs[50]="",t[6],r[5];
int i,j,p=0,q=0;
clrscr();
printf("enter bit string : ");
scanf("%s",a);
strcat(fs,"01111110");
if(strlen(a)<5)
{
strcat(fs,a);
}
else
{
for(i=0;i<strlen(a)-4;i++)
{
for(j=i;j<i+5;j++)
{
t[p++]=a[j];
}
t[p]='\0';
if(strcmp(t,"11111")==0)
{
strcat(fs,"111110");
i=j-1;
}
else
{
r[0]=a[i];
r[1]='\0';
strcat(fs,r);
}
p=0;
}

for(q=i;q<strlen(a);q++)
{
t[p++]=a[q];
4

}
t[p]='\0';
strcat(fs,t);
}
strcat(fs,"01111110");
printf("After stuffing : %s",fs);
getch();
}

text cases
1. for given input data the expected output in different ways
OUTPUT
Enter bit string : 10101111110

After stuffing : 0111111010101111101001111110


Enter bit string : 1011111011110111110
After stuffing : 0111111010111110011110111110001111110

AIM
Implement the data link layer framing method such as Character Stuffing.

Algorithm:
1. Start
2. Read a string , starting & ending delimiter
3. Trace a character in the string
4. If character is same as starting & ending delimiter
5. Repeat 3 & 4 until end of string
6. Stop

FLOWCHART
start

Read a string ,
starting and
ending
delimiter
Trace a character in the
string

Character
is same
as
starting
or ending
delimiter

Add the same character next


to it that is stuff a character

End of
string

Print the stuffed string

stop

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("\nAfter stuffing : %s",fs);
getch();
}

OUTPUT
Enter characters to be stuffed : goodday
Enter a character that represents starting delimiter : d
Enter a character that represents ending delimiter : g
After stuffing : dggooddddayg

AIM
Implement on a data set of characters the CRC Polynomial
CRC 12.
Algorithm:
1. Start
2. Read value for both transmitted and received data
3. Concatenate 12 0s at the transmitted data
4. Perform binary division where crc-12 generating polynomial, is
the divisor and dividend is concatenated string
5. If length of the remainder is 12 replace the 12 0s with the obtained
crc otherwise add required no of 0s to the beginning of the remainder
such that length is 12
6. Perform binary division on this divisor as generating polynomial.
7. If remainder is 0 transmitted data is correct otherwise incorrect

10

FLOW CHART

11

Start

Read values of
both transmitted
and received data

Concatenate twelve 0s at
the end of transmitted data

Perform binary division where


CRC12 generating polynomial is
the divisor and dividend is the
concatenated string

Length of
remainde
r ==12

Add required number of


0s to the beginning of
the remainder such that
length is 12

Replace the twelve 0s with the


obtained CRC

Perform binary division on this with


the divisor as generating polynomial

12

Rema
inder
==0

Printf message
data transmitted
incorrectly

Print meaasge
data transmitted
correctly

stop

13

PROGRAM

#include<stdio.h>
const char * bindiv(const char *,const char *);
const char * binsub(const char *,const char *);
int f=0,ll=0;
main()
{
char *a,p[13]="1100000001011",g[30],g1[30],yy[30]="",td[30],*aa;
int l=0,i;
clrscr();
printf("enter transfered data : ");
scanf("%s",g);
printf("enter received data : ");
scanf("%s",td);
strcpy(g1,g);
strcat(g,"000000000000");
printf("\n%s ) %s (",p,g);
a=bindiv(g,p);
if(strlen(a)<12)
{
for(i=strlen(a);i<12;i++)
{
yy[l++]='0';
}
yy[l]='\0';
}
strcat(yy,a);
strcat(g1,yy);
printf("\ncrc is
%s",yy);
printf("\n
------------------");
strcat(td,yy);
printf("\n\n%s ) %s (",p,td);
ll=0;
aa=bindiv(td,p);
strcpy(a,aa);
printf("\n
%s",a);
printf("\n
-----------------");
if(f==1)
printf("\ndata transfered correctly");
else
printf("\ndata transfered incorrectly");
getch();
14

}
const char * bindiv(const char *s,const char *d)
{
int i,j,k=0,x=13,h,p=0,l;
char q[15]="",b[30],*w;
for(i=0;i<strlen(s);i++)
{
if((i+x)>strlen(s))
x=(i+x)-strlen(s)+1;
for(j=i;j<(i+x);j++)
{
b[k++]=s[j];
}
b[k]='\0';
if(ll!=0)
printf("\n
%s",b);
ll=1;
if(strlen(b)==12)
{
break;
}
printf("\n
%s",d);
printf("\n
-----------------");
w=binsub(b,d);
k=0;i=j-1;
for(l=0;l<strlen(w);l++)
{
if(w[l]=='1')
break;
}
if(l==strlen(w))
{
f=1;
return(w);
}
for(h=l;h<strlen(w);h++)
{
q[p++]=w[h];
}
q[p]='\0';
x=13-strlen(q);
strcpy(b,"");
strcat(b,q);
k=strlen(q); p=0;
15

}
return(b);
}
const char * binsub(const char *x,const char *y)
{
int i,j=0;
char w[15]="",e[3],f[3],n[3];
e[0]='1';
e[1]='\0';
f[0]='0';
f[1]='\0';
for(i=0;i<strlen(x);i++)
{
if((x[i]=='1')&&(y[i]=='1'))
strcat(w,f);
else
if((x[i]=='0')&&(y[i]=='0'))
strcat(w,f);
else
strcat(w,e);
}
n[0]='\0';
n[1]='\0';
strcat(w,n);
return(w);
}

16

AIM
Implement on a data set of characters the CRC Polynomial
CRC 16
Algorithm:
1.

Start

2. Read value for both transmitted and received data


3. Concatenate 16 0s at the transmitted data
4. Perform binary division where crc-16 generating polynomial, is
the divisor and dividend is concatenated string
5. If length of the remainder is 16 replace the 16 0s with the obtained
crc otherwise add required no of 0s to the beginning of the remainder
such that length is 16
6. Perform binary division on this divisor as generating polynomial.
7. If remainder is 0 transmitted data is correct otherwise incorrect

17

FLOW CHART
Start

Read values of
both transmitted
and received data

Concatenate sixteen 0s at
the end of transmitted data

Perform binary division where


CRC16 generating polynomial is
the divisor and dividend is the
concatenated string

Length of
remainde
r ==16

Add required number of


0s to the beginning of
the remainder such that
length is 16

Replace the sixteen 0s with the


obtained CRC

Perform binary division on this with


the divisor as generating polynomial

18

Rema
inder
==0

Printf message
data transmitted
incorrectly

Print meaasge
data transmitted
correctly

stop

19

PROGRAM

#include<stdio.h>
const char * bindiv(const char *,const char *);
const char * binsub(const char *,const char *);
int f=0,ll=0;
main()
{
char *a,p[20]="10001000000100001",
g[30],g1[30],yy[30]="",td[30],*aa;
int l=0,i;
clrscr();
printf("enter transfered data : ");
scanf("%s",g);
printf("enter received data : ");
scanf("%s",td);
strcpy(g1,g);
strcat(g,"0000000000000000");
printf("\n%s ) %s (",p,g);
a=bindiv(g,p);
if(strlen(a)<16)
{
for(i=strlen(a);i<16;i++)
{
yy[l++]='0';
}
yy[l]='\0';
}
strcat(yy,a);
strcat(g1,yy);
printf("\n
------------------");
printf("\ncrc is
%s",yy);
strcat(td,yy);
printf("\n\n%s ) %s (",p,td);
ll=0;
aa=bindiv(td,p);
strcpy(a,aa);
printf("\n
%s",a);
printf("\n
-----------------");
if(f==1)
printf("\ndata transfered correctly");
else
printf("\ndata transfered incorrectly");
getch();
20

}
const char * bindiv(const char *s,const char *d)
{
int i,j,k=0,x=17,h,p=0,l;
char q[25]="",b[30],*w;
for(i=0;i<strlen(s);i++)
{
if((i+x)>strlen(s))
x=(i+x)-strlen(s)+1;
for(j=i;j<(i+x);j++)
{
b[k++]=s[j];
}
b[k]='\0';
if(ll!=0)
printf("\n
ll=1;
if(strlen(b)==16)
{
break;
}
printf("\n
printf("\n
w=binsub(b,d);

%s",b);

%s",d);
-----------------");

k=0;i=j-1;
for(l=0;l<strlen(w);l++)
{
if(w[l]=='1')
break;
}
if(l==strlen(w))
{
f=1;
return(w);
}
for(h=l;h<strlen(w);h++)
{
q[p++]=w[h];
}
q[p]='\0';
x=17-strlen(q);
strcpy(b,"");
strcat(b,q);
k=strlen(q); p=0;
21

}
return(b);
}
const char * binsub(const char *x,const char *y)
{
int i,j=0;
char w[25]="",e[3],f[3],n[3];
e[0]='1';
e[1]='\0';
f[0]='0';
f[1]='\0';
for(i=0;i<strlen(x);i++)
{
if((x[i]=='1')&&(y[i]=='1'))
strcat(w,f);
else
if((x[i]=='0')&&(y[i]=='0'))
strcat(w,f);
else
strcat(w,e);
}
n[0]='\0';
n[1]='\0';
strcat(w,n);
return(w);
}

22

OUTPUT
Enter transfered data : 11011

Enter received data

: 11011

10001000000100001 ) 110110000000000000000 (
10001000000100001
------------------------10100000001000010
10001000000100001
-------------------------10100000110001100
10001000000100001
-------------------------1010001101011010
-------------------------crc is
1010001101011010
10001000000100001 ) 110111010001101011010 (
10001000000100001
-------------------------10101010000101001
10001000000100001
-------------------------10001000000100001
10001000000100001
-------------------------00000000000000000
-------------------------Data transfered correctly

23

AIM
Implement Dijkstras algorithm to compute the Shortest path through a graph.
Algorithm:

1. Start
2. Read data from the user:
nodes=no. of nodes;
dsp[nodes][nodes]=adjacency matrix of the graph;
src=source;
dest=destination
3. two struct arrays permanent, temp are used to store the permanent

and tentavie nodes.


A function sort is used to sort the temp array in decreasing order.
4. permanent[0].src=src;

permanent[0].dest=src;
permanent[0].length=0;
5. store all the neighbouring nodes to src in temp
7. while temp array is not null, then A otherwise for the node recently
added to permanent list,
8. find the neighbouring nodes.
9. if a node is not already present in the temp add the node to the temp
array , otherwise if the length of the node is smaller than the previous
value then update the temp to the new value, else ignore the new value
10. find the destination node in the permanent array. trace the path to
the source and store it an array.
11. display the shortest path from src to dest and the total delay to reach
the dest.
12. sort temp each item is compared to all the other items.
13. if the item is lesser than swap place the last item i.e., the node with
less delay in the permanent array
14. Return
24

FLOW CHART
start
read data from the user:
nodes=no. of nodes;
dsp[nodes][nodes]=adjacency matrix of
the graph;src=source;dest=destination
two struct arrays permanent, temp are used to
store the permanent and tentavie nodes.
A function sort is used to sort the temp
array in decreasing order.
permanent[0].src=src;
permanent[0].dest=src;
permanent[0].length=0;
store all the neighbouring nodes to src in temp
SORT
while temp array is not null

for the node recently added to permanent list,


find the neighbouring nodes.
if a node is not already
present in the temp

25

yes

no

add the node to the temp array

if the length of the node is


smaller than the previous
value then update the temp
to the new value, else ignore
the new value
sort

find the destination node in the permanent


array. trace the path to the source and
store it an array
display the shortest path from src
to dest and the total delay to reach
the dest.
End

26

sort
Bubble Sort Technique:
sort temp
each item is compared to all the other
items. if the item is lesser than swap

place the last item i.e., the node with less


delay in the permanent array.
Return

27

PROGRAM
#include<stdio.h>
void sort(void);
static int dsp[10][10],nodes;
struct{
char src;
char dest;
int length;
}stemp,permanent[10]={' ',' ',0},temp[10]={' ',' ',-1};
static int perm,tem;
void main()
{
int i,j,k,l,m,n=0,point;
char initial,dest,path[10]={' '};
clrscr();
printf("\t\t Shortest Path (Dijkstra's algorithm)");
printf("\n*****************************************************
**");
printf(\nEnter the number of nodes:);
scanf(%d,&nodes);
printf(\nEnter the adjacency matrix for the graph:\n);
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
scanf(%d,&dsp[I][j]);
}
fflush(stdin);
printf("\n enter the source node:");
scanf("%c",&initial);fflush(stdin);
printf("\n Enter the destination node:");
scanf("%c",&dest);
permanent[perm].src=initial;
permanent[perm].dest=initial;
permanent[perm++].length=0;
i=permanent[perm-1].dest-97;
for(j=0;j<nodes;j++)
{
if(i!=j)
{
28

if(dsp[i][j]>0)
{
temp[tem].src=permanent[perm-1].src;
temp[tem].dest=j+97;
temp[tem++].length=dsp[i][j];
}
}
}
sort();
while(tem>=0)
{
j=permanent[perm-1].dest-97;
for(i=0;i<nodes;i++)
{
if(i!=initial-97)
{
if(dsp[j][i]>0)
{
l=-1;
for(k=0;k<perm;k++)
{
if(permanent[k].dest==(i+97))
l=k;
}
for(k=0;k<=tem;k++)
{
if(temp[k].dest==(i+97))
l=k;
}
if(l<0)
{
temp[tem].src=j+97;
temp[tem].dest=i+97;
for(m=0;m<perm;m++)
{
if(permanent[m].dest==temp[tem].src)
n=permanent[m].length;
}
temp[tem++].length=dsp[j][i]+n;
}
else
{
for(m=0;m<perm;m++)
{
if(permanent[m].dest==j+97)
{
29

n=permanent[m].length+dsp[j][i];break;
}
else
n=dsp[j][i];
}
if((n<temp[l].length))
{
temp[l].length=n;
temp[l].src=j+97;
temp[l].dest=i+97;
}
}
}
}
}
sort();
}
printf("\nShortest path:\n");
printf("From %c to %c is:",initial,dest);
for(i=0;i<perm-1;i++)
{
if(permanent[i].dest==dest)
{
point=i;n=i;
break;
}
} i=0;
for(j=perm;j>0;j--)
{
if(permanent[j-1].dest==permanent[point].src)
{
path[i++]=permanent[point].dest;
point=j-1;
}
}
path[i]=initial;
for(j=i;j>=0;j--)
printf("%c ",path[j]);
printf("\t length=%d",permanent[n].length);
getch();
}
void sort()
{
int i,j,k;
for(i=0;i<=tem;i++)
{
30

k=1;
for(j=0;j<=tem;j++)
{
if((temp[j].length <= temp[j+1].length))
{
stemp=temp[j];
temp[j]=temp[j+1];
temp[j+1]=stemp;
k=0;
}
}
if(k)
break;
}
permanent[perm++]=temp[tem-1];
temp[tem-1].src=' ';temp[tem-1].dest=' ';
temp[tem-1].length=-1; tem--;
}

Network topology:
------------------------

1
1

1
2

2
d

Output of execution1:
---------------------------Shortest Path (Dijkstra's algorithm)
*******************************************************************
Enter the number of nodes:6
Enter the adjacency matrix for the graph(-1 for no edge):
0 1 -1 2 -1 -1
1 0 1 -1 -1 -1
-1 1 0 1 1 -1
2 -1 1 0 2 -1
-1 -1 1
2 0 3
-1 -1 -1 -1 3 0
Enter the source node:a
Enter the destination node:f

31

Shortest path:
From a to f is:a b c e f

length=6

Network Topology:

3
E

1
G

D
2

2
H

Output of execution2:
---------------------------Shortest Path (Dijkstra's algorithm)
*******************************************************************
Enter the number of nodes:8
Enter the adjacency matrix for the graph(-1 for no edge):
Enter the adjacency matrix for the graph
0 2 -1 -1 -1 -1 6 -1
2 0 7 -1 2 -1 1 -1
-1 7 0 3 -1 3 -1 -1
-1 -1 3 0 -1 -1 -1 2
-1 2 -1 -1 0 2 1 -1
-1 -1 3 -1 2 0 -1 2
6 -1 -1 -1 1 -1 0 4
-1 -1 -1 2 -1 2 4 0
Enter the source node:a
Enter the destination node:d
Shortest path:
From a to d is:a b e f h d

length=10

32

AIM
Take an example subnet graph with weights indicating delay between nodes.
Now obtain Routing table art each node using distance vector routing algorithm.
Algorithm:
1. Start
2. Read data from user
nodes=no.of nodes

src=node for which distance vector is to be computed;


neighb=no. of neighbours to src;
neigh[]={src,neighbouring node names};
topology[nodes][nodes]=distance vectors from all neighbours are
read from user and stored. for nodes that are not neighbours
infinity is stored as the value. dv[] stores the distance vector of src
3. For I = 0 To Nodes

4. if(I==neigh[n])
5. add1=topology[src][I];
add add1 to the distance vector of I, otherwise go to 3
6. compare the values in distace vector of the source.
to that of I .
8. If the value in distance vector of I less than the original then store
the new value and the line to use as I.
9. Display the distance vector of src
10. Stop

33

FLOW CHART

START
read data from user:
nodes=no. of nodes;src=node for which distance vector
is to be computed;neighb=no. of neighbours to src;
neigh[]={src,neighbouring node names};
topology[nodes][nodes]=distance vectors from all neighbours
are read from user and stored. for nodes that are not neighbours
infinity is stored as the value. dv[] stores the distance vector of src
for(I=0;I<nodes;I++)

if(I==neigh[n])
yes
add1=topology[src][I];
add add1 to the distance vector of I

compare the values in distace vector of the source.


to that of I .if the value in distance vector of I less
than the original then store the new value and the
line to use as I .

display the distance vector


of src
end

34

PROGRAM
#include<stdio.h>
#define e 10000
#define nodes1 40
int topology[nodes1][nodes1];
static int l,n,neighb,nodes;
static char neigh[nodes1];
struct {
char name;
int delay;
} dv[nodes1];
void main()
{
int i,j,k[nodes1*nodes1],add=0,src1;
char src='a';
clrscr();
printf("\t\t\tDistance Vector Routing\n");
printf("*****************************************************
******");
printf("\nEnter the number of nodes:");
scanf("%d",&nodes);fflush(stdin);
printf("\nEnter the node for which Distance Vector table is needed:");
scanf("%c",&src);fflush(stdin);
printf("\nEnter the no. of neighbours to %c:",src);
scanf("%d",&neighb); fflush(stdin);
printf("\nenter the names of the neighbours(in alphabetic order):");
neigh[0]=src;
for(i=1;i<neighb+1;i++)
{
scanf("%c",&neigh[i]);
fflush(stdin);
}
printf("\n enter the distance vectors of the source and the neighbouring
nodes\n");
printf("starting with source,then with neighbouring nodes in alphabetical
order:\n");
for(i=0;i<nodes*(neighb+1);i++)
{
scanf("%d",&k[i]);
35

}
src1=src;
l=nodes;n=1;
for(i=0;i<nodes;i++)
{
if(i==src1-97)
{
for(j=0;j<nodes;j++)
{
if(k[j]<0)
topology[i][j]=e;
else
topology[i][j]=k[j];
}
}
else if(i==neigh[n]-97)
{
for(j=0;j<nodes;j++)
{ if(k[l]<0)
topology[i][j]=e;
else
topology[i][j]=k[l];
l++;
} n++;
}
else
{
for(j=0;j<nodes;j++)
topology[i][j]=e;
}
}
i=src1-97;
for(j=0;j<nodes;j++)
{ dv[j].name=src1;
dv[j].delay=topology[i][j];
}
k[nodes*nodes]=e; n=1;
for(i=0;i<nodes;i++)
{
if((i==neigh[n]-97))
{
add=topology[src1-97][i];
for(j=0;j<nodes;j++)
{
k[j]=add+topology[i][j];
36

}
for(j=0;j<nodes;j++)
{
if(k[j]<dv[j].delay)
{
dv[j].name=i+97;
dv[j].delay=k[j];
}
}
if(i!=src1-97)
n++;
printf("\n%c's distance vector after receiving %c's vector:\n",src1,i+97);
for(j=0;j<nodes;j++)
printf("To %c:\tfrom: %c \t %d\n",j+97,dv[j].name,dv[j].delay);
getch();
}
}
}

Network Graph for Output1:


B
A

D
C

Output from first execution:


-----------------------------------Distance Vector Routing
***************************************************************
*******
Enter the number of nodes:4
Enter the node for which Distance Vector table is needed:d
Enter the no. of neighbours to d:2

37

enter the names of the neighbours(in alphabetic order):b


c
enter the distance vectors of the source and the neighbouring nodes
starting with source,then with neighbouring nodes in alphabetical order:
-1 1 2 0
1011
1102
d's distance vector after receiving b's vector:
To a: from: b
2
To b: from: d
1
To c: from: d
2
To d: from: d
0
d's distance vector after receiving c's vector:
To a: from: b
2
To b: from: d
1
To c: from: d
2
To d: from: d
0
Network Graph for Output2:
A

Output from second execution:


---------------------------------------Distance Vector Routing
***************************************************************
*******
Enter the number of nodes:12
Enter the node for which Distance Vector table is needed:j
Enter the no. of neighbours to j:4
enter the names of the neighbours(in alphabetic order):a
h
i
k

38

enter the distance vectors of the source and the neighbouring nodes
starting with source,then with neighbouring nodes in alphabetical order:
8 -1 -1 -1 -1 -1 -1 12 10 0 6 -1
0 12 25 40 14 23 18 17 21 9 24 29
20 31 19 8 30 19 6 0 14 7 22 9
24 36 18 27 7 20 31 20 0 11 22 33
21 28 36 24 22 40 31 19 22 10 0 9
j's distance vector after receiving a's vector:
To a: from: j
8
To b: from: a
20
To c: from: a
33
To d: from: a
48
To e: from: a
22
To f: from: a
31
To g: from: a
26
To h: from: j
12
To i: from: j
10
To j: from: j
0
To k: from: j
6
To l: from: a
37
j's distance vector after receiving h's vector:
To a: from: j
8
To b: from: a
20
To c: from: h
31
To d: from: h
20
To e: from: a
22
To f: from: a
31
To g: from: h
18
To h: from: j
12
To i: from: j
10
To j: from: j
0
To k: from: j
6
To l: from: h
21
j's distance vector after receiving i's vector:
To a: from: j
8
To b: from: a
20
To c: from: i
28
To d: from: h
20
To e: from: i
17
To f: from: i
30
To g: from: h
18
To h: from: j
12
To i: from: j
10
39

To j: from: j
To k: from: j
To l: from: h

0
6
21

j's distance vector after receiving k's vector:


To a: from: j
8
To b: from: a
20
To c: from: i
28
To d: from: h
20
To e: from: i
17
To f: from: i
30
To g: from: h
18
To h: from: j
12
To i: from: j
10
To j: from: j
0
To k: from: j
6
To l: from: k
15

40

AIM
Take an example subnet of hosts. Obtain broadcast tree for it.
Algorithm:
1. Start
2. Read data from user
No. of nodes of graph : n
Adjacency matrix with weights :
Adj [n] [n], source for broadcast : src
3. If (i<n) then
4. If(j<n) then
distance[j] = adj [root][j] otherwise Mini: min value in the distance[n]
5. If(k<n) then
when distance[k]=mini otherwise go to 3
6. when distance[k]=mini
spantre[i][k]=mini
spantre[i][k]=mini otherwise spantre[i][k]=0
7. Count ! = y, If (i<n) , otherwise stop
8. spantre[root]

41

FLOW CHART
START

No. of nodes of graph : n


Adjacency matrix with weights :
Adj [n] [n], source for broadcast : src

i<n
yes
no
j<n

distance[j] = adj [root][j]


A
Mini: min value in the distance[n]
yes
k<n
no
when
distance[k]=
mini
no

yes

spantre[i][k]=mini
spantre[k][i]=mini

42

spantre[i] [k]=0

Count ! = y

no

yes

stop

i<n
yes
spantre[root]

no

yes
recir[root]
is 1

no

yes
no
recir [j] not 1
yes

increment i by 1

Sent message from root to i

no

recir[ ] for
yes
all nodes=1
43

count = y

PROGRAM
#include<stdio.h>
int max();
int distance[20];
int n;
main()
{
int adj[20][20],adj1[20][20],flag[30];
int i,j,root,x;
int source,count=1,y=0;
printf("enter no of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&adj[i][j]);
}
}
printf("enter the source for broadcasting");
scanf("%d",&source);
for(i=0;i<n;i++)
{
flag[i]=0;
}
for(root=0;root<n;root++)
{
for(i=0;i<n;i++)
{
44

distance[i]=adj[root][i];
}
x=min();
for(i=0;i<n;i++)
{
if(distance[i]==x)
{
adj1[root][i]=x;
adj1[i][root]=x;
}
else
{
adj1[root][i]=0;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(adj1[i][j]!=0)
{
adj1[j][i]=adj[i][j];
}
}
}
printf("given adjacency matrix is");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d",adj[i][j]);
}
printf("\n");
}
printf("minimal spanning tree");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",adj1[i][j]);
}
45

printf("\n");
}
root=source;
flag[root]=1;

while(count!=y)
{
for(i=0;i<n;i++)
{
if(adj1[root][i]!=0 && flag[root]==1 && flag[i]!=1)
{
printf("%d sends message to %d \n",root,i);
flag[i]=1;
}
}
if(root<n-1)
{
root++;
}
else
{
root=0;
}
for(i=0;i<n;i++)
{
if(flag[i]==0)
{
break;
}
}
if(i==n)
{
count=y;
}
}
}
int min()
{
int i,j=0;
int mini;
int distance1[10];
for(i=0;i<n;i++)
{
if(distance[i]!=0)
46

{
distance1[j]=distance[i];
j++;
}
}
mini=distance1[0];
for(i=1;i<j;i++)
{
if(distance1[i]<mini)
{
mini=distance1[i];
}
}
return(mini);
}

OUTPUT
enter no of nodes2
enter the adjacency matrix
02
20
enter the source for broadcasting1
given adjacency matrix is
02
20
2

minimal spanning tree is


02
20
1 sends message to 0
0

enter no of nodes3
enter the adjacency matrix
012
105
250
enter the source for broadcasting2
given adjacency matrix is
012
105
250
47

5
2

minimal spanning tree is


012
100
200
0

2 sends message to 0
0 sends message to 1
enter no of nodes4
enter the adjacency matrix
0687
6050
8504
7040
enter the source for broadcasting2
given adjacency matrix is
0687
6050
8504
7040
4

3
7

8
0

5
1

minimal spanning tree is


0600
6050
0504
0040
3

2
48

4
5
6
2 sends message to 1
2 sends message to 3
1 sends message to 0

enter no of nodes5
enter the adjacency matrix
02050
20360
03079
56708
00980
enter the source for broadcasting2
given adjacency matrix is
02050
20360
03079
56708
00980
3

0
5

9
4

minimal spanning tree is


02050
20300
03000
50008
00080
2

0
5

49

2 sends message to 1
1 sends message to 0
0 sends message to 3
3 sends message to 4

enter the no of nodes6


enter the adjacency matrix
020760
203000

030400
704050
600507
000070
enter the source for broadcasting3
given adjacency matrix is
020760
203000
030400
704050
600507
000070
6
0
2

8
1

5
3

4
2

minimal spanning tree is


020000
203000
030400
004050
000507
000070
7
4

0
2

3
2

50

3 sends message to 2
3 sends message to 4
4 sends message to 5
2 sends message to 1
1 sends message to 0
64-bit key

64-bit plain text

Initial permutation

L(i-1)

Permutation choice 1

R(i-1)

AIM

Expansion/
Permutation
(E table)

C(i-1)

Left shift(s)

D(i-1)

Left shift

Permutation/
Take a 64 bit playing text and encrypt the same using DES algorithm.
XO
Contraction
R
(Permutated
FLOW CHART
choice 2)
Substitution choice
(S-box)

Permutation
C(i)

D(i)

XOR
L (i)
R (i)

i<=1
6
Exit

51

PROGRAM
#inc lude <s tdio. h>
#inc lude <s tring. h>
void ke yge n();
int len,x,y = 0,l[40],r[40],r1[48],str1[20], r2[48],i3,
tmp3 [64],tmp4[64];
int ip [] = {58,50,42,34,26,18,10,2,60,52,44,36,
28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16
,8,57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,61,53,4
5,37,29,21,13,5,63,55,47,39,31,23,15,7};

int e [] = {32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12,


13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 2
7, 28, 29, 28, 29, 30, 31, 32, 1};
int c 1[100];
int s [64];
int round=1;
int k [] = {0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
52

1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1};
int pc 1 [] = {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, 5
4, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4};
int pc 2[]={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 tmp5[8], fla g=0, t, t1, i, j=0, c 2[100],c [100], d[100],
ke y[100], k2[20][100], c ount=1;
int s 1[4][16]= {{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
{0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
{4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
{15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}};
int s 2[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 s 3[4][16]={ {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
{13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
{13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
{1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}};
int s 4[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 s 5[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 s 6[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 s 7[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 s 8[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 p[32]={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};
53

int g3, w [8], i4, d1=0, g, g1=0, g2,s iz e, c h1[8], a 1[4], b1[8][4],
b[8]
[6], i1, j1, k1=0, tz [4], tmp, tmp1, t11, te mp,z =0, m,c ount1=0, c 11[32]
, res [32];
int iip[]={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};
cha r c h[20];
int s tr[64];
void ma in()
{
c lrs c r();
printf("e nte r the s tring\n");
ge ts (c h);
s ize =s trle n(c h);
for(g=0;g <s iz e ;g++)
{
c h1[g]=c h[g];
}
for(g=0;g<s iz e ;g++)
{
g1=0;
while (c h1[g]>0)
{
w[g1++]=c h1[g] %2;
ch1[g]=c h1[g]/2;
}
for(g3=g1;g3 <8;g3++ )
w[g3]=0;
for(g2=7;g2 >=0;g2- -)
{
str[d1++ ]=w [g2 ];
}
}
for(g3=0;g3<64;g3+ +)
s [g3]=s tr[g3];
for(g2=0;g2 <64;g2++ )
printf("%d", s tr[g2]) ;
54

ke yge n(); /*16 ke y ge ne ra tion*/


/*initia l pe rmuta tion*/
for(x=0;x <64;x++ )
{
c 1[x]=s tr[ip[x ]- 1];
}
for(x=0;x<64;x+ +)
{
printf("%d", c 1[x] );
if(((x %7)= =0)&&(x !=0) )
printf("\n" );
}
/*dividing the w hole s tring into l & r*/
for(x=0;x<32;x+ +)
l[x]=c 1[x];
y=0;
for(x=32;x<64;x+ +)
r[y++] =c 1[x];
/*round sta rts he re */
while (round<=16)
{
/*e xpa ns ion pe rmuta tion e ta ble */
for(x=0;x<48;x+ +)
{
r1[x]=r[e [x ]- 1];
}
printf("r1: ");
for(x=0;x<48;x+ +)
printf("%d", r1 [x]);
printf("\n" );
/*xor r1 w ith ke y*/
for(x=0;x<48;x+ +)
{
if(r1[x]= =k2[round ][x] )
r2[x]=0;
e ls e
r2[x]=1;
}
printf("\nke y");
for(x=0;x<48;x+ +)
55

printf("%d", k2[round] [x]) ;


printf("\n");
for(x=0;x<48;x+ +)
printf("%d", r2[x] );
/*s ubs titution s -box*/
k1=0;
for(i1=0;i1 <8;i1+ +)
{
for(j1=0;j1<6;j1 ++)
{
b[i1][j1]=r2 [k1++ ];
}
}
c ount1=0;
for(i1=0;i1 <8;i1+ +)
{
c ount1++;
tz [0]=b[i1] [0];
tz [1]=b[i1] [5];
tmp=(tz [0]*2) +tz [1];
k1=0;
printf("\n" );
for(j1=1;j1 <5;j1+ +)
{
tz [k1]=b[i1] [j1];
printf("%d", tz [k1] );
k1++;
}
tmp1=(tz [0]*8) +(tz [1]*4 )+( tz [2]*2) +tz [3];
s witc h(c ount1)
{
c as e 1 : te mp=s 1[tmp][tmp1];
brea k;
c as e 2 : te mp=s 2[tmp][tmp1];
brea k;
c as e 3 : te mp=s 3[tmp][tmp1];
brea k;
c as e 4 : te mp=s 4[tmp][tmp1];
brea k;
c as e 5 : te mp=s 5[tmp][tmp1];
brea k;
c as e 6 : te mp=s 6[tmp][tmp1];
brea k;
c as e 7 : te mp=s 7[tmp][tmp1];
brea k;
c as e 8 : te mp=s 8[tmp][tmp1];
56

brea k;
}
printf("te mp is %d", te mp);
t11=0;
if(te mp==0)
{
for(m=0;m <4;m+ +)
b1[i1][m] =0;
}
e ls e
{
while (te mp>0)
{
a1[t11]=te mp%2;
te mp=te mp/2;
t11++;
}
for(m=t11;m<4;m ++)
a1[m]=0;
z =0;
for(m=3;m >=0;m - -)
{
b1[i1][z ++ ]=a 1[m];
}
}
}
k1=0;
printf("b1 is \n");
for(i1=0;i1 <8;i1+ +)
{
for(j1=0;j1 <4;j1+ +)
printf("%d", b1[i1] [j1]);
printf("\n" );
}
for(i1=0;i1<8;i1 ++)
{
for(j1=0;j1 <4;j1+ +)
c 11[k1++]=b1[ i1][j1 ];
}
/*pe rmuta tion a fte r s ubs titution*/

57

for(i1=0;i1<32;i1 ++)
res [i1]=c 11[p[i1 ]- 1];
for(i1=0;i1<32;i1 ++)
printf("%d", c 11[i1]);
printf("a fte r subs tn\n");
for(i1=0;i1<32;i1 ++)
printf("%d", re s [i1]);
printf("xor l[i] w ith res [i]\n");
for(i1=0;i1<32;i1 ++)
{
if(l[i1] ==re s [i1] )
res [i1]=0;
e lse
re s [i1]=1;
}
for(i1=0;i1<32;i1 ++)
l[i1]=r [i1];
for(i1=0;i1<32;i1 ++)
r[i1]= re s [i1];
printf("le ft a nd right for ne xt round\n");
for(i1=0;i1 <32;i1+ +)
printf("%d", l[i1 ]);
printf("\n");
for(i1=0;i1<32;i1 ++)
printf("%d", r [i1]);
round++;
}
for(i3=0;i3 <32;i3+ +)
{
tmp3[i3]=r[i3 ];
}
i4=0;
for(i3=32;i3<64;i3 ++)
{
tmp3[i3]=l[i4+ +];
}
printf("\na fte r s wa pping\n");
for(i3=0;i3 <64;i3+ +)
printf("%d", tmp3[ i3]);
for(i3=0;i3 <64;i3+ +)
{
tmp4[i3]=tmp3[iip [i3]- 1];
58

}
printf("output is \n");
for(i3=0;i3<64;i3 ++)
printf("%d", tmp4[ i3]);
j=0;
for(i=0;i<64;i ++)
{
tmp5[j]=(t mp4[i]*128 )+(tmp4 [i+1 ]*64)+ (tmp4[i+2 ]*32)+
(tmp4[i+3]*16 )+(t mp4[i+4 ]*8)+ (tmp4[i +5]*4) +
(tmp4[i+6]*2 )+tmp4 [i+7];
i=i+7;
j++;
}
printf("\n");
for(i=0;i<8;i ++)
printf("%c ", tmp5[i ]);
ge tc h();
}
void ke yge n()
{
for(i=0;i <56;i+ +)
{
ke y[i]=k[pc 1[i] - 1];
}
for(i=0;i<28;i ++)
{
c [i]=ke y[i];
}
for(i=28;i<56;i ++)
{
d[j++] =ke y[i];
}
while (c ount<17)
{
t=c [0];
t1=d[0];
for(i=0;i <27;i+ +)
{
c [i]=c [i+1];
d[i]=d[i+1] ;
59

}
if(i==27)
{
c [i]=t;
d[i]=t1;
}
if((c ount==1)||(c ount ==2)|| (c ount==9)|| (c ount==16) )
c ount++;
e ls e if(fla g==1 )
{
count++;
fla g=0;
}
e ls e
fla g=1;
if(fla g==0)
{
for(i=0;i<28;i ++)
c2[i]=c [i] ;
j=0;
for(i=28;i<56;i ++)
c2[i]=d[j ++] ;
for(i=0;i<48;i ++)
k2[c ount-1][i]=c 2[pc 2[i ]- 1];
}
}
}

OUTPUT
Enter the string of 8 chars
SREERAMA
010100110101001001000101010001010101001001000001010011010100000
111111111
0001001
1010011
0011101
1010000
0000000
0000001
0000000
0010011
60

r1: 100000000000000000000000001000000000000010100110
key 010000011101011110001101101111100110001110101010
110000011101011110001101100111100110001100001100
1000temp is 15
1110temp is 11
1111temp is 8
0110temp is 0
0011temp is 7
0011temp is 5
0110temp is 8
0110temp is 11
b1 is
1111
1011
1000
0000
0111
0101
1000
1011
11111011100000000111010110001011after substn
01101000100011101110101110001011xor l[i] with res[i]
left and right for next round
00000000000000000100000000010011
10010111100111011010011101100110r1:
010010101111110011111011110100001110101100001101
key111111100010010011101000000101110110110100110111
101101001101100000010011110001111000011000111010
0110temp is 1
0110temp is 8
0000temp is 13
1001temp is 7
1000temp is 6
1100temp is 1
1100temp is 5
1101temp is 3
b1 is
0001
1000
1101
0111
0110
0001
0101
0011
00011000110101110110000101010011after substn
61

10000110010111110011100110000010xor l[i] with res[i]


left and right for next round
10010111100111011010011101100110
10000110010111110111100110010001r1:
110000001100001011111110101111110011110010100011
key110110101100111000111000011011110000100111010100
000110100000110011000110110100000011010101110111
0011temp is 1
0000temp is 0
1001temp is 15
0011temp is 3
1010temp is 12
0001temp is 15
1010temp is 5
1011temp is 0
b1 is
0001
0000
1111
0011
1100
1111
0101
0000
00010000111100111100111101010000after substn
10010111011101010010000100001110xor l[i] with res[i]
left and right for next round
10000110010111110111100110010001
00000000111010001000011001101000r1:
000000000001011101010001010000001100001101010000
key110011001011101100011110110000011110000111011111
110011001010110001001111100000010010001010001111
1001temp is 11
0101temp is 11
1000temp is 4
0111temp is 3
0000temp is 4
1001temp is 13
0101temp is 0
0111temp is 4
b1 is
1011
1011
0100
0011
0100
62

1101
0000
0100
10111011010000110100110100000100after substn
11010000110011010110001000101010xor l[i] with res[i]
left and right for next round
00000000111010001000011001101000
01010110100100100001101110111011r1:
101010101101010010100100000011110111110111110110
key001001101011111001001111011001111001011010001101
100011000110101011101011011010001110101101111011
0001temp is 12
0011temp is 14
0101temp is 9
0101temp is 1
1101temp is 0
0111temp is 8
0110temp is 10
1101temp is 5
b1 is
1100
1110
1001
0001
0000
1000
1010
0101
11001110100100010000100010100101after substn
11010100100010001000110100110001xor l[i] with res[i]
left and right for next round
01010110100100100001101110111011
11010100011000000000101101011001r1:
111010101000001100000000000001010110101011110011
key011010110111110001100010110110100001010111101111
100000011111111101100010110111110111111100011100
0000temp is 4
1111temp is 5
1110temp is 2
0001temp is 6
1011temp is 9
1011temp is 7
1110temp is 9
1110temp is 12
b1 is
0100
63

0101
0010
0110
1001
0111
1001
1100
01000101001001101001011110011100after substn
00101011011000001111000000111101xor l[i] with res[i]
left and right for next round
11010100011000000000101101011001
01111101111100101110101110000110r1:
001111111011111110100101011101010111110000001100
key111010001110110111111000000011101101101110101101
110101110101001001011101011110111010011110100001
1010temp is 3
1010temp is 7
0100temp is 3
1110temp is 14
1111temp is 9
1101temp is 13
1111temp is 1
0000temp is 2
b1 is
0011
0111
0011
1110
1001
1101
0001
0010
00110111001111101001110100010010after substn
01110111010000100111001001011110xor l[i] with res[i]
left and right for next round
01111101111100101110101110000110
10100011001000100111100100000111r1:
110100000110100100000100001111110010100000001111
key110101001110011100011011010100100111110111110001
000001001000111000011111011011010101010111111110
0000temp is 0
0100temp is 6
1100temp is 5
1111temp is 9
1101temp is 9
1010temp is 13
64

1011temp is 12
1111temp is 8
b1 is
0000
0110
0101
1001
1001
1101
1100
1000
00000110010110011001110111001000after substn
11111101000100010010000001011001xor l[i] with res[i]
left and right for next round
10100011001000100111100100000111
10000000111000111100101111011111r1:
110000000001011100000111111001010111111011111111
key101111100100101111100110111011001111100001010111
011111100101110011100001000010011000011010101000
1111temp is 8
0010temp is 10
1001temp is 15
0000temp is 3
0001temp is 12
1100temp is 14
1101temp is 10
0100temp is 9
b1 is
1000
1010
1111
0011
1100
1110
1010
1001
10001010111100111100111010101001after substn
11011101111011010000110100001101xor l[i] with res[i]
left and right for next round
10000000111000111100101111011111
01111110110011110111010000001010r1:
001111111101011001011110101110101000000001010100
key101110100111001100101001101001111100011011111010
100001011010010101110111000111010100011010101110
0000temp is 15
1101temp is 0
65

1010temp is 5
1011temp is 11
0011temp is 12
1010temp is 3
1101temp is 10
0111temp is 2
b1 is
1111
0000
0101
1011
1100
0011
1010
0010
11110000010110111100001110100010after substn
10000101111001111010011001000011xor l[i] with res[i]
left and right for next round
01111110110011110111010000001010
00000101000001000110110110011100r1:
000000001010100000001000001101011011110011111000
key100010010001011101111101100111011001111101000011
100010011011111101110101101010000010001110111011
0001temp is 1
1101temp is 9
1110temp is 2
1010temp is 5
0101temp is 13
0001temp is 1
0111temp is 13
1101temp is 5
b1 is
0001
1001
0010
0101
1101
0001
1101
0101
00011001001001011101000111010101after substn
10100011000111000111100000100111xor l[i] with res[i]
left and right for next round
00000101000001000110110110011100
11011101110100110000110000101101r1:
111011111011111010100110100001011000000101011011

66

key110001010101101011011101100111101100011001110100
001010101110010001111011000110110100011100101111
0101temp is 15
0111temp is 1
1000temp is 2
1101temp is 7
0011temp is 1
1010temp is 4
1110temp is 6
0111temp is 13
b1 is
1111
0001
0010
0111
0001
0100
0110
1101
11110001001001110001010001101101after substn
10101000110100001101111000101110xor l[i] with res[i]
left and right for next round
11011101110100110000110000101101
10101101110101001011001110110010r1:
010101011011111010101001010110100111110110100101
key000101111111101111100000010110011110111111000100
010000100100010101001001000000111001001001100001
1000temp is 3
0010temp is 7
1010temp is 5
0100temp is 6
0000temp is 2
1100temp is 6
0100temp is 4
0000temp is 2
b1 is
0011
0111
0101
0110
0010
0110
0100
0010
00110111010101100010011001000010after substn
01000100011100110101001010011010xor l[i] with res[i]
left and right for next round
67

10101101110101001011001110110010
10011001101000000101111010110111r1:
110011110011110100000000001011111101010110101111
key100110100111110111100011101110001110010010011001
010101010100000011100011100101110011000100110110
1010temp is 12
1010temp is 2
0001temp is 7
0001temp is 15
0010temp is 12
1001temp is 14
0010temp is 2
1011temp is 13
b1 is
1100
0010
0111
1111
1100
1110
0010
1101
11000010011111111100111000101101after substn
11011101111001011001110001101100xor l[i] with res[i]
left and right for next round
10011001101000000101111010110111
01110000001100010010111111011110r1:
001110100000000110100010100101011111111011111100
key111110010110011101001101111010110111011000000111
110000110110011011101111011111101000100011111011
1000temp is 15
1011temp is 6
1101temp is 11
0111temp is 8
1111temp is 6
0100temp is 2
0001temp is 11
1101temp is 5
b1 is
1111
0110
1011
1000
0110
0010
1011
68

0101
11110110101110000110001010110101after substn
01000110101001001000111111110111xor l[i] with res[i]
left and right for next round
01110000001100010010111111011110
11011111000001001101000101000000r1:
011011111110100000001001011010100010101000000001
key001011110011110010111110001010111001101110110011
010000001101010010110111010000011011000110110010
1000temp is 3
0110temp is 8
1001temp is 13
1011temp is 11
1000temp is 8
1101temp is 11
0011temp is 14
1001temp is 6
b1 is
0011
1000
1101
1011
1000
1011
1110
0110
00111000110110111000101111100110after substn
10010101011110110010011101100011xor l[i] with res[i]
left and right for next round
11011111000001001101000101000000
11100101010010100000100010111101
after swapping
111001010100101000001000101111011101111100000100110100010100000
0output is
110010011001000011100001100101011000100101000001110110101100100
1

69

AIM
Using RSA algorithm Encrypt a text data and Decrypt the same.
1. Start
2. Read p.q,d
3. If p,q,d are prime
4. Perform n=p*q, z=(p-1)*(q-1), e=1 mod z
5. Print n,z,e
6. Read Plain Text(p1)
7. Perform Cipher Text C=p^e(mod n)
70

8. Print Cipher Values(C)


9. Perform Decrypted Text P2=c^d(mod n)
10. Print Decrypted Values(P2)
11. Stop

Start

Read p,q,d

If
p,q,d are
prime

n=p*q
z=(p-1)*(q-1)
e=1 mod z

Print n, z , e

Read plain text


(p1)

FLOW CHART

Cipher Text
C=p^e (mod n)

Print
Cipher
values
(c)
Decrypted Text
P2=c^d ( mod n )

Print Decrypted values


(p2)

71
Stop

PROGRAM
/********** RSA PROGRAM ***********/
/*d value should be less than 11 bcoz (c^d)modn can't be computed using available
datatypes*/
#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
72

char a[]={"0ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
int n,i,j,s,n2,k1,p,q,d,m1,e1,l5,z,p2[30],s1,c[30];
unsigned long int l3,m,l4,k2;
double l2,l1,l6;
float e,l;
char p1[30];
clrscr();
printf("enter two prime numbers p and q\n");
scanf("%d %d",&p,&q);
do{
n=p*q;
if(n<26)
{ printf("\n n value is not large enough.\nplease select p, q value such that p*q is
greater than 26");
scanf("%d %d",&p,&q);}
}while(n<26);
z=((p-1)*(q-1));
printf("enter the value of d:\n");
scanf("%d",&d);
for(j=1;j<z;j++)
{
if((j*d)%z==1)
break;
}
e=j;
printf("%d %d
%f\n",n,z,e);
printf("ENCRYPTION-CIPHERTEXT");
printf("enter the plain text\n");
scanf("%s",p1);
for(i=0;i<strlen(p1);i++)
{
for(j=1;j<strlen(a);j++)
{
if(a[j]==p1[i])
{
s=j;
break;
}
else
continue;
}
printf("%d",s);
e1=(int)e;
l1=pow(((double)s),((double)e1));
k2=fmod(l1,(double)n);
printf("\n%lu\n",k2);
c[i]=(int)k2;
73

printf("cipher:%d\n",c[i]);
}
printf("\n");
for(i=0;i<strlen(p1);i++)
{
l2=(pow(((double)c[i]),((double)d)));
m=fmod(l2,(double)n);
m1=(int)m;
printf(" %c\n",a[m1]);}
getch();
}

OUTPUT
enter two prime numbers p and q
3 11
enter the value of d:
7
33
20
3.000000
ENCRYPTION-CIPHERTEXTenter the plain text
SUZANNE
74

19
28
cipher:28
21
21
cipher:21
26
20
cipher:20
1
1
cipher:1
14
5
cipher:5
14
5
cipher:5
5
26
cipher:26
S
U
Z
A
N
N
E

RSA output:
enter two prime numbers p and q
5 13
enter the value of d:
7
65
48
7.000000
ENCRYPTION-CIPHERTEXTenter the plain text
NAINA
14
14
cipher:14
1
1
cipher:1
9
9
cipher:9
14
75

14
cipher:14
1
1
cipher:1
N
A
I
N
A

References
1.
2.

Computer Networks
A.S.Tanenbaum, 3rd editition, PHI
Computer Networking a Top-Down approach
76

3.
4.
5.

featuring the internet.


J.F.Kurose, K.W.Ross, Pearson Education
Data Communication and networking
A.S. Godbole, TMH
High speed networks and internets
W. Stallings, Pearson education
Internetworking with TCP/IP, Vol. 1, Douglas t. Comer, 4th
Edition, Pearson

77

Das könnte Ihnen auch gefallen