1.DES简介:DES是Data Encryption Standard的缩写,是美国国家标准局于1977年公布的由IBM公司研制的加密算法,并被作为非机要部门使用的数据加密标准。DES算法是应用最广泛的一种分组密码算法,对密码理论的发展和应用起了重大作用。DES综合运用了置换、代替、代数等多种密码技术,是面向二进制的密码算法,因而能够加解密任何形式的计算机数据。
2.DES加密算法:DES算法中数据以64比特分组进行加密,有效密钥长度为56位。它的加密算法与解密算法相同,只是解密子密钥与加密子密钥的使用顺序刚好相反。
3.程序思路:程序最关键的地方在于对明文的分块处理、二进制转化、盒子操作、以及子密钥的产生和十六次加密迭代使用。因此,在进行实验时目标明确,先处理密钥,生成16个48位的子密钥,再处理明文,接着实现盒子操作与加密函数f,然后迭代16次,产生64位数据组,之后对数据组进行处理后,经过逆初始置换,得到64位明文,至此一组加密过程全部结束。在本实验中,定义以下规则:(1)将初始明文/密文分组并依次转化为二进制比特流之后,若分组内比特流长度不足64位,则补0。(2)对密钥的操作处理:输入7位英文字符,得到其对应的ASCII码,将ASCII码用二进制表示,最终得到56位二进制比特流,将此作为DES的56位密钥。之后在第8,16,24,32,40,48,56,64位处插入奇偶校验位,合成64位密钥。
4.DES整体流程
5.子密钥产生流程:
6.算法步骤
7.函数说明
8.DES加解密实现(C++)
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
#include<iostream> #include<string> #include<string.h> #include<stdlib.h> #include<math.h> #include<fstream> #include<sstream> using namespace std; int Check(string); int *keyTrans(string, int[64]); void plainTrans(string, int, int(&)[800][64], int); //将明文分块,并转化为二进制存储形式 void initial(int(&)[800][64], int(&)[800][64], string); void makeKey(int *, int(&)[16][48], int); void funEBox(int extrightPlain[48], int tempright[32]); void funSBox(int[][48], int[48], int[32], int); void funPBox(int[32], int[32]); void funFinalTP(int[32], int[32], int[64], int, int(&)[800][64]); void outputchar(int(&)[800][64], int, int); //由ASCII码输出字符 string readFileIntoString(char * filename); const static int IP[64] = { //初始置换IP(针对明文) 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; const static int PC_1[56] = { //密钥置换,将64位密钥置换为56位 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; const static int PC_2[48] = { //密钥压缩置换,将56位密钥置换为48位 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; const static int EBox[48] = { //扩展置换,将32位扩展为48位 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; const static int SBox[8][4][16] = { { //Sbox_1 { 14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7 }, { 0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8 }, { 4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0 }, { 15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13 } }, { //Sbox_2 { 15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10 }, { 3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5 }, { 0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15 }, { 13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9 } }, { //Sbox_3 { 10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8 }, { 13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1 }, { 13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7 }, { 1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12 } }, { //Sbox_4 { 7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15 }, { 13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9 }, { 10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4 }, { 3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14 } }, { //Sbox_5 { 2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9 }, { 14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6 }, { 4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14 }, { 11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3 } }, { //Sbox_6 { 12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11 }, { 10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8 }, { 9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6 }, { 4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13 } }, { //Sbox_7 { 4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1 }, { 13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6 }, { 1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2 }, { 6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12 } }, { //Sbox_8 { 13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7 }, { 1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2 }, { 7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8 }, { 2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11 } } }; const static int PBox[32] = { 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; const static int FT[64] = { 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; 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; } int *keyTrans(string key, int keytemp[64]) { //密钥二进制转换并输出 int i, k, t, m, n, q; int block; int flag = 0; int temp[56] = { 0 }; int *p = keytemp; m = 0; for (k = 0; k < 7; k++) { t = key[6 - k]; //二进制转换 for (i = 0; i < 8; i++) { temp[56 - 1 - m] = t % 2; t /= 2; m++; } } cout << endl; cout << "-----------------------------------Key Information----------------------------------" << endl << endl; cout << "Key(56bit): "; //56位密钥 for (i = 0; i < 56; i++) { cout << temp[i]; if ((i + 1) % 7 == 0) cout << " "; } cout << endl << endl; n = 0; m = 0; q = 0; cout << "Key(64bit)(With odd parity)" << endl; // 显示有奇偶校验位的64位密钥 cout << "Key(64bit): "; for (i = 0; i < 56; i++) { cout << temp[i]; keytemp[q] = temp[i]; q++; if (temp[i] == 0) { n++; } if (((m + 1) % 7 == 0) && (n % 2 != 0)) { cout << "1 "; keytemp[q] = 1; //将64位密钥存入数组 q++; m++; n = 0; continue; } else if (((m + 1) % 7 == 0) && (n % 2 == 0)) { cout << "0 "; keytemp[q] = 0; q++; m++; n = 0; continue; } m++; } cout << endl << endl; return p; } void plainTrans(string st, int block, int(&plaintext)[800][64], int way) { // 实现明文/密文分块和二进制转化 unsigned int i, k, t, m, n, j; int flag = 0; int temp[56] = { 0 }; //将明文分块,存入二维数组 n = 0; k = st.length() % 8; if (k == 0) k = 8; m = block; for (i = 0; i < st.length(); i++) { t = (unsigned int)st[(st.length() - 1 - i)]; if (t > 256) { t = t - 4294967040; //若字符的ASCII超出范围,则减去过余码 } for (j = 0; j < 8; j++) { if (n == 64) { n = 0; m--; } if (m != block) { plaintext[m][(64 - 1 - n)] = t % 2; } else { plaintext[m][(8 * k - 1 - n)] = t % 2; if (n == (8 * k - 1)) { //明文最后一个分块,不足64位补0 n = -1; m--; } } t /= 2; n++; } } cout << endl; //输出分块明文比特流 if (way == 0) { cout << "--------------------------------Binary Plaintext Block-------------------------------" << endl << endl; } else { cout << "--------------------------------Binary Ciphertext Block------------------------------" << endl << endl; } for (i = 0; i < block + 1; i++) { cout << "Block" << i + 1 << ":"; for (j = 0; j < 64; j++) { cout << plaintext[i][j]; } cout << endl; } cout << endl; } void initial(int(&plainTemp)[800][64], int(&initialPlain)[800][64], string st) { // DES初始置换 int block; int i, j; block = (st.length() - 1) / 8; for (i = 0; i < block + 1; i++) { for (j = 0; j < 64; j++) { initialPlain[i][j] = plainTemp[i][(IP[j] - 1)]; //查表,初始化明文块 } } } void makeKey(int *key, int(&subKey)[16][48], int way) { //构造子密钥 int i, j, l, r, k, flag, p, q; int temp0[56] = { 0 }; int templeft[28] = { 0 }; int tempright[28] = { 0 }; int combine[56] = { 0 }; cout << "-----------------------------------Product SubKey----------------------------------" << endl << endl; for (i = 0; i < 56; i++) { // PC-1 temp0[i] = key[(PC_1[i] - 1)]; if (i < 28) { templeft[i] = key[(PC_1[i] - 1)]; } else { tempright[i - 28] = key[(PC_1[i] - 1)]; } //PC-1完成,输出结果用于检验 } //输出PC-1 cout << endl << "PC-1: "; for (i = 0; i < 56; i++) { cout << temp0[i]; } cout << endl; //输出L0和R0 cout << "L(0): "; for (i = 0; i < 28; i++) { cout << templeft[i]; } cout << endl << "R(0): "; for (i = 0; i < 28; i++) { cout << tempright[i]; } cout << endl << endl; for (i = 0; i < 16; i++) { //循环左移 if (i == 0 || i == 1 || i == 8 || i == 15) { l = templeft[0]; r = tempright[0]; for (j = 0; j < 27; j++) { templeft[j] = templeft[j + 1]; tempright[j] = tempright[j + 1]; } templeft[j] = l; tempright[j] = r; //移完一次 储存 //输出L和R cout << "L(" << i + 1 << "): "; for (p = 0; p < 28; p++) { cout << templeft[p]; } cout << endl << "R(" << i + 1 << "): "; for (p = 0; p < 28; p++) { cout << tempright[p]; } //组合L和R for (flag = 0; flag < 56; flag++) { if (flag < 28) { combine[flag] = templeft[flag]; } else { combine[flag] = tempright[flag - 28]; } } //PC-2 cout << endl << "SubKey" << i + 1 << ": "; if (way == 0) { //加密 for (flag = 0; flag < 48; flag++) { subKey[i][flag] = combine[(PC_2[flag] - 1)]; cout << subKey[i][flag]; } } else { // 解密子密钥顺序相反 for (flag = 0; flag < 48; flag++) { subKey[15 - i][flag] = combine[(PC_2[flag] - 1)]; cout << subKey[15 - i][flag]; } } cout << endl << endl; } else { for (k = 0; k < 2; k++) { l = templeft[0]; r = tempright[0]; for (j = 0; j < 27; j++) { templeft[j] = templeft[j + 1]; tempright[j] = tempright[j + 1]; } templeft[j] = l; tempright[j] = r; //移完两次 储存 } //输出L和R cout << "L(" << i + 1 << "): "; for (p = 0; p < 28; p++) { cout << templeft[p]; } cout << endl << "R(" << i + 1 << "): "; for (p = 0; p < 28; p++) { cout << tempright[p]; } for (flag = 0; flag < 56; flag++) { if (flag < 28) { combine[flag] = templeft[flag]; } else { combine[flag] = tempright[flag - 28]; } } //PC-2 cout << endl << "SubKey" << i + 1 << ": "; if (way == 0) { //加密 for (flag = 0; flag < 48; flag++) { subKey[i][flag] = combine[(PC_2[flag] - 1)]; cout << subKey[i][flag]; } } else { //解密密钥顺序相反 for (flag = 0; flag < 48; flag++) { subKey[15 - i][flag] = combine[(PC_2[flag] - 1)]; cout << subKey[15 - i][flag]; } } cout << endl << endl; } } cout << "------------------------------SubKey Information-----------------------------" << endl << endl; for (i = 0; i < 16; i++) { cout << "SubKey" << i + 1 << ": "; for (flag = 0; flag < 48; flag++) { cout << subKey[i][flag]; } cout << endl; } cout << endl; } void funEBox(int extrightPlain[48], int tempright[32]) { //扩展置换(EBox) int i; for (i = 0; i < 48; i++) { extrightPlain[i] = tempright[(EBox[i] - 1)]; cout << extrightPlain[i]; } cout << endl; } void funSBox(int key[][48], int extrightPlain[48], int out[32], int j) { //代替函数组(SBox) int m, n, p, s, t, k, q; int temp[8][6] = { 0 }; int temp2[8] = { 0 }; cout << endl << " SubKey(" << j + 1 << "): "; for (m = 0; m < 48; m++) { cout << key[j][m]; } cout << endl << " Key XOR RP: "; p = 0; for (m = 0; m < 8; m++) { for (n = 0; n < 6; n++) { temp[m][n] = (key[j][p]) ^ (extrightPlain[p]); cout << temp[m][n]; p++; } } for (m = 0; m < 8; m++) { s = (temp[m][0]) * 2 + temp[m][5]; t = (temp[m][1] * 8 + temp[m][2] * 4 + temp[m][3] * 2 + temp[m][4]); temp2[m] = SBox[m][s][t]; } //二进制转换 m = 0; for (k = 0; k < 8; k++) { t = temp2[7 - k]; //二进制转换 for (q = 0; q < 4; q++) { out[32 - 1 - m] = t % 2; t /= 2; m++; } } cout << endl << " OutPut: "; for (m = 0; m < 32; m++) { cout << out[m]; } cout << endl; } void funPBox(int outSBox[32], int outPBox[32]) { //置换运算(PBox) int i; for (i = 0; i < 32; i++) { outPBox[i] = outSBox[(PBox[i] - 1)]; cout << outPBox[i]; } cout << endl << endl; } void funFinalTP(int left[32], int right[32], int finalTrans[64], int i, int(&finalresult)[800][64]) {//逆初始置换 int j; int tempfinalTrans[64] = { 0 }; for (j = 0; j < 64; j++) { if (j < 32) { tempfinalTrans[j] = left[j]; } else { tempfinalTrans[j] = right[j - 32]; } } cout << endl << "Block" << i + 1 << " Ciphertext" << ": "; for (j = 0; j < 64; j++) { finalTrans[j] = tempfinalTrans[(FT[j] - 1)]; finalresult[i][j] = finalTrans[j]; cout << finalTrans[j]; } cout << endl; } void outputchar(int(&finalresult)[800][64], int i, int way) { ofstream fout; int temp[8000] = { 0 }; int t, j, q, sum, m, n; char ch[10000] = { '\0' }; sum = 0; q = 0; for (t = 0; t < i; t++) { for (j = 0; j < 64; j++) { m = 7 - (j % 8); n = pow(2, m); sum = sum + (finalresult[t][j])*n; if (m == 0) { temp[q] = sum; q++; sum = 0; } } } //输出ASCII对应的字符 cout << endl << endl << "Character Ciphertext: " << endl << endl; for (m = 0; m < q; m++) { ch[m] = temp[m]; cout << ch[m]; } if (way == 0) { fout.open("DESciphertext.txt"); //打开DESciphertext.txt文件 fout << ch; fout.close(); cout << endl << endl << endl; cout << "-------------------------------" << endl; cout << "Saved in the DESciphertext.txt"; } else { fout.open("DESplaintext.txt"); //打开DESciphertext.txt文件 fout << ch; fout.close(); cout << endl << endl << endl; cout << "------------------------------" << endl; cout << "Saved in the DESplaintext.txt"; } cout << endl << endl; } string readFileIntoString(char * filename) { //从文件读入到string里 cin >> filename; strcat(filename, ".txt"); fstream openencryfile(filename); if (!openencryfile) { cout << "Open failed!" << endl << endl; exit(1); } ifstream ifile(filename, ios::binary); //文件以二进制形式打开 ostringstream buf; //将文件读入到ostringstream对象buf中 char ch; while (buf && ifile.get(ch)) buf.put(ch); return buf.str(); //返回与流对象buf关联的字符串 } int main() { string key; string st; string strchoose; fstream opendecryfile; fstream openencryfile; char choose; char filename[30]; char * fn = filename; int i, j, block, m, n, flag, way; int *temp; //指向二进制密钥的指针 int keytemp[64] = { 0 }; int pltext[800][64] = { 0 }; int leftPlain[800][32] = { 0 }; int rightPlain[800][32] = { 0 }; int extrightPlain[48] = { 0 }; int iniPlain[800][64] = { 0 }; int subKey[16][48] = { 0 }; int outSBox[32] = { 0 }; int outPBox[32] = { 0 }; int templeft[32] = { 0 }; int tempright[32] = { 0 }; int temptrans[32] = { 0 }; int finalTrans[64] = { 0 }; int finalresult[800][64] = { 0 }; //将各block密文组合到一块储存起来 while (1) { fflush(stdin); way = 0; flag = 0; do { fflush(stdin); cout << "========================================================================" << endl; cout << "------------Data Encryption Standard(Encryption/Decryption)-------------" << endl; cout << "========================================================================" << endl; cout << endl << endl; cout << " ************************************************" << endl; cout << " * OPTIONS *" << endl; cout << " * *" << endl; cout << " * 1 Encryption from the Keyboard *" << endl; cout << " * 2 Encryption from the File *" << endl; cout << " * 3 Decryption from the File *" << endl; cout << " * 4 Exit *" << endl; cout << " ************************************************" << endl; cout << endl << " What do you want to do: "; cin >> strchoose; if (strchoose.at(0) != '1'&&strchoose.at(0) != '2'&&strchoose.at(0) != '3'&&strchoose.at(0) != '4' || strchoose.length() != 1) { cout << "Error!Please enter again!" << endl; } } while (strchoose.at(0) != '1'&&strchoose.at(0) != '2'&&strchoose.at(0) != '3'&&strchoose.at(0) != '4' || strchoose.length() != 1); choose = strchoose[0]; switch (choose) { case '1': fflush(stdin); cout << "--------------------Data Encryption Standard(Encryption)-------------------" << endl; cout << endl << "Please enter the plaintext:" << endl; cout << "Plaintext: "; cin >> st; break; case '2': fflush(stdin); cout << "--------------------Data Encryption Standard(Encryption)-------------------" << endl; cout << endl << "Please enter the file name that you want to Encryption:" << endl; cout << "File name: "; st = readFileIntoString(fn); cout << endl << filename << ": " << endl; cout << st; cout << endl; break; case '3': fflush(stdin); cout << "--------------------Data Encryption Standard(Decryption)-------------------" << endl; cout << endl << "Please enter the file name that you want to Decryption:" << endl; cout << "File name: "; st = readFileIntoString(fn); cout << endl << filename << ": " << endl; cout << st; cout << endl; way = 1; //设置标记,标记为1为解密,标记为0为加密 break; default: cout << endl << " "; return 0; } do { //输入key fflush(stdin); cout << endl << "Please enter the key(7 English letters): " << endl; //通过输入字符,转化为ASCⅡ作为密钥 cout << "Key: "; cin >> key; if (key.length() != 7) { cout << "Error!The key must be 56bit(7 English letters)!" << endl << endl; exit(-1); } flag = Check(key); } while (flag); block = (st.length() - 1) / 8; // 明文分组,共block+1块 plainTrans(st, block, pltext, way); temp = keyTrans(key, keytemp); //密钥二进制转换 makeKey(temp, subKey, way); //得到子密钥 initial(pltext, iniPlain, st); //DES初始置换 for (i = 0; i < block + 1; i++) { //将初始化了的明文分离存入数组,得到LP和RP for (j = 0; j < 64; j++) { if (j < 32) { leftPlain[i][j] = iniPlain[i][j]; } else { rightPlain[i][j - 32] = iniPlain[i][j]; } } } for (i = 0; i < block + 1; i++) { //处理的分块 for (flag = 0; flag < 32; flag++) { //明文初始块的LP和RP templeft[flag] = leftPlain[i][flag]; tempright[flag] = rightPlain[i][flag]; } cout << endl << "------------------------------Block " << i + 1 << " --------------------------------" << endl; cout << endl << "Block" << i + 1 << ":"; for (m = 0; m < 64; m++) { cout << pltext[i][m]; } cout << endl; cout << "IT: "; for (m = 0; m < 64; m++) { cout << iniPlain[i][m]; } cout << endl << endl << "LP(0): "; for (flag = 0; flag < 32; flag++) { cout << templeft[flag]; } cout << endl; cout << "RP(0): "; for (flag = 0; flag < 32; flag++) { cout << tempright[flag]; } cout << endl; for (j = 0; j < 16; j++) { //每次加密需要循环16次 cout << "EBox" << j << ": "; //Ebox扩展置换 funEBox(extrightPlain, tempright); cout << "SBox" << j << ": "; funSBox(subKey, extrightPlain, outSBox, j); cout << "PBox" << j << ": "; funPBox(outSBox, outPBox); for (m = 0; m < 32; m++) { temptrans[m] = tempright[m]; tempright[m] = (templeft[m]) ^ (outPBox[m]); templeft[m] = temptrans[m]; } if (j == 15) { for (m = 0; m < 32; m++) { temptrans[m] = tempright[m]; tempright[m] = templeft[m]; templeft[m] = temptrans[m]; } } cout << endl << "LP(" << j + 1 << "): "; for (flag = 0; flag < 32; flag++) { cout << templeft[flag]; } cout << endl << "RP(" << j + 1 << "): "; for (flag = 0; flag < 32; flag++) { cout << tempright[flag]; } cout << endl; } funFinalTP(templeft, tempright, finalTrans, i, finalresult); } cout << endl << "------------------------------Final Result --------------------------------" << endl; cout << endl << "Stream Ciphertext: " << endl << endl; for (i = 0; i < block + 1; i++) { for (j = 0; j < 64; j++) { cout << finalresult[i][j]; } cout << endl; } //由ASCII码输出字符 outputchar(finalresult, i, way); } } |
程序结果验证:
验证一:
1.从键盘输入加密。加密内容为www.flagxue.cn,密钥为program.
2.加密。加密结果保存在DESciphertext.txt.
3.从文件解密。解密文件为DESciphertext.txt, 密钥为program.
4.解密。解密后结果恢复www.flagxue.cn.解密结果保存在DESplaintext.txt.
验证二:
1.新建文件temp.txt, 将待加密内容写入文件。或者选择任一待加密文件。
2.从文件加密。密钥为flagxue.
3.加密。加密结果保存在DESciphertext.txt.
4.从文件解密。解密文件为DESciphertext.txt, 密钥为flagxue.
5.解密。解密后恢复明文,解密结果保存在DESplaintext.txt.
其他信息:
1.二进制明/密文信息
2.密钥信息
3.生成子密钥过程(部分省略)
4.子密钥信息
5.分块流程(部分省略)
思考:
1.DES 的原理是什么?
答:DES是分组加密算法, 明文按64位进行分组,有效密钥长度为56位。通过将分组后的明文组和56位的密钥按位替代或置换的方法形成密文组。
2.DES使用多少位密钥?
答:DES使用56位密钥。
3.DES对明文分块的单位是多少?
答:DES算法中明文以64位进行分组。
4.DES对每一个数据块加密的轮次是多少?
答:DES对每一个数据块加密的轮次为16轮。
5.简单描述EBox的操作过程。
答:Ebox是一种扩展运算,对32位的数据组的各位按照一定的顺序进行选择和排列,产生一个48位的结果。
6.简单描述SBox的操作过程。
答:Sbox将48比特向量通过非线性映射变为32比特向量。首先,48比特的向量被分为8个6比特分组,8个分组在8个不同的非线性Sbox的作用下被转变为8个4比特分组,其中每个Sbox都将6比特输入映射为4比特输出。
7.简单描述PBox的操作过程。
答:Pbox是一种置换运算,把Sbox输出的32位数据打乱重排,得到32位的加密函数输出。用P置换来提供扩散,把S盒的混淆作用扩散开来。
[编译运行环境:g++ (Debian 7.3.0-19) 7.3.0]
很详细!谢谢!