RSA ALGORITHM

advertisement
RSA ALGORITHM
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Key Generation Algorithm
Step 1:Generate two large random primes, p and q.
Step 2: Compute n = p*q and z = (p-1)(q-1).
Step 3: Choose a number relatively prime to z and call it d.
Step 4: Find e such that e*d=1 mod z.
Step 5: The public key is (n, e) and the private key is (n, d).
Encryption
Sender A does the following:Step 1: Obtains the recipient B's public key (n, e).
Step 2: Represents the plaintext message as a positive integer m.
Step 3:Computes the ciphertext c =m^e mod n.
Step 4: Sends the ciphertext c to B.
Decryption
Recipient B does the following:Step 1: Uses his private key (n, d) to compute m = c^d mod n.
Step 2:Extracts the plaintext from the integer representative m.
1
/****************** RSA PROGRAM *****************/
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
long int i,p,q,n,z,e,j,d,c,pt;
void prime();
int gcd(int z);
clrscr();
prime();
printf("\n enter two large prime no from above:");
scanf("%ld%ld",&p,&q);
n=p*q;
z=(p-1)*(q-1);
d=gcd(z);
for(i=1;i<20;i++)
if((i*d)%z==1){
e=i;
break;
}
printf("public key:(%ld,%ld)",n,e);
printf("\n private key:(%ld,%ld)",n,d);
printf("\n enter any plain text:");
scanf("%ld",&pt);
i=pow(pt,e);
c=i%n;
printf("encryption text:%ld",c);
j=pow(c,d);
pt=j%n;
printf("\n plain text is:%ld",pt);
getch();
}
void prime()
{
long int i=2,flag,a;
while(i<20)
{
flag=0;
for(a=2;a<=i/2;a++)
{
if(i%a==0)
flag=1;
}
2
if(flag==0)
printf("%ld ",i);
i++;
}
}//end of prime
int gcd(int z)
{
long int i=2,e,j;
while(i<20)
{
e=0;
for(j=1;j<=z;j++)
if((z%j==0)&&(i%j==0))
e=j;
if(e==1)
return i;
i++;
}
}//End of gcd.
RSA OUTPUT
2 3 5 7 11 13 17 19
Enter two large prime no from above: 3 11
Public Key : (33,7)
Private Key: (33,3)
Enter any Plain text : 7
Encryption text : 28
Plain text is: 7
3
BITSTUFFING FLOW CHART
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
4
/****************** BIT STUFFING PROGRAM *****************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
main(){
char string[40],stx[7]="111111",result[100]="";
char temp[40];
int i,j,k,l;
clrscr();
printf("Enter the frame data:");
scanf("%s",string);
l=strlen(string);
for(i=0;i<l;i++){
k=0;
for(j=i;j<i+5;j++)
temp[k++]=string[j];
temp[k]='\0';
if(strcmp(temp,"11111")==0){
for(k=l;k>i+5;k--)
string[k]=string[k-1];
string[i+5]='0';
string[l+1]='\0';
l++;
}
}
strcat(result,stx);
strcat(result,string);
strcat(result,stx);
printf("The BitStuffed string is:%s",string);
printf("\nThe ouput string is:%s",result);
getch();
}//End of main.
BIT STUFFING OUTPUT
Enter the frame data : 10101111110
The Bitstuffed String is : 101011111010
The output Sting is : 111111101011111010111111
5
CHARACTER STUFFING FLOW CHART
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
6
/************** CHARACTER STUFFING PROGRAM*************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
main(){
char str[40],s,e,str1[40];
int i,j,l,k=0;
clrscr();
printf("Enter a string:");
scanf("%s",str);
printf("Staring delimiter:");
fflush(stdin);
scanf("%c",&s);
printf("Ending delimiter:");
fflush(stdin);
scanf("%c",&e);
l=strlen(str);
str1[k++]=s;
for(i=0;i<l;i++){
if(str[i]==s)
str1[k++]=s;
else if(str[i]==e)
str1[k++]=e;
str1[k++]=str[i];
}
str1[k++]=e;
str1[k]='\0';
printf("The string is:%s",str1);
getch();
}//End of main.
CHARACTER STUFFING OUTPUT
Enter a String : goodday
Starting Delimiter : d
Ending Delimiter : g
The String is : dggooddddayg
7
CRC-12 FLOW CHART
Start
Read values of
both transmitted
and received data
Concatenate twelve 0’s at
the end of transmitted data
Perform binary division where
CRC12 generating polynomial is
the divisor and dividend is the
concatenated string
Length of
remainder
==12
Add required number of
0’s to the beginning of
the remainder such that
length is 12
Replace the twelve 0’s with the
obtained CRC
Perform binary division on this with
the divisor as generating polynomial
8
Remain
der==0
Printf message
“data transmitted
incorrectly”
Print meaasge
“data transmitted
correctly”
stop
9
/********************* CRC - 12 PROGRAM******************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
char d[30]="1100000001011";
char I[40];
char a[12]="000000000000";
char t[13];
int j,l,n,p,i=0;
main(){
void division(void);
clrscr();
printf("Enter the input string:");
scanf("%s",I);
n=strlen(I);
strcat(I,a);
p=12+n;
division();
printf("The remainder at sender is:%s",t);
for(i=n,j=1;i<p;i++,j++)
I[i]=t[j];
printf("\nThe padded string to be sent:%s",I);
division();
printf("\nThe remainder string at receiver:%s",t);
printf("\n---------Received Message is correct----------");
getch();
}//End of main.
void division(void){
for(i=0;i<13;i++)
t[i]=I[i];
while(i<p){
for(l=0;l<13;l++){
if(t[l]==d[l])
t[l]='0';
else
t[l]='1';
}
if(t[0]=='0'){
for(j=0;j<12;j++)
t[j]=t[j+1];
t[j]=I[i++];
}
}//End of while
while(t[0]=='1')
for(l=0;l<13;l++){
10
if(t[l]==d[l])
t[l]='0';
else
t[l]='1';
}
t[l]='\0';
}//End of division.
CRC-12 OUTPUT
Enter the input String : 10101
The remainder at sender is : 0100011100011
The padded String to be sent : 10101100011100011
The remainder string at receiver :000000000000
-----------Received Message is Correct----------------
11
Shortest Path Routing (Dijkstra’s Algorithm) 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
A
for the node recently added to permanent list,
find the neighbouring nodes.
if a node is not already
present in the temp
12
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
A
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
13
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
14
/**********SHORTEST PATH(Dijkstra’s Algorithm)*************/
#include<stdio.h>
#include<conio.h>
struct node{
int length;
int flag;
int prev;
};
int adj[10][10],i,j;
int path[10],source,dest,n;
main(){
void shortpath(void);
clrscr();
printf("Enter the no of nodes:");
scanf("%d",&n);
printf("Enter the adjascent matrix of (%d,%d)(-1 for no
edge):\n",n,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
printf("Enter the source and destination:");
scanf("%d%d",&source,&dest);
shortpath();
getch();
}//End of main.
void shortpath(void){
struct node a[10];
int k,min;
for(i=1;i<=n;i++){
a[i].length=999;
a[i].flag=0;
a[i].prev=0;
}
k=source;
a[k].flag=1;
a[k].length=0;
while(k!=dest){
for(i=1;i<=n;i++)
if((adj[k][i]>0)&&(a[i].flag==0))
if((a[k].length+adj[k][i])<a[i].length){
a[i].prev=k;
a[i].length=a[k].length+adj[k][i];
}
min=999;
15
k=0;
for(i=1;i<=n;i++){
if((a[i].flag==0)&&(a[i].length<min)){
min=a[i].length;
k=i;
}
}
a[k].flag=1;
}//End of while.
path[0]=dest;
k=dest;i=1;
while(k!=0){
j=a[k].prev;
if(j>0)
path[i++]=j;
k=j;
}
printf("The shortest path is:");
for(j=i-1;j>=0;j--)
printf(" %d ",path[j]);
printf("\nShortest path length is :%d ",a[dest].length);
}//End of shortestpath.
Network topology:
------------------------
1
1
2
3
1
1
1
2
5
3
6
2
4
Output of execution:
---------------------------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 and destination node : 1 6
The Shortest path is : 1 2 3 5 6
The Shortest Length is : 6
16
Distance Vector Routing 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
17
/*************** DISTANCE VECTOR ROUTING **************/
#include<stdio.h>
#include<conio.h>
main(){
int i,j,k,l,s,n,m;
int a[20][20],source[10];
int neigh[10];
clrscr();
printf("Enter the no of nodes:");
scanf("%d",&n);
printf("Enter node for which dist vect table is needed:");
scanf("%d",&s);
printf("Enter the no of neighbouring nodes to-%d:",s);
scanf("%d",&l);
printf("Enter the neighbouring nodes in increasing order:");
for(i=1;i<=l;i++)
scanf("%d",&neigh[i]);
printf("Enter the dist vectors of the source node and neigh
nodes:\n");
for(j=1;j<=n;j++)
scanf("%d",&source[j]);
for(i=1;i<=l;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[neigh[i]][j]);
for(i=1;i<=l;i++){
k=neigh[i];
for(j=1;j<=n;j++){
if((source[j]<0)&&(a[k][j]>0))
source[j]=source[k]+a[k][j];
else if((source[j]>source[k]+a[k][j])&&(a[k][j]>0))
source[j]=source[k]+a[k][j];
}//End of for.
printf("\nThe dist vect of-%d after receiving-%d\n",s,k);
for(j=1;j<=n;j++)
printf(" %d ",source[j]);
}//End of main for loop.
getch();
}//End of main.
18
Network Graph for Output:
2
1
3
4
Output from first execution:
-----------------------------------Distance Vector Routing
***************************************************************
Enter the number of nodes:4
Enter the node for which Distance Vector table is neede:4
Enter the no. of neighbours to -4:2
enter the neighbouring nodes of -4 in incr order: 2 3
enter the distance vectors of the source and the neighbouring nodes:
-1 1 2 0
1011
1102
The dist vect of -4 after receiving node-2 :
2120
The dist vect of -4 after receiving node-3:
2120
19
Download