RSA  

Posted in

#include"stdio.h"
#include"math.h"
#include"conio.h"

double min(double x,double y)
{
return(xy ? x:y);
}

double gcd(double x,double y)
{
if(x==y)
return x;
else
return(gcd(min(x,y),max(x,y)-min(x,y)));
}

/*long double dbin(long double n)
{
long double i=0,s=0,v,b;
do
{
s+=pow(10,i)*(fmod((int)n,2));
i++;
n=n/2;
}while(n>=1);
return(s);
} */

long double modexp(long double a,long double x,long double n)
{
long double r=1;
while(x>0)
{
if((int)(fmodl(x,2))==1)
r=fmodl((r*a),n);
a=fmodl((a*a),n);
x/=2;
}
return(r);

/* r=fmodl(pow(a,x),n);
return r;*/
}

void main()
{
long double p,q,phi,n,e,d;
long double ms,es,ds,a[100];
int ch,i;
char s[100],c,m;
clrscr();
do
{
printf("\nEnter prime numbers\n");
scanf("%Lf%Lf",&p,&q);
printf("%Lf %Lf",p,q);
}while(p==q);
n=p*q;
phi=(p-1)*(q-1);
do
{
printf("\nEnter prime value of e\n");
scanf("%Lf",&e);
printf("%Lf",e);
}while((gcd(e,phi)!=1) && e>phi);
for(d=1;d if(fmod((e*d),phi)==1)
break;
printf("\nD within main=%Lf",d);
do
{
printf("\nEnter your choice\n1:Numbers\n2:Characters\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\nEnter the message[numbers]:\n");
scanf("%Lf",&ms);
printf("%Lf",ms);
es=modexp(ms,e,n);
ds=modexp(es,d,n);
printf("\n\nOriginal message:%Lf\n",ms);
printf("\nEncrypted message:%Lf\n",es);
printf("\nDecrypted message:%Lf\n",ds);
break;

case 2:printf("Enter message[characters]\n");
fflush(stdin);
gets(s);
i=0;
while((c=s[i])!='\0')
{
c=c-'a';
c++;
a[i]=c;
es=modexp(a[i],e,n);
ds=modexp(es,d,n);
printf("\nOriginal message=%c",c+'a'-1);//((int)a[i]+'a'-1));
printf("\nEncrypted message=%Lf",es);
printf("\nDecrypted message=%c",((int)ds+'a'-1));
i++;
}
break;
default:break;
}
}while(ch<=2);
getch();
}

This entry was posted at 10:23 AM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment

Reverse Engineering :Subscribe Now

Zts - ZTS

DISCLAIMER

This Blog is ONLY for educational purposes,
and any such codes/snippets provided are to be executed on
your sole discretion. The author is not responsible for the codes.