本文介绍了使用C ++实现RSA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C ++类实现RSA。以下是该计划:



I am trying to implement RSA using a C++ class. Below is the program :

#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);
	
	
}





我遇到第125行的问题(dat [i] =(长) )pow(dat [i],publickey)%modl;)

这个错误出现在编译中:



调用重载`pow (char&,long int&)'含糊不清



请帮我解决此错误



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

推荐答案

dat[i]=(long)pow((double)dat[i],publickey)%modl;



请参见 []。


这篇关于使用C ++实现RSA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:17