Click here to Skip to main content
15,918,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to implement RSA using a C++ class. Below is the program :

C++
#include<math.h>
#include<iostream>
#include<cmath>
#include<Windows.h>
#include <algorithm>


using namespace std;

class rsacrypto
{
    long publickey;
    long privatekey;
    long modl; //Modulus

 

 

    public :
 
    rsacrypto(); //To be used to just generate private and public keys.
    rsacrypto(long &,long &,long &);//To be used just to generate private and public keys.
    rsacrypto(long key,long modulus) // Should be used when a data is to be encrypted or decrypted using a key.
    {
        publickey = privatekey = key;
        modl = modulus;
    }
 

    long ret_publickey()
    {
        return publickey;
    }
    long ret_privatekey()
    {
        return privatekey;
    }
    long ret_modulus()
    {
        return modl;
    }
 
    void encrypt(char *);
    void decrypt(char *);
    int genrndprimes(int, int);
    int totient(int);
    int genrndnum(int, int);
};
 
rsacrypto::rsacrypto()
    {
        long p1,p2; //Prime numbers
        long n = 0; //Modulus
        long phi =0; //Totient value.

        long e = 0; //Public key exponent.
        long d = 0; //Private key exponent.

        p1 = genrndprimes(100,900);
        Sleep(1000);
        p1 = genrndprimes(100,900);
 
        n = p1*p2;
        phi = totient(n);
 
        e = genrndnum(2,(phi-1));
 
        while(std::__gcd(e,phi)!=1)
        {
            e = genrndnum(2,(phi-1));
        }
 
        d = (1/e)%phi; //Modular Multiplicative Inverse.

        privatekey = e;
        publickey = d;
        modl = n;
 

    }
 

rsacrypto::rsacrypto(long &pubkey,long &privkey,long &mdls)
    {
        long p1,p2; //Prime numbers
        long n = 0; //Modulus
        long phi =0; //Totient value.

        long e = 0; //Public key exponent.
        long d = 0; //Private key exponent.

        p1 = genrndprimes(100,900);
        Sleep(1000);
        p1 = genrndprimes(100,900);
 
        n = p1*p2;
        phi = totient(n);
 
        e = genrndnum(2,(phi-1));
 
        while(std::__gcd(e,phi)!=1)
        {
            e = genrndnum(2,(phi-1));
        }
 
        d = (1/e)%phi; //Modular Multiplicative Inverse.

        privatekey = e;
        publickey = d;
 
        pubkey = publickey;
        privkey = privatekey;
        mdls = n;
        modl = n;
 
    }
 

void rsacrypto::encrypt(char *dat)
{
    long siz = strlen(dat);
    for(long i=0;i<siz;i++)
    {
        dat[i]=(long)pow(dat[i],publickey)%modl;
    }
}
 
void rsacrypto::decrypt(char *datn)
{
    long sizz = strlen(datn);
    for(long i=0;i<sizz;i++)
    {
        datn[i]=(long)pow(datn[i],privatekey)%modl;
    }
 
}

void main()
{
	char datm[]="Hello!! , Implementing RSA";
	
	rsacrypto m;
	long prkey = m.ret_privatekey();
	long publkey = m.ret_publickey();
	long modulm = m.ret_modulus();
	
	rsacrypto jj(publkey,modulm);
	rsacrypto ll(prkey,modulm);
	
	jj.encrypt(datm);
	
	puts(datm);
	
	cout<<"\n";
	
	ll.decrypt(datm);
	
	puts(datm);
	
	
}


I face problem in line 125 (dat[i]=(long)pow(dat[i],publickey)%modl;)
and this error appear in compile:

call of overloaded `pow(char&, long int&)' is ambiguous

please help me to solve this error
Posted
Updated 3-Feb-15 23:56pm
v4
Comments
KarstenK 4-Feb-15 5:42am    
What is your question? If it doesnt work write some tests to verify your code. On Windows is a CryptoAPI avaliable.
saeedbahal 4-Feb-15 6:00am    
my question not clear?!!
CHill60 4-Feb-15 5:42am    
Where is the code you have used to overload pow?
saeedbahal 4-Feb-15 6:01am    
what?

1 solution

You should cast dat[i], for instance
C++
dat[i]=(long)pow((double)dat[i],publickey)%modl;

See pow[^].
 
Share this answer
 
Comments
saeedbahal 4-Feb-15 6:00am    
i implemen this way , but again face same error
CPallini 4-Feb-15 6:29am    
It is working fine on my system.
saeedbahal 4-Feb-15 7:59am    
oh very good
What program do you use for write your code ? what compiler do you use?
CPallini 4-Feb-15 12:14pm    
I used the following program, it compiles and runs fine both on Windows (Visual Studio 2012) and on Linux (g++ 4.7):

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
char dat[] = "012ABC";
long exp = 2;
long modl = 5;
dat[0] = (long)pow((double)dat[0], exp) % modl;
cout << (int) dat[0] << endl;

}
saeedbahal 5-Feb-15 3:33am    
ok , thanks for your attention

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900