forked from gao27024037/Encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptAlgorithm.cpp
More file actions
146 lines (132 loc) · 3.55 KB
/
EncryptAlgorithm.cpp
File metadata and controls
146 lines (132 loc) · 3.55 KB
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
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include "Vigenere.h"
#include "DES.h"
#include "RSA.h"
#include "MD5.h"
#include "DSS.h"
using namespace std;
void printHead();
void dss();
void vigenere();
void md5();
void rsa();
void des();
string getText(string filename);
void writeCiphertext(string ciphertext, string fileName);
int main()
{
printHead();
int a;
while (cin>>a)
{
switch (a)
{
case 1:vigenere(); break;
case 2:des(); break;
case 3:rsa(); break;
case 4:md5(); break;
case 5:dss(); break;
default:
break;
}
if (a==6)
{
cout << "谢谢使用" << endl;
break;
}
system("pause");
system("cls");
printHead();
}
system("pause");
return 0;
}
void printHead() {
cout << "---------------------------------------" << endl;
cout << "- 1.Vigenere加密算法 -" << endl;
cout << "- 2.DES加密算法 -" << endl;
cout << "- 3.RSA加密算法 -" << endl;
cout << "- 4.MD5加密算法 -" << endl;
cout << "- 5.DSS签名算法 -" << endl;
cout << "- 6.退出 -" << endl;
cout << "---------------------------------------" << endl;
cout << "明文内容为:"+ getText("plaintext.txt") +"\n" << endl;
cout << "选择:";
}
void dss() {
cout << "\n*** DSS加密算法 ***" << endl;
cout << "随机生产公钥私钥对:" << endl;
DSS d(1,"1");
d.generateKey();
d.signature(getText("plaintext.txt"));
cout << "公钥签名后签名为(r,s):" << "(" << d.r << "," << d.s << ")" << endl;
writeCiphertext(d.sig, "ciphertext.dss");
cout << "签名验证:" << endl;
d.verify(getText("plaintext.txt"));
cout << "签名已写入 ciphertext.dss 文件" << endl;
}
void vigenere() {
string keyword;
cout << "\n*** Vigenere加密算法 ***" << endl;
cout << "输入密钥(一个英文单词):" << endl;
cin >> keyword;
Vigenere v(keyword);
v.Encrypt(getText("plaintext.txt"));
cout << "加密后密文为:" << v.ciphertext << endl;
writeCiphertext(v.ciphertext, "ciphertext.vig");
v.Decrypt(getText("ciphertext.vig"));
cout << "解密后明文为:" << v.plaintext << endl;
cout << "密文已写入 ciphertext.vig 文件" << endl;
}
void md5() {
string keyword;
cout << "\n*** MD5加密算法 ***" << endl;
MD5 m;
m.Encrypt(getText("plaintext.txt"));
cout << "加密后密文为:" << m.ciphertext << endl;
writeCiphertext(m.ciphertext, "ciphertext.md5");
cout << "密文已写入 ciphertext.md5 文件" << endl;
}
void rsa() {
string keyword;
cout << "\n*** RSA加密算法 ***" << endl;
cout << "随机生产公钥私钥对:" << endl;
RSA r(1,1);
r.generateKey();
r.Encrypt(getText("plaintext.txt"));
cout << "公钥加密后密文为:" << r.ciphertext << endl;
writeCiphertext(r.ciphertext, "ciphertext.rsa");
r.Decrypt(getText("ciphertext.rsa"));
cout << "私钥解密后明文为:" << r.plaintext << endl;
cout << "密文已写入 ciphertext.rsa 文件" << endl;
}
void des() {
string keyword;
cout << "\n*** DES加密算法 ***" << endl;
cout << "输入密钥:" << endl;
cin >> keyword;
DES d(keyword);
d.Encrypt(getText("plaintext.txt"));
cout << "加密后密文为:" << d.ciphertext << endl;
writeCiphertext(d.ciphertext, "ciphertext.des");
d.Decrypt(getText("ciphertext.des"));
cout << "解密后明文为:" << d.plaintext << endl;
cout << "密文已写入 ciphertext.des 文件" << endl;
}
//读入文本
string getText(string filename) {
ifstream infile(filename);
string plaintext;
getline(infile, plaintext);
infile.close();
return plaintext;
}
//创建并写入文本
void writeCiphertext(string ciphertext, string fileName) {
ofstream outfile(fileName, ofstream::out);
outfile << ciphertext;
outfile.close();
}