📄 rsa.cpp
字号:
#include "rsa.h"
////////////////////*************** 产生RSA秘钥空间 **************////////////////////////////
///*** 1)生成秘钥空间
void GenerateRSAKey( )
{
unsigned int p, q, n,fn,e,d ; //rsa秘钥空间
p = GetPrim( LENGTH );
q = GetPrim( LENGTH );
while(p == q)
{
q = GetPrim( LENGTH);
}
n = p * q ;
fn = (p-1)*(q-1);
srand(time(NULL));
do
{
do
{
e = rand()%(fn - 100) + 100;
if( e > pow(2 ,8) || e == 1)
continue;
if(gcd(fn,e) == 1)
break;
}while(1);
d = Inverse(fn,e);
if(d > 0)
break;
}while(1);
cout<<" 秘钥空间为: \n";
cout <<"p " << p << endl
<< "q " << q << endl
<< "n " << n << endl
<< "e " << e << endl
<< "d " << d << endl;
}
///*** 2)生成素数
unsigned int GetPrim( unsigned int a)
{
unsigned int x = 0;
while( 1 )
{
x = GetOdd( a );
if(SolovayStrassen(x, 0) || SolovayStrassen((x-1)/2 , 0) )
return x;
}
}
///**** 3) 生成奇数
unsigned int GetOdd( unsigned int a)
{
srand(time(NULL));
unsigned int x;
while(1)
{
x = rand();
if( x >= pow(2, a) && x <= pow(2,a+1) && x%2 != 0)
return x;
}
}
///*** 4)素性检测
bool SolovayStrassen( unsigned int n, unsigned int count)
{
if(count < 10)
{
unsigned int a= rand()%(n-2) + 2;
if(gcd(n,a) > 1)
return true;
else
{
unsigned int JacobiValue = Jacobi(n,a);
if(JacobiValue == -1)
JacobiValue = n - 1;
unsigned int y = SquareAndMultiply( a, (n-1)/2, n);
if(JacobiValue == y)
{
count++;
if(!SolovayStrassen(n,count))
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
}
else
{
return false;
}
}
template <class T>
T Jacobi(T n, T a) // n > a --- n is odd
{
T state = 1;
T temp = 0;
do
{
if(a%2 != 0) // if a and n are all odds...
{
temp = n;
n = a;
a = temp; // property 4 --- swap a and n...
if(((a%4 == 3) && (n%4 == 3)))
{
state *= -1;
}
a = a%n; // property 1 --- a = a mod n...
}
else if(a%2 == 0)
{
unsigned int k=1;
while((a /= 2)%2 == 0)
{
k++;
} // property 3 --- get the exponent of 2 and the odd a...
if((n%8 == 3 || n%8 == 5))
state *= ((k%2 == 0)?1:-1); // property 2 -- get -1 or 1 of 2/n...
}
if(a == 1)
{
return state;
}
else if(a == 2)
{
return (n%8 == 3 || n%8 == 5)?((-1)*state):state;
}
}while(1);
}
///*** 5) 求最大公约数
unsigned int gcd( unsigned int a, unsigned int b)
{
unsigned int c;
for(c = a % b ; c > 0 ; c = a % b)
{
a = b;
b = c;
}
return b;
}
///*** 6) 求逆
unsigned int Inverse( unsigned int a, unsigned int b)
{
unsigned int a0 = a, b0 = b;
unsigned int t0 = 0, t = 1;
unsigned int m = a0/b0;
unsigned int r = a0 - m*b0;
unsigned int temp;
while(r > 0)
{
temp = t0 - m*t;
t0 = t;
t = temp;
a0 = b0;
b0 = r;
m= a0/b0;
r = a0 - m*b0;
}
do
{
if(t < 0)
{
t += a;
}
else
{
break;
}
}while(1);
t %= a;
return (b0==1)?t:0;
}
///*** 7)快速求指数
unsigned int SquareAndMultiply( unsigned int BaseNum, unsigned int exponent, unsigned int ModN)
{
unsigned int result = 1;
unsigned int ExpLength = sizeof(exponent)*8;
unsigned int temp = 1;
temp <<= (ExpLength-1);
for( unsigned int i=0; i<ExpLength; i++)
{
result = (result*result)%ModN;
result = temp & exponent ? (result*BaseNum)%ModN : result;
exponent <<= 1;
}
return result;
}
///////////*************RSA加密解密****************//////////////////////////////
///*** 1)加密
void RSAEncrypt( unsigned int key , unsigned int n ,string filename) //key加密秘钥 filename加密文件
{
FILE *fp, *fc;
if((fp = fopen(filename.c_str(), "rb")) == NULL)
{
cout << "file "<< filename<< " open failed!" << endl;
exit(0);
}
if((fc = fopen("cipher.txt", "wb")) == NULL)
{
cout << "file cipher.txt write open failed!" << endl;
exit(0);
}
unsigned char Idata;
unsigned int Odata;
fread(&Idata,sizeof(unsigned char),1,fp);
do
{
Odata = SquareAndMultiply(Idata, key, n);
fwrite(&Odata,4,1,fc);
fread(&Idata,sizeof(unsigned char),1,fp);
}while(!feof(fp));
fclose(fp);
fclose(fc);
cout << "Encrypt end !\nThe file has been encrypted to cipher.txt" << endl;
}
///*** 2) 解密
void RSADecrypt( unsigned int key , unsigned int n , string filename) //key解密秘钥 filename加密文件
{
FILE *fp, *fc;
if((fp = fopen(filename.c_str(), "rb")) == NULL)
{
cout << "file "<<filename <<" open failed!" << endl;
exit(0);
}
if((fc = fopen("deplain.txt", "wb")) == NULL)
{
cout << "file deplain.txt open failed!" << endl;
exit(0);
}
unsigned int Idata;
unsigned char Odata;
fread(&Idata,sizeof( unsigned int),1,fp);
do
{
Odata =(unsigned char)SquareAndMultiply(Idata, key, n);
fwrite(&Odata,1,1,fc);
fread(&Idata,sizeof( unsigned int),1,fp);
}while(!feof(fp));
fclose(fp);
fclose(fc);
cout << "Decrypt end !\nThe file has been decrypted to deplain.txt" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -