|
1 | 1 | #include <string> |
2 | 2 | #include <iostream> |
3 | 3 | #include <vector> |
| 4 | +#include <fstream> |
4 | 5 | #include "Vigenere.h" |
5 | 6 | #include "DES.h" |
6 | 7 | #include "RSA.h" |
7 | 8 | #include "MD5.h" |
8 | 9 | #include "DSS.h" |
9 | 10 | 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); |
10 | 19 | int main() |
11 | 20 | { |
12 | 21 | 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 | + } |
25 | 45 | system("pause"); |
26 | 46 | return 0; |
27 | 47 | } |
28 | 48 |
|
| 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 | + |
29 | 146 |
|
30 | 147 |
|
0 commit comments