Skip to content

Commit 1a6512b

Browse files
committed
添加从文本中读入明文,进行加密,并将加密后的结果放入新的文件中。
添加实例明文文件和程序运行后所产生的密文文件
1 parent 9bb3bd7 commit 1a6512b

File tree

11 files changed

+174
-22
lines changed

11 files changed

+174
-22
lines changed

Encryption/DSS.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,57 @@ class DSS
1111
unsigned __int64 y; //公钥
1212
unsigned __int64 r; //数字签名(r,s)
1313
unsigned __int64 s;
14+
string sig;
1415

1516
DSS() {
1617
}
1718

18-
DSS(unsigned __int64 xy, unsigned __int64 rr, unsigned __int64 ss) {
19+
DSS(unsigned __int64 xy, string sigg) {
1920
x = y = xy;
20-
r = rr;
21-
s = ss;
21+
sig = sigg;
2222
}
2323

2424
void signature(string message) {
25+
//签名
2526
srand(time(0));
2627
hash<std::string> h;
2728
unsigned __int64 k = rand() % q; //k为随机数 0<k<q
28-
//unsigned __int64 HM = 1234;
2929
r = PowMod(g, k, p) % q;
3030
s = mod_reverse(k, q)*((h(message) + x*r) % q) % q;
31+
32+
//r*1000+s 存入字符串sig
33+
int str = r * 1000 + s;
34+
char cnum[7];
35+
sprintf_s(cnum, "%d", str);
36+
sig = cnum;
37+
while (sig.size() < 6) //若不够6位则在前面补0
38+
{
39+
sig = "0" + sig;
40+
}
3141
}
3242

3343
void verify(string message) {
44+
//从sig里获取(x,s)
45+
char cnum[7];
46+
int str;
47+
strcpy(cnum, sig.substr(0, 6).c_str());
48+
sscanf(cnum, "%d", &str);
49+
r = str / 1000;
50+
s = str % 1000;
51+
52+
//验证
3453
hash<std::string> h;
35-
//unsigned __int64 HM = 1234;
3654
unsigned __int64 w = mod_reverse(s,q);
3755
unsigned __int64 u1 = h(message) * w % q;
3856
unsigned __int64 u2 = r * w % q;
3957
unsigned __int64 v = (PowMod(g, u1, p)*PowMod(y, u2, p) % p) % q; //用了a*b % n = (a % n)*(b % n) % n
4058
if (v == r) {
4159
cout << "ture" << endl;
4260
}
61+
else
62+
{
63+
cout << "false" << endl;
64+
}
4365
}
4466

4567
//产生私钥x和公钥y

Encryption/EncryptAlgorithm.cpp

Lines changed: 129 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,147 @@
11
#include <string>
22
#include <iostream>
33
#include <vector>
4+
#include <fstream>
45
#include "Vigenere.h"
56
#include "DES.h"
67
#include "RSA.h"
78
#include "MD5.h"
89
#include "DSS.h"
910
using namespace std;
11+
void printHead();
12+
void dss();
13+
void vigenere();
14+
void md5();
15+
void rsa();
16+
void des();
17+
string getText(string filename);
18+
void writeCiphertext(string ciphertext, string fileName);
1019
int main()
1120
{
1221
string plaintext = "hello nsfjnoiwjwljelk";
13-
string ciphertext = "";
14-
string keyword = "123456";
15-
DSS dss;
16-
dss.generateKey();
17-
dss.signature(plaintext);
18-
dss.verify(plaintext);
19-
20-
//cout << "ÃÜÎÄÊÇ£º" << endl;
21-
//cout << d.ciphertext << endl;
22-
//d.Decrypt(d.ciphertext);
23-
//cout << "Ã÷ÎÄÊÇ£º" << endl;
24-
//cout << d.plaintext << endl;
22+
printHead();
23+
int a;
24+
while (cin>>a)
25+
{
26+
switch (a)
27+
{
28+
case 1:vigenere(); break;
29+
case 2:des(); break;
30+
case 3:rsa(); break;
31+
case 4:md5(); break;
32+
case 5:dss(); break;
33+
default:
34+
break;
35+
}
36+
if (a==6)
37+
{
38+
cout << "谢谢使用" << endl;
39+
break;
40+
}
41+
system("pause");
42+
system("cls");
43+
printHead();
44+
}
2545
system("pause");
2646
return 0;
2747
}
2848

49+
void printHead() {
50+
cout << "---------------------------------------" << endl;
51+
cout << "- 1.Vigenere加密算法 -" << endl;
52+
cout << "- 2.DES加密算法 -" << endl;
53+
cout << "- 3.RSA加密算法 -" << endl;
54+
cout << "- 4.MD5加密算法 -" << endl;
55+
cout << "- 5.DSS签名算法 -" << endl;
56+
cout << "- 6.退出 -" << endl;
57+
cout << "---------------------------------------" << endl;
58+
cout << "明文内容为:Hello are you OK i am lei jun\n" << endl;
59+
cout << "选择:";
60+
}
61+
62+
void dss() {
63+
cout << "\n*** DSS加密算法 ***" << endl;
64+
cout << "随机生产公钥私钥对:" << endl;
65+
DSS d(1,"1");
66+
d.generateKey();
67+
d.signature(getText("plaintext.txt"));
68+
cout << "公钥签名后签名为(r,s):" << "(" << d.r << "," << d.s << ")" << endl;
69+
writeCiphertext(d.sig, "ciphertext.dss");
70+
cout << "签名验证:" << endl;
71+
d.verify(getText("plaintext.txt"));
72+
cout << "签名已写入 ciphertext.dss 文件" << endl;
73+
}
74+
75+
void vigenere() {
76+
string keyword;
77+
cout << "\n*** Vigenere加密算法 ***" << endl;
78+
cout << "输入密钥(一个英文单词):" << endl;
79+
cin >> keyword;
80+
Vigenere v(keyword);
81+
v.Encrypt(getText("plaintext.txt"));
82+
cout << "加密后密文为:" << v.ciphertext << endl;
83+
writeCiphertext(v.ciphertext, "ciphertext.vig");
84+
v.Decrypt(getText("ciphertext.vig"));
85+
cout << "解密后明文为:" << v.plaintext << endl;
86+
cout << "密文已写入 ciphertext.vig 文件" << endl;
87+
}
88+
89+
void md5() {
90+
string keyword;
91+
cout << "\n*** MD5加密算法 ***" << endl;
92+
MD5 m;
93+
m.Encrypt(getText("plaintext.txt"));
94+
cout << "加密后密文为:" << m.ciphertext << endl;
95+
writeCiphertext(m.ciphertext, "ciphertext.md5");
96+
cout << "密文已写入 ciphertext.md5 文件" << endl;
97+
}
98+
99+
void rsa() {
100+
string keyword;
101+
cout << "\n*** RSA加密算法 ***" << endl;
102+
cout << "随机生产公钥私钥对:" << endl;
103+
RSA r(1,1);
104+
r.generateKey();
105+
r.Encrypt(getText("plaintext.txt"));
106+
cout << "公钥加密后密文为:" << r.ciphertext << endl;
107+
writeCiphertext(r.ciphertext, "ciphertext.rsa");
108+
r.Decrypt(getText("ciphertext.rsa"));
109+
cout << "私钥解密后明文为:" << r.plaintext << endl;
110+
cout << "密文已写入 ciphertext.rsa 文件" << endl;
111+
112+
}
113+
114+
void des() {
115+
string keyword;
116+
cout << "\n*** DES加密算法 ***" << endl;
117+
cout << "输入密钥:" << endl;
118+
cin >> keyword;
119+
DES d(keyword);
120+
d.Encrypt(getText("plaintext.txt"));
121+
cout << "加密后密文为:" << d.ciphertext << endl;
122+
writeCiphertext(d.ciphertext, "ciphertext.des");
123+
d.Decrypt(getText("ciphertext.des"));
124+
cout << "解密后明文为:" << d.plaintext << endl;
125+
cout << "密文已写入 ciphertext.des 文件" << endl;
126+
}
127+
128+
//读入文本
129+
string getText(string filename) {
130+
ifstream infile(filename);
131+
string plaintext;
132+
getline(infile, plaintext);
133+
infile.close();
134+
return plaintext;
135+
}
136+
137+
//创建并写入文本
138+
void writeCiphertext(string ciphertext, string fileName) {
139+
ofstream outfile(fileName, ofstream::out);
140+
outfile << ciphertext;
141+
outfile.close();
142+
}
143+
144+
145+
29146

30147

Encryption/Encryption.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@
149149
<ItemGroup>
150150
<ClCompile Include="EncryptAlgorithm.cpp" />
151151
</ItemGroup>
152+
<ItemGroup>
153+
<Text Include="plaintext.txt" />
154+
</ItemGroup>
152155
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
153156
<ImportGroup Label="ExtensionTargets">
154157
</ImportGroup>

Encryption/Encryption.vcxproj.filters

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@
3939
<Filter>源文件</Filter>
4040
</ClCompile>
4141
</ItemGroup>
42+
<ItemGroup>
43+
<Text Include="plaintext.txt">
44+
<Filter>资源文件</Filter>
45+
</Text>
46+
</ItemGroup>
4247
</Project>

Encryption/RSA.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class RSA
1515
unsigned __int64 b; //a对于φ(n)的模反元素
1616
unsigned __int64 a = 65537;
1717

18+
1819
RSA(int nn, int ab) {
1920
n = nn;
2021
b = a = ab;
@@ -68,11 +69,9 @@ class RSA
6869
p = prime[rand() % 90];
6970
q = prime[rand() % 90];
7071
n = p * q;
71-
cout << "私钥:" << endl;
72-
cout << "(" << n << "," << a <<")"<< endl;
73-
cout << "公钥:" << endl;
74-
unsigned __int64 b = mod_reverse(a, (p - 1)*(q - 1));
75-
cout << "(" << n << "," << b << ")" << endl;
72+
b = mod_reverse(a, (p - 1)*(q - 1));
73+
cout << "私钥:" << "(" << n << "," << a <<")"<< endl;
74+
cout << "公钥:" << "(" << n << "," << b << ")" << endl;
7675
}
7776

7877

Encryption/ciphertext.des

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
QK�Q���|�R jv@�2���G|KV�[f��

Encryption/ciphertext.dss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
043014

Encryption/ciphertext.md5

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
edd12a79ee29797ee0b539e90eae7c43

Encryption/ciphertext.rsa

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
009511166015201575038263070514288127073860343157027859045371260236096316097682129031154291

Encryption/ciphertext.vig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�y���Fg�:���L)I��Q[,��

0 commit comments

Comments
 (0)