Thursday, 3 April 2014

RSA algorithm in C

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int FLAG=1;
void check(int e,int phi)
{
int i;
for(i=3;e%i==0 && phi%i==0;i+2)
{
FLAG = 1;
return;
}

FLAG = 0;
}
main()
{

int f,i,j,k,phi,p,q,n,g,d,e,w,c,s,v,b[10];
char a[100],ac[100],as[100],qw[100],qe[100],qt[100],qy[100];
printf("enter the two prime numbers\n");
scanf("%d",&p);
scanf("%d",&q);
n=m*n;
phi=(p-1)*(q-1);
do
{
printf("enter the e value which is co-prime to %d\n",phi);
scanf("%d",&e);
check(e,(m-1)*(n-1));

}while(FLAG==1);

for(i=2;i<phi;i++)
{
d=(e*i)%l;
if(d==1)
break;
}
d=i;
printf("%d",d);
printf("the public keys are\n");
printf("(%d %d) \n",e,n);
printf("the private keys are\n");
printf("(%d %d) \n",d,n);
printf("enter the text\n");
scanf("%s",a);
v=strlen(a);
for(i=0;i<v;i++)
{
g=a[i];
c=1;
for(j=0;j<e;j++)
{
c=(c*g)%n;
}
b[i]=c;
as[i]=b[i];
}
printf("the encrypted form is\n");
for(i=0;i<v;i++)
printf("%d ",b[i]);
printf("\n");


for(i=0;i<v;i++)
{
g=b[i];
c=1;
for(j=0;j<d;j++)
{
c=(c*g)%n;
}
ac[i]=c;
}
printf("the decrypted form is\n");
printf("%s\n",ac);
}

No comments:

Post a Comment