/* C program for the Implementation Of RSA Algorithm */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int phi,n,e,d,FLAG;
char M[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]_
abcdefghijklmnopqrstuvwxyz{|}~`";
char C[100];
int check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return 0;
}
FLAG = 0;
}
void printfact(int num)
{
printf("\n Factors of %d:- ",num);
for(int i=1;i<=num/2;i++)
{
if(num%i == 0)
printf(" %d",i);
}
printf("\n");
}
void encrypt()
{ int j,i;
clrscr();
printf("\n \t ........ Encryption Phase ........\n");
int k=strlen(M);
for(j=0;j<k;j++)
{
printf("\n Initially --> %d %c \n",M[j],M[j]);
int t=M[j];
int s = 1;
for(i=0;i< e;i++)
{
s=(s*t)%n;
printf("%d",s);
}
s = s%n;
C[j]=s;
printf("\n Now --- > %c %d\n",C[j],C[j]);
}
printf("\n\tEncrypted keyword :\n");
for(i=0;i<j;i++)
printf(" %c",C[i]);
getch();
}
void decrypt()
{
int j,i;
printf("\n \t ........ Decryption Phase ........\n");
for(j=0;j<strlen(C);j++)
{
int k=C[j];
int t = 1;
printf("\n\n Earlier .. %d %c --> ",C[j],C[j]);
for(i=0;i< d;i++)
{t=(t*k)%n;
printf(" %d",t);
}
t = t%n;
M[j]=t;
printf("\n Now ... %c %d -->",M[j],M[j]);
}
printf("\n\tDecrypted keyword :");
for(i=0;i<j;i++)
printf(" %c",M[i]);
getch();
}
int main()
{
clrscr();
int p,q,s,i;
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
printfact(phi);
do
{
printf("\n\nEnter e\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
d = 1;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
fflush(stdin);
puts(M);
printf("\n\n");
for(i=0;i<strlen(M);i++)
printf(" %d",M[i]);
encrypt();
printf("\n\n");
for(i=0;i<strlen(M);i++)
printf(" %d",C[i]);
//
//printf("\n\nEnter the Cipher text\t: ");
//gets(C);
clrscr();
decrypt();
printf("\n\n");
for(i=0;i<strlen(M);i++)
printf(" %d",M[i]);
printf("\n");
puts(M);
getch();
return 0;
}
No comments:
Post a Comment