/* Program for simple RSA algorithm to Encrypt & Decrypt Data
USN : 2BA11CS409 Roll no 50 Batch A4*/
#include<stdio.h>
#include<stdlib.h>
int phi,n,m,e,d,c;
void encrypt();
void decrypt();
int main()
{
int p,q,s;
printf("Enter two relative prime no\n");
scanf("%d%d",&p,&q);
n=p*q;
printf("The moduls value : %d\n",n);
phi=(p-1)*(q-1);
printf("E(n) : %d\n",phi);
printf("Enter the exponent value\n");
scanf("%d",&e);
d=1;
do
{
s=(d*e)%phi;
d++;
}while(s!=1);
d=d-1;
printf("Public key { %d %d }\n",n,e);
printf("Private key{ %d %d }\n",n,d);
printf("Enter the plain text\n");
scanf("%d",&m);
encrypt();
printf("enter the Cipher text\n");
scanf("%d",&c);
decrypt();
return 0;
}
void encrypt()
{
int i,c=1;
for(i=0;i<e;i++)
c=c*m%n;
c=c%n;
printf("encryptd data : %d\n",c);
}
void decrypt()
{
int i;
m=1;
for(i=0;i<d;i++)
m=m*c%n;
m=m%n;
printf("Decrypted data : %d\n",m);
}
/* OUTPUT
[root123@localhost ~]$ cc RSA.c
[root123@localhost ~]$ ./a.out
Enter two relative prime no
49
71
The moduls value : 3479
E(n) : 3360
Enter the exponent value
79
Public key { 3479 79 }
Private key{ 1999 79 }
Enter the plain text
688
encryptd data : 3061
enter the Cipher text
3061
Decrypted data : 688
*/