📄 rsa.h
字号:
#ifndef RSA_H
#define RSA_H
#include<fstream>
#include<cstdio>
using namespace std;
#include"BigInt.h"
class rsa{
public:
rsa();
~rsa();
void setkey(const BigInt& prp, const BigInt& prq, const BigInt& pubk);
void setpubkey(const BigInt& pubk, const BigInt& n);
void setMFileName(char* rsa_m);
void setCFileName(char* rsa_c);
void DeCode(int choose, int base);
void EnCode(int choose, int base);
private:
void init();
void DeCodingProcess(int choose, int base);
void DeCodingSpecial(int choose, int base);
void EnCodingProcess(int base);
void EnCodingSpecial(int base);
BigInt primeP;
BigInt primeQ;
BigInt modn;
BigInt pubkey;
BigInt prikey;
BigInt fp;
BigInt fq;
char* rsach;
char rsacFile[30];
char rsamFile[30];
int rsal;
};
rsa::rsa()
{
primeP = 952559;
primeQ = 999983;
pubkey = 11;
init();
rsach = NULL;
strcpy(rsacFile, "c.txt");
strcpy(rsamFile, "m.txt");
}
rsa::~rsa()
{
primeP.clear();
primeQ.clear();
modn.clear();
pubkey.clear();
prikey.clear();
fp.clear();
fq.clear();
}
void rsa::init()
{
modn = primeP * primeQ;
--primeP;
--primeQ;
prikey = primeP * primeQ;
prikey = pubkey.BIinverse(prikey);
++primeP;
++primeQ;
rsal = modn.nodeNumber();
rsal *= 32;
intNode* t = modn.getHead();
if(TheLastNode(t))
rsal -= FindZeroBit(t->num);
fp = primeP.BIinverse(primeQ);
fq = primeQ.BIinverse(primeP);
}
void rsa::setkey(const BigInt& prp, const BigInt& prq, const BigInt& pubk)
{
primeP.copy(prp);
primeQ.copy(prq);
pubkey.copy(pubk);
init();
}
void rsa::setpubkey(const BigInt& pubk, const BigInt& n)
{
pubkey.copy(pubk);
modn.copy(n);
rsal = modn.nodeNumber();
rsal *= 32;
intNode* t = modn.getHead();
if(TheLastNode(t))
rsal -= FindZeroBit(t->num);
}
void rsa::setMFileName(char* rsa_m)
{
strcpy(rsamFile, rsa_m);
}
void rsa::setCFileName(char* rsa_c)
{
strcpy(rsacFile, rsa_c);
}
//1. represent number based on decimal.
//2. represent number based on hex.
//3. represent with a = 01, b = 02........... to decimal
void rsa::EnCode(int choose,int base)
{
switch(choose)
{
case 1:
EnCodingProcess(base);
break;
case 2:
EnCodingSpecial(base);
break;
default:
break;
}
}
void rsa::DeCode(int choose, int base)
{
switch(choose)
{
case 1:
DeCodingProcess(choose, base);
break;
case 2:
DeCodingProcess(choose, base);
break;
case 3:
DeCodingSpecial(choose, base);
break;
case 4:
DeCodingSpecial(choose, base);
break;
default:
break;
}
}
void rsa::EnCodingProcess(int base)
{
FILE* fp;
if( (fp = fopen(rsamFile, "r") ) == NULL)
return;
fstream out;
out.open(rsacFile, ios::out);
int r_length;
r_length = rsal / 8;
rsach = new char[r_length];
BigInt temp;
char* rsa_re;
int rsa_leng;
int count = 0;
int i;
int times = 0;
while(! feof(fp))
{
rsach[count++] = fgetc(fp);
if(count == r_length)
{
++times;
cout << "第 " << times << " 轮加密!";
count = 0;
temp.loadSTR(rsach, r_length);
temp = BImodexp2(temp, pubkey, modn);
if(base == 10)
rsa_re = temp.outputDEC(rsa_leng);
else
rsa_re = temp.outputHEX(rsa_leng);
for(i = 0; i < rsa_leng; ++i)
out << rsa_re[i];
delete[] rsa_re;
out << endl;
}
}
++times;
cout << "第 " << times << " 轮加密!";
temp.loadSTR(rsach, count);
temp = BImodexp2(temp, pubkey, modn);
if(base == 10)
rsa_re = temp.outputDEC(rsa_leng);
else
rsa_re = temp.outputHEX(rsa_leng);
for(i = 0; i < rsa_leng; ++i)
out << rsa_re[i];
delete[] rsa_re;
delete[] rsach;
temp.clear();
out.close();
fclose(fp);
}
void rsa::DeCodingProcess(int choose, int base)
{
fstream out;
fstream in;
in.open(rsacFile, ios::in);
out.open(rsamFile, ios::out);
BigInt temp;
BigInt temp1;
BigInt temp2;
int r_length;
char* rr;
r_length = rsal * 3 / 10;
rsach = new char[r_length + 5];
int times = 0;
while(in >> rsach)
{
++times;
cout << "第 " << times << " 轮解密!";
if(base == 10)
temp.loadDEC(rsach, strlen(rsach));
else
temp.loadHEX(rsach, strlen(rsach));
if(choose == 1)
temp = BImodexp2(temp, prikey, modn);
else
{
temp1 = BImodexp2(temp, prikey, primeP);
temp2 = BImodexp2(temp, prikey, primeQ);
temp1 = temp1 * fq;
temp1 = temp1 * primeQ;
temp2 = temp2 * fp;
temp2 = temp2 * primeP;
temp = temp1 + temp2;
temp = temp % modn;
}
rr = temp.outputSTR(r_length);
for(int i = 0; i < r_length; ++i)
out << rr[i];
out << endl;
delete[] rr;
}
delete[] rsach;
temp.clear();
temp1.clear();
temp2.clear();
out.close();
in.close();
}
void rsa::EnCodingSpecial(int base)
{
FILE* ffp;
if( (ffp = fopen(rsamFile, "r") ) == NULL)
return;
fstream out;
out.open(rsacFile, ios::out);
int r_length;
r_length = rsal * 3 / 20;
BigInt temp;
int rsa_leng;
int count = 0;
unsigned int rsa_int;
char rsa_c;
int i;
int times = 0;
while(! feof(ffp))
{
rsa_c = fgetc(ffp);
if((rsa_c <= 'z' && rsa_c >= 'a') || (rsa_c <= 'Z' && rsa_c >= 'A'))
{
++count;
if(rsa_c <= 'Z' && rsa_c >= 'A')
rsa_int = ((unsigned int) ( rsa_c - 'A')) + 26;
else
rsa_int = ((unsigned int) ( rsa_c - 'a'));
temp = temp * 100;
temp += rsa_int;
if(count == r_length)
{
++times;
cout << "第 " << times << " 轮加密!";
count = 0;
temp = BImodexp2(temp, pubkey, modn);
if(base == 10)
rsach = temp.outputDEC(rsa_leng);
else
rsach = temp.outputHEX(rsa_leng);
temp.setZero();
for(i = 0; i < rsa_leng; ++i)
out << rsach[i];
delete[] rsach;
out << endl;
}
}
}
temp = BImodexp2(temp, pubkey, modn);
if(base == 10)
rsach = temp.outputDEC(rsa_leng);
else
rsach = temp.outputHEX(rsa_leng);
for(i = 0; i < rsa_leng; ++i)
out << rsach[i];
delete[] rsach;
temp.clear();
out.close();
fclose(ffp);
}
void rsa::DeCodingSpecial(int choose, int base)
{
fstream out;
fstream in;
in.open(rsacFile, ios::in);
out.open(rsamFile, ios::out);
BigInt temp;
BigInt temp1;
BigInt temp2;
int r_length;
char* rr;
r_length = rsal * 3 / 10;
rsach = new char[r_length + 5];
rr = new char[r_length + 5];
unsigned int rsa_int;
int count;
int times = 0;
while(in >> rsach)
{
++times;
cout << "第 " << times << " 轮解密!";
if(base == 10)
temp.loadDEC(rsach, strlen(rsach));
else
temp.loadHEX(rsach, strlen(rsach));
if(choose == 3)
temp = BImodexp2(temp, prikey, modn);
else
{
temp1 = BImodexp2(temp, prikey, primeP);
temp2 = BImodexp2(temp, prikey, primeQ);
temp1 = temp1 * fq;
temp1 = temp1 * primeQ;
temp2 = temp2 * fp;
temp2 = temp2 * primeP;
temp = temp1 + temp2;
temp = (temp % modn);
}
count = 0;
while(! temp.BIlisZero())
{
rsa_int = temp % 100;
temp = temp / 100;
if(rsa_int >= 0 && rsa_int <= 25)
rr[count ++] = (char)(rsa_int + ((unsigned int)'a'));
else if(rsa_int >= 26 && rsa_int <= 51)
rr[count ++] = (char)(rsa_int + ((unsigned int)'A') - 26);
}
for(int i = 0; i < count; ++i)
out << rr[count - i - 1];
}
delete[] rr;
delete[] rsach;
temp.clear();
temp1.clear();
temp2.clear();
out.close();
in.close();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -