-
Notifications
You must be signed in to change notification settings - Fork 0
/
源.cpp
212 lines (210 loc) · 4.27 KB
/
源.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include<time.h>
using namespace std;
/*************************************************************
* @fn isPrime
*
* @brief 判断输入数是否为素数
*
* @data 需要判断的数
*
* @return 1-是素数,0-不是素数
*/
bool isPrime(int num)
{
int tmp = sqrt(num); //这里是利用数的开根号若不满足质数的条件,则本身必不满足的定理来缩短判断质数的时间
for (int i = 2; i <= tmp; i++)
if (num %i == 0)
return 0;
return 1;
}
/*************************************************************
* @fn isMutualPrime
*
* @brief 判断输入的两个数是否互为素数
*
* @data 需要判断的数
*
* @return 1-是,0-不是
*/
bool isMutualPrime(int num1,int num2)
{
while (num1 != num2)//这是利用两个数若互为质数,则他们的公倍数为1的定理
{
if (num1>num2)num1 = num1 - num2;
if (num1<num2)num2 = num2 - num1;
}
if (num1 == 1)
return 1;
else
return 0;
}
/*************************************************************
* @fn calculateKey
*
* @brief 生成公钥和私钥
*
* @data 两个质数,公钥和私钥的参数
*
* @return 1-生成成功,0-生成失败
*/
bool calculateKey(int &d,int &e,int &n,int num1,int num2)
{
int t = 0;
n = num1*num2;
t = (num1 - 1)*(num2 - 1);
cout << "enter ‘e’" << endl;
cin >> e;//e满足1<e<t
if (e<1 || e>t){
cout << "e is error,please input again:";
cin >> e;
}
d = 1;
while (((e*d) % t) != 1)//d是e在与n有关模下的乘法逆元
d++;
cout << "the public_key is (" << e << "," << n <<")"<< endl;
cout << "the private_key is (" << d << "," << n << ")" << endl;
return 1;
}
/*************************************************************
* @fn encryption
*
* @brief 加密
*
* @data 要加密的明文数字,加密后的数字,参数e,n,r
*
* @return 1-生成成功,0-生成失败
*/
bool encryption(int &r, int &e, int n)
{
int m = 0;
r = 1;
cout << "请输入要加密的明文数字" << endl;
cin >> m;
e = e + 1;
while (e != 1){
r = r*m;
r = r%n;
e--;
}
cout << "加密后的数字为" << r << endl;
return 1;
}
/*************************************************************
* @fn decrypt
*
* @brief 解密
*
* @data 要解密的数字,解密后的数字,参数d,n,r
*
* @return 1-生成成功,0-生成失败
*/
bool decrypt(int&r, int &d, int n)
{
int o = 1;
d = d + 1;
while (d != 1)
{
o = o*r;
o = o%n;
d --;
}
cout << "解密后的数字为" << o << endl;
return 1;
}
/*************************************************************
* @fn encryptionFile
*
* @brief 加密文档
*
* @data 参数e,n,r
*
* @return 1-生成成功,0-生成失败
*/
bool encryptionFile(int &r, int &e, int n){
clock_t start, finish;
double totaltime;
start = clock();
ifstream myfile("E:\\text1.txt");
ofstream outfile("E:\\text2.txt", ofstream::app);
string temp;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
}
while (getline(myfile, temp))
{
int i = stoi(temp);
r = 1;
e = e + 1;
while (e != 1){
r = r*i;
r = r%n;
e--;
}
outfile << r;
}
myfile.close();
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "\n加密文档所需时间为" << totaltime << "秒!" << endl;
return 1;
}
/*************************************************************
* @fn decryptFile
*
* @brief 加密文档
*
* @data 参数d,n,r
*
* @return 1-生成成功,0-生成失败
*/
bool decryptFile(int &r, int &d, int n){
clock_t start, finish;
double totaltime;
start = clock();
ifstream myfile("E:\\text2.txt");
ofstream outfile("E:\\text3.txt", ofstream::app);
string temp;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
}
while (getline(myfile, temp))
{
int i = stoi(temp);
int o = 1;
d = d + 1;
while (d != 1)
{
o = o*i;
o = o%n;
d--;
}
outfile << o;
}
myfile.close();
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "\n解密文档所需时间为" << totaltime << "秒!" << endl;
return 1;
}
int main()
{
int num1 = 0, num2 = 0,n = 0,e = 0,d = 0,r=0;
cout << "请输入两个素数。"<<endl;
cin >> num1 >> num2;
cout<<isPrime(num1)<<endl;
cout << isPrime(num1) << endl;
cout << isMutualPrime(num1,num2) << endl;
cout<<calculateKey(d,e,n,num1,num2)<<endl;
//cout << encryption(r,e,n) << endl;
//cout << decrypt(r,d,n) << endl;
cout << encryptionFile(r, e, n) << endl;
cout << decryptFile(r, d, n) << endl;
system("PAUSE ");
return 0;
}