If I give sample inputs as p=61,q=53 and if I give a message value that lies between 1 and 21 for this particular prime set my program successfully encrypts and decrypts the message back to the correct original message. But if I give input as p=61,q=53 and message value is greater than 21 it is not able decrypt the message back to the correct original message.
And It's not just these particular prime numbers set. It's for any pair of prime numbers that only a certain range of message values get encrypted and decrypted correctly. So why is that? Can any one suggest on how to solve this?
class RSA
{
private:
long int p,q;
long int msg,cipherMsg,plainMsg;
long int n,totient;
long int publicKey,privateKey;
public:
RSA();
void ComputeKeys(long int p1,long int p2,long long int message);
void EncryptMessage();
void DecryptMessage();
};
RSA :: RSA()
{
p=0;q=0;
n=0;totient=0;
publicKey=0;privateKey=0;
msg=0;cipherMsg=0;plainMsg=0;
}
void RSA :: EncryptMessage()
{
int y=1;
cout<<"Encrypting Message....."<<endl;
for(long int i=1;i<=publicKey;i++)
{
y=y*msg;
}
msg=y;
cout<<"m^e:"<<msg<<endl;
cipherMsg= msg%n;
cout<<"Encryption Complete!"<<endl;
cout<<"Cipher Message:"<<cipherMsg<<endl;
}
void RSA :: DecryptMessage()
{
long int y= 1,tmp;
cout<<"Decrypting Message...."<<endl;
for(long int i=1;i<=privateKey;i++)
{
y=y*cipherMsg;
tmp=y;
y=fmod(y,n);
}
cout<<"c^d:"<<tmp<<endl;
plainMsg=y;
cout<<"Decryption Complete!"<<endl;
cout<<"Original Message:"<<plainMsg<<endl;
}
int main()
{
RSA obj;
cout<<"\t------RSA Cryptography------"<<endl;
obj.ComputeKeys(61,53,21);
return 0;
}
(Note, I have not posted my ComputeKeys()
method for proprietary reasons. But I believe this where I need to do modifications.)