C++ 数字和字符值的RSA算法
我不是编程专家。我对密码学还不熟悉,而且我已经学习了RSA的安全算法。我这样写代码:C++ 数字和字符值的RSA算法,c++,algorithm,C++,Algorithm,我不是编程专家。我对密码学还不熟悉,而且我已经学习了RSA的安全算法。我这样写代码: #include<math.h> #include<iostream> #include<cmath> #include<Windows.h> using namespace std; class rsacrypto { long publickey; long privatekey; long modl; //Modulus
#include<math.h>
#include<iostream>
#include<cmath>
#include<Windows.h>
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 genrndnum(int, int);
int totient(int);
int gcd (int, int);
int mulinv(int, int);
boolean isPrime(long);
};
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(1,10);
Sleep(1000);
p2 = genrndprimes(1,10);
n = p1*p2;
phi = (p1-1)*(p2-1);
e = genrndnum(2,(phi-1));
while(gcd(e,phi)!=1)
{
e = genrndnum(2,(phi-1));
}
d = mulinv(e, phi);
cout<<"Public Key=("<<e<<","<<n<<")"<<"\n";
cout<<"Private Key=("<<d<<","<<n<<")"<<"\n";
privatekey = e;
publickey = d;
modl = n;
int m=11;
int en=0, decr=0;
//Encryption
en=(long)pow((double)m,d)%n;
cout<<en<<"\n";
//Decryption
decr=(long)pow((double)en,e)%n;
cout<<decr;
}
/*
void rsacrypto::encrypt(char *dat)
{
long siz = strlen(dat);
for(long i=0;i<siz;i++)
{
dat[i]=(long)pow((double)dat[i],publickey)%modl;
cout<<i<<"="<<dat[i]<<"\n";
}
}
void rsacrypto::decrypt(char *datn)
{
long sizz = strlen(datn);
for(long i=0;i<sizz;i++)
{
datn[i]=(long)pow((double)datn[i],privatekey)%modl;
}
cout<<datn;
}*/
int rsacrypto::mulinv(int a, int b)
{
int b0 = b, t, q;
int x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
int rsacrypto::genrndprimes(int a, int b){
long pivot;
do{
pivot= rand() % b + a;
if (isPrime(pivot))
return pivot;
} while (1==1);
}
boolean rsacrypto::isPrime(long pivot) {
if(pivot <= 1)
return false;
int root = sqrt((double)pivot);
//start at 2 because all numbers are divisible by 1
for(int x = 2; x <= root; x++) //You only need to check up to and including the root
{
if(pivot % x == 0)
return false;
}
return true;
}
int rsacrypto::genrndnum(int a, int b){
long pivot;
pivot= rand() % b + a;
return pivot;
}
int rsacrypto::gcd ( int a, int b )
{
int c;
while ( a != 0 ) {
c = a; a = b%a; b = c;
}
return b;
}
void main()
{
rsacrypto m;
system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
类rsa加密
{
长公钥;
长私钥;
长模;//模
公众:
rsacrypto();//仅用于生成私钥和公钥。
rsacypto(long&,long&,long&);//仅用于生成私钥和公钥。
rsacypto(长密钥,长模)//在使用密钥加密或解密数据时应使用。
{
公钥=私钥=密钥;
modl=模量;
}
长ret_公钥()
{
返回公钥;
}
long ret_privatekey()
{
返回私钥;
}
长雷诺模量()
{
返回模式;
}
无效加密(字符*);
无效解密(字符*);
int genrndpremes(int,int);
int genrndnum(int,int);
int-toticent(int);
int gcd(int,int);
int mulinv(int,int);
布尔互质(长);
};
rsacypto::rsacypto()
{
长p1,p2;//素数
长n=0;//模
long phi=0;//ToClient值。
长e=0;//公钥指数。
长d=0;//私钥指数。
p1=genrndprimes(1,10);
睡眠(1000);
p2=genrndprimes(1,10);
n=p1*p2;
φ=(p1-1)*(p2-1);
e=genrndnum(2,(phi-1));
while(gcd(e,phi)!=1)
{
e=genrndnum(2,(phi-1));
}
d=mulinv(e,φ);
cout我猜您的问题是首先将双值(十六进制十进制值)转换为字符值。然后您可以使用现有代码对字符值进行加密/解密
有两种方法可以将双精度值转换为字符值:
- 将每个双精度字符转换为两个字符作为其可打印/可读形式,例如123.455->“123.456”
我引用了以下代码:
或重新解释演员阵容:
char* b = reinterpret_cast<double*>(d);
char*b=reinterpret\u cast(d);
现在,在将双精度值转换为字符值后,我们可以直接利用现有代码对数据进行加密。@user1658435您可以查看我的答案并给出一些反馈吗?
union {
double d[2];
char b[sizeof(double) * 2];
};
char* b = reinterpret_cast<double*>(d);