You are on page 1of 2

/* RSA*/

#include <stdio.h>
#include <math.h>
int gcd(int a,int b) //computes the GCD using the Euclid method
{
int c;
while(1)
{
c = a%b;
if(c==0)
return b;
a = b,b = c;
}
}
int En_De(int e,int val,int n)
{
int i,a=1;
for(i=1;i<=e;i++)
{
a = (val%n)*a;
a %= n;
}
return (a%n);
}
void main()
{
char c;
int as,p,q,n,et,i,e,d;
int ciph,pt;
printf("Enter No. :");
scanf("%d",&as);
printf("Enter prime No.s p,q :");
scanf("%d %d",&p,&q);
n = p*q;
et=(p-1)*(q-1);
for(i=2;i<et;i++)
if(gcd(et,i) == 1)
printf("d = %d",i);
printf("\nSelect e value:");
scanf("%d",&d);
/* i=et/e;
while(1)
{
if( ((e*i)%et) == 1)
break;
else

i++;
}
d=i;*/
for(i=et/d; ((d*i)%et) == 1;i++);
e = ++i;
printf("\n\nEntered Text = %d\nEncryption Key = %d\nDeycryption Key = %d",as,d,e);
// Encrypting...
//ciph = (long)(pow(as,e))%n;
ciph = En_De(e,as,n);
pt = En_De(d,ciph,n);
//pt = pow((double)ciph,(double)e) ;
printf("\nEnrypted Text(CipherText) = %d",ciph);
printf("\nDecrypted Text(PlainText) = %d",pt);
}

You might also like