Click here to Skip to main content
15,896,450 members

implement RSA using a C++

saeedbahal asked:

Open original thread
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
Tags: C++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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