加密代码实现:
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
#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, t = 0, q = 0; int period; int flag = 0; int flag1, flag2, flag3, flag4; int flag_1, flag_2, flag_3, flag_4; char(*matrix_1)[5]; char(*matrix_2)[5]; char matrix1[5][5] = { 0 }; char matrix2[5][5] = { 0 }; char plain[20][2] = { 0 }; char test[10][30] = { 0 }; char tem[50] = { 0 }; string st; do { fflush(stdin); cout << "---------Double Playfair cipher(Encryption)---------" << endl; cout << " the null letter is 'x' " << endl; cout << "Please enter the plaintext:"; cin >> st; flag = Check(st); } while (flag); cout << "Please enter the cipher period:"; //*此处可改进以下,检测输入的是不是数字,防止运行时出错 cin >> period; strtem(st, tem); matrix_1 = matrixkey(matrix1); matrix_2 = matrixkey(matrix2); //打印组合的matrix key cout << endl << endl << " Playfair squares" << endl << endl; for (i = 0; i < 5; i++) { cout << " "; for (j = 0; j < 5; j++) { cout << matrix_1[i][j]; cout << " "; } cout << " "; for (k = 0; k < 5; k++) { cout << matrix_2[i][k]; cout << " "; } cout << endl; } cout << endl; cout << " Group Of The Plaintext" << endl; k = 0; for (i = 0; i < strlen(tem); ) { if (tem[k] != '\0') { if ((strlen(tem) - k) >= (2 * period)) { for (m = 0; m < 2; m++) { cout << " "; for (j = 0; j < period; j++) { test[t][j] = tem[k]; cout << " " << test[t][j]; k++; i++; } cout << endl; t++; } } else { q = (strlen(tem) - k) / 2; for (m = 0; m < 2; m++) { cout << " "; for (j = 0; j < q; j++) { test[t][j] = tem[k]; cout << " " << test[t][j]; k++; i++; } t++; cout << endl; } } } else { break; } } cout << endl; cout << "CipherText: "; flag = t - 1; m = 0; for (i = 0; i < flag; ) { if ((i == (flag - 1)) && q != 0) { period = q; } for (j = 0; j < period; j++) { for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { if (matrix_1[m][n] == test[i][j]) { flag1 = m; flag2 = n; } if (matrix_2[m][n] == test[(i + 1)][j]) { flag3 = m; flag4 = n; } flag_1 = flag1; flag_2 = flag2; flag_3 = flag3; flag_4 = flag4; } } if (flag1 == flag3) { for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { if (matrix_1[m][n] == matrix_2[flag_3][(flag_4 + 1) % 5]) { flag1 = m; flag2 = n; } if (matrix_2[m][n] == matrix_1[flag_1][(flag_2 + 1) % 5]) { flag3 = m; flag4 = n; } } } if (flag1 == flag3) { cout << matrix_2[flag3][(flag4 + 1) % 5] << matrix_1[flag1][(flag2 + 1) % 5]; } else { cout << matrix_2[flag1][flag4] << matrix_1[flag3][flag2]; } } else { for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { if (matrix_1[m][n] == matrix_2[flag_1][flag_4]) { flag1 = m; flag2 = n; } if (matrix_2[m][n] == matrix_1[flag_3][flag_2]) { flag3 = m; flag4 = n; } } } if (flag1 == flag3) { cout << matrix_2[flag3][(flag4 + 1) % 5] << matrix_1[flag1][(flag2 + 1) % 5]; } else { cout << matrix_2[flag1][flag4] << matrix_1[flag3][flag2]; } } } i = i + 2; } 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
#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, t = 0, q = 0; int period; int flag = 0; int flag1, flag2, flag3, flag4; int flag_1, flag_2, flag_3, flag_4; char(*matrix_1)[5]; char(*matrix_2)[5]; char matrix1[5][5] = { 0 }; char matrix2[5][5] = { 0 }; char plain[20][2] = { 0 }; char test[10][30] = { 0 }; char plaintext[20][20] = { 0 }; char tem[50] = { 0 }; string st; do { fflush(stdin); system("cls"); cout << "----------Double 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); cout << "Please enter the cipher period:"; cin >> period; strtem(st, tem); matrix_1 = matrixkey(matrix1); matrix_2 = matrixkey(matrix2); //打印组合的matrix key cout << endl << endl << " Playfair squares" << endl << endl; for (i = 0; i < 5; i++) { cout << " "; for (j = 0; j < 5; j++) { cout << matrix_1[i][j]; cout << " "; } cout << " "; for (k = 0; k < 5; k++) { cout << matrix_2[i][k]; cout << " "; } cout << endl; } cout << endl; cout << " Group Of The Ciphertext" << endl << endl; //整理密文为矩阵形式 k = 0; t = 0; for (i = 0; i < strlen(tem); ) { if (tem[k] != '\0') { if ((strlen(tem) - k) >= (2 * period)) { for (m = 0; m < period; m++) { for (j = t; j < (t + 2); j++) { test[j][m] = tem[k]; k++; i++; } } t = t + 2; } else { q = (strlen(tem) - k) / 2; for (m = 0; m < q; m++) { for (j = t; j < (t + 2); j++) { test[j][m] = tem[k]; k++; i++; } } t = t + 2; } } else { break; } } //打印矩阵密文 k = 0; t = 0; for (i = 0; i < strlen(tem); ) { if (tem[k + 1] != '\0') { if ((strlen(tem) - k) >= (2 * period)) { for (j = 0; j < 2; j++) { for (m = 0; m < period; m++) { cout << " "; cout << test[t][m]; k++; i++; } cout << endl; t++; } } else { q = (strlen(tem) - k) / 2; for (j = 0; j < 2; j++) { for (m = 0; m < q; m++) { cout << " "; cout << test[t][m]; k++; i++; } cout << endl; t++; } } } else { break; } } cout << endl; //寻找明文 flag = t - 1; m = 0; for (i = 0; i < flag; ) { if ((i == (flag - 1)) && q != 0) { period = q; } for (j = 0; j < period; j++) { for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { if (matrix_2[m][n] == test[i][j]) { flag3 = m; flag4 = n; } if (matrix_1[m][n] == test[(i + 1)][j]) { flag1 = m; flag2 = n; } flag_1 = flag1; flag_2 = flag2; flag_3 = flag3; flag_4 = flag4; } } if (flag1 == flag3) { for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { if (matrix_2[m][n] == matrix_1[flag_1][(flag_2 + 4) % 5]) { flag3 = m; flag4 = n; } if (matrix_1[m][n] == matrix_2[flag_3][(flag_4 + 4) % 5]) { flag1 = m; flag2 = n; } } } if (flag1 == flag3) { plaintext[i][j] = matrix_1[flag1][(flag2 + 4) % 5]; plaintext[i + 1][j] = matrix_2[flag3][(flag4 + 4) % 5]; } else { plaintext[i][j] = matrix_1[flag3][flag2]; plaintext[i + 1][j] = matrix_2[flag1][flag4]; } } else { for (m = 0; m < 5; m++) { for (n = 0; n < 5; n++) { if (matrix_2[m][n] == matrix_1[flag_3][flag_2]) { flag3 = m; flag4 = n; } if (matrix_1[m][n] == matrix_2[flag_1][flag_4]) { flag1 = m; flag2 = n; } } } if (flag1 == flag3) { plaintext[i][j] = matrix_1[flag1][(flag2 + 4) % 5]; plaintext[i + 1][j] = matrix_2[flag3][(flag4 + 4) % 5]; } else { plaintext[i][j] = matrix_1[flag3][flag2]; plaintext[i + 1][j] = matrix_2[flag1][flag4]; } } } i = i + 2; } j = 0; k = 0; cout << "PlainText:"; for (i = 0; i < st.length();) { if (plaintext[j][k] != '\0') { cout << plaintext[j][k]; k++; i++; continue; } k = 0; j++; } cout << endl << endl; return 0; } |
(编译运行环境:g++ [Debian 7.3.0-19] 7.3.0)
说点什么