I had written a C++ program to perform the Schmidt - Samoa Public Key encryption. The code is as below :
void keygen(long &publickey,long &privatekey,long &p,long &q)
{
p = genrndprimes(0,10);
q = genrndprimes(10,30);
long n = power(p,2)*q;
long d = (1/n)%lcm((p-1),(q-1));
publickey = n;
privatekey = d;
}
void encrypt(char *str,long *nums,long key)
{
for(long i=0;i<strlen(str);i++)
{
nums[i]=(long)power(str[i],key)%key;
}
}
void decrypt(char *str,long *nums,long key,long p,long q)
{
for(long i=0;i<strlen(str);i++)
{
str[i]=(long)power(nums[i],key)%(p*q);
}
}
void main()
{
char m[]="Hello!!";
long *j = new long[strlen(m)];
long p,q,privtkey,publkey;
keygen(publkey,privtkey,p,q);
encrypt(m,j,publkey);
puts(m);
cout<<"\n\n";
decrypt(m,j,privtkey,p,q);
cout<<"\n\n";
puts(m);
getch();
}
The code however does not seem to encrypt properly , and therefore decryption gives some unknown
characters (whose ASCII is unknown). What could be the mistake ? I have also optimised the pow() function with power() (which uses exponentiation by squaring.) but in vain.