加密代码实现:
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 |
#include <iostream> #include <string> #include <string.h> #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; return 1; } i++; } return 0; } //Playfair cipher构造matrix key char(*matrixkey(char(&matrix)[5][5]))[5]{ int i = 0,j = 0,k = 0,n = 0; string keyword; int flag = 0; string key(20, '\0'); string rule(50, '\0'); string alpha("abcdefghijklmnopqrstuvwxyz"); do { //输入keyword fflush(stdin); cout << "select a keyword:"; cin >> keyword; flag = Check(keyword); } while (flag); for (i = 0; i < keyword.length(); i++) { //提取key flag = 1; if (keyword[i] == 'j') keyword[i] = i; for (j = 0; j < i; j++) { if (key[j] == keyword[i]) flag = 0; } if (flag) key[k++] = keyword[i]; } key[k] = '\0'; //组合对应表 while (key[n] != '\0') { rule[n] = key[n]; n++; } rule[n] = '\0'; for (i = 0; i < alpha.length(); i++) { flag = 1; for (j = 0; j < key.length(); j++) { if (alpha[i] == key[j] || alpha[i] == 'j') { //i,j占据同一个字符 flag = 0; break; } } if (flag) rule[n++] = alpha[i]; } rule[n] = '\0'; // 组建 matrix key 并打印到屏幕 cout << endl << " matrix key" << endl << endl; k = 0; for (i = 0; i < 5; i++) { cout << " "; for (j = 0; j < 5; j++) { matrix[i][j] = rule[k]; cout << " " << rule[k]; k++; } cout << endl; } cout << endl << endl; return matrix; } //Playfair cipher 处理输入的明文/密文,将格式一致化 char *strtem(string st,char tem[50]) { int i = 0, j = 0; int flag = 0; //两两组合配对,检查每组元素是否相同,若相同,则添加元素'q' for (i = 0; i < st.length(); i++) { if (st[i] == 'j') { //字母j用字母i替代 tem[i] = 'i'; continue; } tem[i] = st[i]; } tem[i] = '\0'; for (i = 0; i < strlen(tem) - 1; i++) { if ((i % 2 == 0) && (tem[i] == tem[i + 1])) { //检测分组元素 for (j = strlen(tem) - 1; j >= i; j--) { tem[j + 1] = tem[j]; } tem[i + 1] = 'q'; } } //若长度为奇数,则在末尾添加元素'q',使其成为偶数 if ((strlen(tem) % 2 != 0) && (tem[strlen(tem) - 1] == 'q')) { cout << "Error!Please choose another encryption algorithm!" << endl; exit(-1); } if ((strlen(tem) % 2 != 0) && (tem[strlen(tem) - 1] != 'q')) { tem[strlen(tem)] = 'q'; } tem[strlen(tem)] = '\0'; return tem; } int main(){ int i = 0, j = 0, k = 0, n = 0, m = 0; int flag = 0; int flag1, flag2, flag3, flag4; char(*matrix)[5]; char matrix1[5][5] = { 0 }; char plain[20][2] = { 0 }; char tem[50] = { 0 }; string st; do { fflush(stdin); cout << "-------------Playfair cipher(Encryption)------------" << endl; cout << " the null letter is 'q' " << endl; cout << "Please enter the plaintext:"; cin >> st; flag = Check(st); } while (flag); strtem(st, tem); matrix = matrixkey(matrix1); //将一维数组转化为二维数组,便于加密 for (i = 0; i < strlen(tem) / 2; i++) { for (j = 0; j < 2; j++) { plain[i][j] = tem[k]; k++; } } //以下为加密 flag = i; cout << "CipherText:"; for (i = 0; i < flag; i++) { j = 0; for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { j = 0; if (matrix[m][n] == plain[i][j]) { flag1 = m; flag2 = n; } if (matrix[m][n] == plain[i][(j + 1)]) { flag3 = m; flag4 = n; } } } if (flag1 == flag3) { cout << matrix[flag1][((flag2 + 1) % 5)] << matrix[flag3][((flag4 + 1) % 5)]; } else if (flag2 == flag4) { cout << matrix[((flag1 + 1) % 5)][flag2] << matrix[((flag3 + 1) % 5)][flag4]; } else { cout << matrix[flag1][flag4] << matrix[flag3][flag2]; } } 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 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 |
#include <iostream> #include <string> #include <string.h> #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; return 1; } i++; } return 0; } //Playfair cipher构造matrix key char(*matrixkey(char(&matrix)[5][5]))[5]{ int i = 0,j = 0,k = 0,n = 0; string keyword; int flag = 0; string key(20, '\0'); string rule(50, '\0'); string alpha("abcdefghijklmnopqrstuvwxyz"); do { //输入keyword fflush(stdin); cout << "select a keyword:"; cin >> keyword; flag = Check(keyword); } while (flag); for (i = 0; i < keyword.length(); i++) { //提取key flag = 1; if (keyword[i] == 'j') keyword[i] = i; for (j = 0; j < i; j++) { if (key[j] == keyword[i]) flag = 0; } if (flag) key[k++] = keyword[i]; } key[k] = '\0'; //组合对应表 while (key[n] != '\0') { rule[n] = key[n]; n++; } rule[n] = '\0'; for (i = 0; i < alpha.length(); i++) { flag = 1; for (j = 0; j < key.length(); j++) { if (alpha[i] == key[j] || alpha[i] == 'j') { //i,j占据同一个字符 flag = 0; break; } } if (flag) rule[n++] = alpha[i]; } rule[n] = '\0'; // 组建 matrix key 并打印到屏幕 cout << endl << " matrix key" << endl << endl; k = 0; for (i = 0; i < 5; i++) { cout << " "; for (j = 0; j < 5; j++) { matrix[i][j] = rule[k]; cout << " " << rule[k]; k++; } cout << endl; } cout << endl << endl; return matrix; } //Playfair cipher 处理输入的明文/密文,将格式一致化 char *strtem(string st,char tem[50]) { int i = 0, j = 0; int flag = 0; //两两组合配对,检查每组元素是否相同,若相同,则添加元素'q' for (i = 0; i < st.length(); i++) { if (st[i] == 'j') { //字母j用字母i替代 tem[i] = 'i'; continue; } tem[i] = st[i]; } tem[i] = '\0'; for (i = 0; i < strlen(tem) - 1; i++) { if ((i % 2 == 0) && (tem[i] == tem[i + 1])) { //检测分组元素 for (j = strlen(tem) - 1; j >= i; j--) { tem[j + 1] = tem[j]; } tem[i + 1] = 'q'; } } //若长度为奇数,则在末尾添加元素'q',使其成为偶数 if ((strlen(tem) % 2 != 0) && (tem[strlen(tem) - 1] == 'q')) { cout << "Error!Please choose another encryption algorithm!" << endl; exit(-1); } if ((strlen(tem) % 2 != 0) && (tem[strlen(tem) - 1] != 'q')) { tem[strlen(tem)] = 'q'; } tem[strlen(tem)] = '\0'; return tem; } int main(){ int i = 0, j = 0, k = 0, n = 0, m = 0; int flag = 0; int flag1, flag2, flag3, flag4; char(*matrix)[5]; char matrix1[5][5] = { 0 }; char plain[20][2] = { 0 }; char tem[50] = { 0 }; string st; do { fflush(stdin); cout << "-------------Playfair cipher(decryption)------------" << endl; cout << "Please enter the ciphertext:"; cin >> st; if ((st.length() % 2) != 0) { cout << "Error!There must be even number of characters in the ciphertext!Please enter again!" << endl; exit(-1); } flag = Check(st); } while (flag); strtem(st, tem); matrix = matrixkey(matrix1); //将一维数组转化为二维数组,便于解密 for (i = 0; i < strlen(tem) / 2; i++) { for (j = 0; j < 2; j++) { plain[i][j] = tem[k]; k++; } } //以下为解密 flag = i; cout << "PlainText:"; for (i = 0; i < flag; i++) { j = 0; for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { j = 0; if (matrix[m][n] == plain[i][j]) { flag1 = m; flag2 = n; } if (matrix[m][n] == plain[i][(j + 1)]) { flag3 = m; flag4 = n; } } } //解密关键代码 if (flag1 == flag3) { cout << matrix[flag1][((flag2 + 4) % 5)] << matrix[flag3][((flag4 + 4) % 5)]; } else if (flag2 == flag4) { cout << matrix[((flag1 + 4) % 5)][flag2] << matrix[((flag3 + 4) % 5)][flag4]; } else { cout << matrix[flag1][flag4] << matrix[flag3][flag2]; } } cout << endl << endl; return 0; } |
(编译运行环境:g++ [Debian 7.3.0-19] 7.3.0)
说点什么