?? rsa.cpp
字號:
/***********************************************************************
RSA 加密與解密
1、p , q 均是 " << bits <<" bits 的大素數(概率素數);
2、加密密鑰 e 是 100 bits 的大整數;
3、明文文件, 密文文件及解密文件均存放于當前目錄下;
***********************************************************************/
#include"RSA.H"
# pragma comment (lib, "NTL-5.3.2\\NTL532.lib")
#define BITS 256 //要求概率素數 p , q 的比特位數達 BITS bits
void main()
{
ZZ p, q ; //兩個 BITS 比特的隨機概率素數 p , q
ZZ n, e, d, euler; // n=p*q, euler=(p-1)*(q-1), e---加密密鑰, d---解密密鑰
int bits = BITS; //要求概率素數為 BITS bits
cout<<endl;
cout<<"**********************************************************"<<endl;
cout<<" NTL 庫實現 RSA 加密與解密 "<<endl;
cout<<" 1、p , q 均是 " << bits <<" bits 的大素數(概率素數); "<<endl;
cout<<" 2、加密密鑰 e 是 100 bits 的大整數; "<<endl;
cout<<" 3、明文文件, 密文文件及解密文件均存放于當前目錄下; "<<endl;
cout<<"**********************************************************"<<endl;
system("cls");
RandomPrime( p, bits, 10 ); RandomPrime( q, bits, 10 );
n=p*q; euler=(p-1)*(q-1); //計算 n=p*q 及 歐拉函數值 euler(n)=(p-1)*(q-1)
KeyGeneration(d, e, euler); //生成密鑰: d, e , where d*e=1 Mod euler(n)
SaveKey(e, d, n); //保存公鑰及私鑰到文件. 注意: PU={e,n}, PR={d,n}
//注意: 公鑰 KU={e,n}, 用于加密方
// 私鑰 KR={d,n}, 用于解密方
Encryption(e, n); //加密: C=M^e(Mod n)
Decryption(d, n); //解密: M=C^d(Mod n)
cout<<endl;
cout<<"**********************************************************"<<endl;
cout<<" NTL 庫實現 RSA 加密與解密 "<<endl;
cout<<" 1、p , q 均是 " << bits <<" bits 的大素數(概率素數); "<<endl;
cout<<" 2、加密密鑰 e 是 100 bits 的大整數; "<<endl;
cout<<" 3、明文文件, 密文文件及解密文件均存放于當前目錄下; "<<endl;
cout<<"**********************************************************"<<endl;
cout<<"\n程序執行完畢!"<<endl;
cout<<"*********** 以下所有文件均保存于當前目錄下 ***********"<<endl;
cout<<"\t 明文文件: "<< file1 <<endl;
cout<<"\t 密文文件: "<< file2 <<endl;
cout<<"\t 解密文件: "<< file3 <<endl;
cout<<"\t RSA 加密密鑰文件: PublicKey.txt" << endl;
cout<<"\t RSA 解密密鑰文件: PrivateKey.txt" << endl;
cout<<endl;
cout<<"******************************************************"<<endl;
cout<<endl; system("pause");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -