#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int p,q,r;
int i,t[100],t1[100],t2[100];
char M[100],C[100];
int phi,n,e=53,d=11;
int cipher(int t)
{
int i,temp;
temp = 1;
for(i=0;i< e;i++)
temp = temp * t % n;
temp = temp % n;
return temp;
}
int plain(int t)
{
int temp,i;
temp = 1;
for(i=0; i< d; i++)
temp=temp * t % n;
temp = temp % n;
return temp;
}
void prime()
{
int i;
int flag=0;
long int r;
randomize();
r=random(100);
// printf("%ld\t",r);
for(i=2;i<=(r/2);i++)
{
if(r%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
if(q==0)
q=r;
else if(p==0)
p=r;
}
}
int gcd( int m, int n)
{
while( m!= n)
{
if( m > n)
m= m - n;
else
n= n - m;
}
return ( m);
}
void main()
{
int s,k,j,count,sum;
clrscr();
do
{
q=0;
while(q==0)
prime();
p=0;
while(p==0)
prime();
}while(gcd(p,q)!=1) ;
printf("\n\np = %d\nq = %d",p,q);
n = p*q;
phi=(p-1)*(q-1);
//
printf("\n\tN = %d",n);
printf("\n\tF(n) = %d",phi);
do
{
randomize();
r=random(phi);
prime();
e=r;
}while((gcd(e,phi)!=1) && e<1 || e>500);
printf("\n\ne = %d",e);
// e=3;
// d=1;
do
{
s=(d*e)%phi;
d++;
}while(s!=1);
d=d-1;
printf("\n\nd = %d",d);
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
//
printf("\n\nPlain Text: ");
gets(M);
** printf("\nEncrypted Text:");
for(i=0;i<strlen(M);i++)
{
t[i] = (int)M[i];
t1[i] = cipher(t[i]);
printf("%c ",t1[i]);
}
printf("\n\nDecrypted Text: ");
for(i=0;i<strlen(M);i++)
{
t2[i] = plain(t1[i]);
printf("%c ",t2[i]);
}
getch();
}
My problem is, e and k keys are generated incorrect. Can anyone please help wit this?