program verification
In order to verify the correctness of study notes 3 and 4, here is a small program to verify. The values of relevant parameters are the same as those in cryptography study notes 3 and 4. The code snippets are as follows:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int p, q;
int N, f_n;
int e, d;
int C, m;
cout << "请输入p,q(p,q互质,此处为练习,p,q取值不能太大)" << endl;
cout << "p = ";
cin >> p;
cout << endl;
cout << "q = ";
cin >> q;
cout << endl;
N = p*q;
f_n = (p - 1)*(q - 1);
cout << "N = " << N << endl << "f(n) = " << f_n << " " << endl;
cout << endl;
cout << "请输入e(e与f(n)互质)" << endl;
cout << "e = ";
cin >> e;
cout << endl;
//求解私钥
d = 1;
for (d = 1;; d++)
{
if (e*d % f_n == 1)
break;
}
cout << "公钥为:" << e << " " << N << endl;
cout << "私钥为:" << d << " " << N << endl;
cout << endl;
//输入被加密的信息;
cout << "请输入明文 m = ";
cin >> m;
cout << endl;
//公钥加密,求出密文
C = m % f_n;
for (int i = 1; i < e; i++)
C = (C*m) % N;
cout << "密文为:" << C << endl;
cout << endl;
system("pause");
return 0;
}
The screenshot of the program running is as follows:
At this point, the RSA algorithm study notes are over, and the next section will learn the one-way hash algorithm, that is, the MD5 and SHA algorithms we often mention.