加密代码实现:
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 |
#include <iostream> #include <string> #include <unistd.h> using namespace std; int Check(string s) { //检查输入的字符串是否为纯英文 int i = 0; while (s[i] != '\0') { if (!((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z'))) { cout << "Error!Please enter again!" << endl; sleep(1); return 1; } i++; } return 0; } char(*vigenereTable(char(&list)[26][26]))[26]{ //返回二维数组 int i, j, k; char ch; for (i = 0; i < 26; i++) { for (j = 0; j < 26; j++) { k = 'a' + (i + j) % 26; ch = k; list[i][j] = ch; } } cout << " a b c d e f g h i j k l m n o p q r s t u v w x y z" << endl; for (i = 0; i < 26; i++) { cout << list[0][i] << " "; for (j = 0; j < 26; j++) { cout << list[i][j] << " "; } cout << endl; } cout << endl; return list; } int main(){ int i, m, n; int flag = 0; string st, key; char(*list)[26]; string rule(50, '\0'); char list1[26][26] = { 0 }; do { fflush(stdin); cout << "-------------Autokey plaintext(Encryption)------------" << endl; cout << endl << "Vigenere table" << endl << endl; list = vigenereTable(list1); cout << "Please enter the plaintext:" << endl; cout << "Plaintext:"; cin >> st; flag = Check(st); } while (flag); do { //输入key fflush(stdin); cout << "Please enter the key: "; cin >> key; flag = Check(key); } while (flag); for (i = 0; i < key.length(); i++) { rule[i] = key[i]; } rule[i] = '\0'; for (i = 0; i < st.length() - key.length(); i++) { rule[i + key.length()] = st[i]; } rule[i + key.length()] = '\0'; cout << endl; cout << "---------------------------------------" << endl; cout << "Keyword: " << rule << endl; cout << "Plaintext: " << st << endl; cout << "---------------------------------------" << endl << endl; cout << "Ciphertext: "; for (i = 0; i < st.length(); i++) { m = rule[i] - 'a'; n = st[i] - 'a'; cout << list[n][m]; } cout << endl << endl; return 0; } |
解密代码实现:
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 |
#include <iostream> #include <string> #include <unistd.h> using namespace std; int Check(string s) { //检查输入的字符串是否为纯英文 int i = 0; while (s[i] != '\0') { if (!((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z'))) { cout << "Error!Please enter again!" << endl; sleep(1); return 1; } i++; } return 0; } char(*vigenereTable(char(&list)[26][26]))[26]{ //返回二维数组 int i, j, k; char ch; for (i = 0; i < 26; i++) { for (j = 0; j < 26; j++) { k = 'a' + (i + j) % 26; ch = k; list[i][j] = ch; } } cout << " a b c d e f g h i j k l m n o p q r s t u v w x y z" << endl; for (i = 0; i < 26; i++) { cout << list[0][i] << " "; for (j = 0; j < 26; j++) { cout << list[i][j] << " "; } cout << endl; } cout << endl; return list; } int main(){ int i, j, m, n; int flag = 0; string st, key; char(*list)[26]; string pl(50, '\0'); string rule(50, '\0'); char list1[26][26] = { 0 }; do { fflush(stdin); cout << "-------------Autokey plaintext(Decryption)------------" << endl; cout << endl << "Vigenere table" << endl << endl; list = vigenereTable(list1); cout << "Please enter the ciphertext:" << endl; cout << "Ciphertext:"; cin >> st; flag = Check(st); } while (flag); do { //输入key fflush(stdin); cout << "Please enter the key: "; cin >> key; flag = Check(key); } while (flag); //解密与对应关系同时进行 for (i = 0; i < key.length(); i++) { rule[i] = key[i]; } rule[i] = '\0'; for (i = 0; i < st.length(); i++) { m = rule[i] - 'a'; for (j = 0; j < 26; j++) { if (list[j][m] == st[i]) break; } n = 'a' + j; pl[i] = n; if (i < st.length() - key.length()) { rule[i + key.length()] = n; rule[i + key.length() + 1] = '\0'; } //rule[i + key.length()] = st[i]; } pl[i] = '\0'; cout << endl; cout << "---------------------------------------" << endl; cout << "Keyword: " << rule << endl; cout << "Ciphertext: " << st << endl; cout << "---------------------------------------" << endl << endl; cout << "Plaintext: "; cout << pl; cout << endl << endl; return 0; } |
(编译运行环境:g++ [Debian 7.3.0-19] 7.3.0)
说点什么