📄 rand.h
字号:
#ifndef RAND_H
#define RAND_H
#include"DESProcess.h"
#include"BigInt.h"
#include<fstream>
#include<iostream>
using namespace std;
class Rand{
public:
Rand()
{
num = 2;
length = 1000.;
for(int i = 0; i < 2; ++i)
k1[i] = k2[i] = x[i] = y[i] = z[i] = w[i] = v[i] = 0;
ev = true;
}
~Rand(){}
void setKeyFile(char* c)
{
fstream in;
in.open(c, ios::in);
in >> setbase(16) >> k1[0] >> k1[1] >> k2[0] >> k2[1];
in.close();
}
void setZVFile(char* c)
{
fstream in;
in.open(c, ios::in);
in >> setbase(16) >> z[0] >> z[1] >> v[0] >> v[1];
in.close();
}
BigInt randStrPrime()
{
BigInt p;
BigInt k;
BigInt temp;
cout << "生成第一个素因子.\n";
temp = randPrime();
while(k.BIlisZero())
{
k = randBigInt();
k >>= 16;
k <<= 1;
}
p = temp * k;
temp = temp * 2;
++p;
cout << "生成第二个素因子.\n";
while(!isPrime(p))
{
cout << "新的一轮!" << endl;
p = p + temp;
}
temp.copy(p);
k = randBigInt();
k <<= 96;
if(ev)
k <<= 4;
ev = !ev;
p = temp * k;
temp = temp * 2;
++p;
cout << "生成强素数.\n";
while(!isPrime(p))
{
cout << "新的一轮!" << endl;
p = p + temp;
}
cout << "强素数生成成功!"<< endl;
k.clear();
temp.clear();
return p;
}
private:
int randlength()
{
int randlength_temp;
length = length * r_l + r_cc;
length = fmod(length, r_M);
randlength_temp =(int) (length / r_MM);
if(randlength_temp == 0)
++randlength_temp;
return randlength_temp;
}
unsigned int randnumber()
{
num = r_laimda * num + r_c;
return num;
}
BigInt randBigInt()
{
BigInt a;
int ran;
ran = randlength();
for(int j = 0; j < ran; ++j)
{
a <<= 32;
a += randnumber();
}
return a;
}
void randkey(unsigned int& r, unsigned& l)
{//1
d.setKey(k1[0], k1[1]);
d.setBits(v[0], v[1]);
d.desEnCode();
d.outputBits(x[0], x[1]);
//2
d.setKey(x[0], x[1]);
d.setBits(v[0], v[1]);
d.desDeCode();
d.outputBits(w[0], w[1]);
//3
d.setKey(z[0], z[1]);
d.setBits(x[0], x[1]);
d.desDeCode();
d.outputBits(y[0], y[1]);
//4
d.setKey(y[0], y[1]);
d.setBits(k2[0], k2[1]);
d.desEnCode();
d.outputBits(v[0], v[1]);
//5
d.setKey(k2[0], v[1]);
d.setBits(w[0], w[1]);
d.desEnCode();
d.outputBits(r, l);
//6
d.setKey(k1[0], k1[1]);
d.setBits(z[0], z[1]);
d.desDeCode();
d.outputBits(z[0], z[1]);
}
bool isPrime(const BigInt& p)
{
BigInt t;
t.copy(p);
--t;
unsigned int last32 = t.getLast32Bit();
int count = 0;
while((last32 & 1) == 0)
{
t >>= 1;
last32 = t.getLast32Bit();
++count;
}
BigInt a;
BigInt re;
int i, j;
bool flag;
unsigned int con = 1;
char chd;
for(i = 0; i < 100; ++i)
{
flag = false;
if(i < 10)
a = sta_p[i];
else
{
if(i == 10)
{
cout << "前10次检测已经通过,要进行100次的检测吗(这可能很耗时间)(y/n)?" << endl;
cin >> chd;
if(chd == 'n' || chd == 'N')
break;
}
a = randBigInt();
}
a.print(1);
cout << "第 " << setbase(10) << i + 1 << " 次检查!"<< endl;
re = BImodexp(a, t, p);
if(count > 1)
{
if(re.BICompareABS(con) == 0)
continue;
++re;
if(re.BICompareABS(p) == 0)
continue;
--re;
for(j = 2; j < count; ++j)
{
re = BImodmul2(re, re, p);
if(re.BICompareABS(con) == 0)
{
t.clear();
a.clear();
re.clear();
return false;
}
++re;
if(re.BICompareABS(p) == 0)
{
flag = true;
break;
}
--re;
}
re = BImodmul2(re, re, p);
}
++re;
if(flag)
continue;
if(re.BICompareABS(p) != 0)
{
t.clear();
a.clear();
re.clear();
return false;
}
}
t.clear();
a.clear();
re.clear();
return true;
}
BigInt randPrime()
{
int i;
BigInt p;
unsigned int right, left;
unsigned int temp;
for(i = 0; i < 2; ++i)
{
randkey(right, left);
p <<= 32;
p += right;
p <<= 32;
p += left;
}
temp = p % 30;
p += (29 - temp);
while(true)
{//30 as a circle
cout << "新的一轮!" << endl;
p += 2;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 6;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 4;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 2;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 4;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 2;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 4;
if(isPrime(p))
return p;
cout << "新的一轮!" << endl;
p += 6;
if(isPrime(p))
return p;
}
}
unsigned int num;
double length;
unsigned int k1[2];
unsigned int k2[2];
unsigned int x[2];
unsigned int y[2];
unsigned int z[2];
unsigned int w[2];
unsigned int v[2];
bool ev;
DESProcess d;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -