RSA program of CN

Thursday, April 15, 2010
#include
#include
int phi,m,n,e,d,c,flag;
void check()
{
int i;
for(i=3;e%i==0 && phi%i==0;i=i+2)
{
flag=1;
return;

}
flag=0;
}
void encrypt()/*encrypts the plain text */
{
int i;
c=1;
for(i=0;i c=c*m%n;
c=c%n;
printf("\n\tEncryted keyword:%d",c);
}
void decrypt()/*decrypts the cipher text */
{
int i;
m=1;
for(i=0;i m=m*c%n;
m=m%n;
printf("\n\tDecrypted keyword:%d",m);
}
void main()
{
int p,q,s;
clrscr();
printf("\nEnter two relatively prime nos.\t:");
scanf("%d%d",&p,&q);
n=p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t=%d",phi);
do//chooses correct e value
{
printf("\nEnter e:");
scanf("%d",&e);
check();
}while(flag==1);
d=1;
do//generates exact d value
{
s=(d*e)%phi;
d++;
}while(s!=1);
d=d-1;
printf("\nPublic key\t:{%d,%d}",e,n);
printf("\nPrivate key\t:{%d,%d}",d,n);
printf("\nEnter the plain text\t");/*entered plain text has to be in the range 0<=p<=n*/
scanf("%d",&m);
encrypt();
printf("\nEnter the cipher text\t:");
scanf("%d",&c);
decrypt();
getch();
}

0 comments:

Post a Comment