├── .github ├── FUNDING.yml └── workflows │ └── cmake-single-platform.yml ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── docs ├── 4-DES.pdf ├── DES-f-function.png ├── DES-ip.svg ├── DES-key-schedule.png ├── DES-main-network.png ├── Data_Encription_Standard_Flow_Diagram.svg ├── Riv85.txt ├── The DES Algorithm Illustrated.html └── fips46-3.pdf ├── include └── cppdes │ ├── des.h │ ├── des3.h │ ├── des3cbc.h │ └── descbc.h ├── lib ├── des.cpp ├── des3.cpp ├── des3cbc.cpp ├── des_key.h ├── des_lookup.h └── descbc.cpp ├── src ├── fileencryption.cpp ├── fileencryption.h └── main.cpp └── test └── main.cpp /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [fffaraz] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 12 | polar: # Replace with a single Polar username 13 | buy_me_a_coffee: # Replace with a single Buy Me a Coffee username 14 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 15 | -------------------------------------------------------------------------------- /.github/workflows/cmake-single-platform.yml: -------------------------------------------------------------------------------- 1 | # This starter workflow is for a CMake project running on a single platform. There is a different starter workflow if you need cross-platform coverage. 2 | # See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-multi-platform.yml 3 | name: CMake on a single platform 4 | 5 | on: 6 | push: 7 | branches: [ "master" ] 8 | pull_request: 9 | branches: [ "master" ] 10 | 11 | env: 12 | # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) 13 | BUILD_TYPE: Release 14 | 15 | jobs: 16 | build: 17 | # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. 18 | # You can convert this to a matrix build if you need cross-platform coverage. 19 | # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix 20 | runs-on: ubuntu-latest 21 | 22 | steps: 23 | - uses: actions/checkout@v4 24 | 25 | - name: Configure CMake 26 | # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. 27 | # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type 28 | run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} 29 | 30 | - name: Build 31 | # Build your program with the given configuration 32 | run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} 33 | 34 | - name: Test 35 | working-directory: ${{github.workspace}}/build 36 | # Execute tests defined by the CMake configuration. 37 | # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail 38 | run: ctest -C ${{env.BUILD_TYPE}} 39 | 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | build/ 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(cppDES) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 6 | 7 | add_library( 8 | libcppdes 9 | include/cppdes/des.h 10 | include/cppdes/des3.h 11 | include/cppdes/des3cbc.h 12 | include/cppdes/descbc.h 13 | lib/des_key.h 14 | lib/des_lookup.h 15 | lib/des.cpp 16 | lib/des3.cpp 17 | lib/des3cbc.cpp 18 | lib/descbc.cpp) 19 | target_include_directories(libcppdes PUBLIC include) 20 | 21 | add_executable(cppDES src/main.cpp src/fileencryption.h src/fileencryption.cpp) 22 | target_link_libraries(cppDES libcppdes) 23 | 24 | include(FetchContent) 25 | FetchContent_Declare( 26 | googletest 27 | URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz) 28 | FetchContent_MakeAvailable(googletest) 29 | 30 | enable_testing() 31 | 32 | add_executable(cppDESTest test/main.cpp) 33 | target_link_libraries(cppDESTest libcppdes GTest::gtest_main) 34 | 35 | include(GoogleTest) 36 | gtest_discover_tests(cppDESTest) 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Faraz Fallahi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cppDES 2 | ====== 3 | 4 | C++ implementation of Data Encryption Standard 5 | 6 | DES, Triple DES (3DES), DES ECB Mode, DES CBC Mode 7 | 8 | The Data Encryption Standard (DES) has been a standard encryption method in the United States for a number of years. It is moderately secure. No easy ways have been found to crack it, although a brute-force approach, using expensive special-purpose equipment, is probably feasible. 9 | 10 | When is compiled as a standalone application, it produces a command-line application that encrypts or decrypts a file using a hexadecimal key taken from the second command-line argument. 11 | 12 | Usage: `cppDES -e/-d key1 key2 key3 [input-file] [output-file]` 13 | 14 | * [benvanik/xenia](https://github.com/benvanik/xenia/tree/master/third_party/crypto/des) - Xbox 360 Emulator Research Project 15 | -------------------------------------------------------------------------------- /docs/4-DES.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fffaraz/cppDES/a9f58e3f97825d44ac0d6a999ce4b6495369d0b6/docs/4-DES.pdf -------------------------------------------------------------------------------- /docs/DES-f-function.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fffaraz/cppDES/a9f58e3f97825d44ac0d6a999ce4b6495369d0b6/docs/DES-f-function.png -------------------------------------------------------------------------------- /docs/DES-ip.svg: -------------------------------------------------------------------------------- 1 | 2 | 5 | ]> 6 | 8 | 9 | 11 | 12 | des 13 | 14 | 15 | u1 16 | 17 | 1 18 | 19 | 20 | u1--u1 21 | 22 | 23 | 24 | u2 25 | 26 | 2 27 | 28 | 29 | u1--u2 30 | 31 | 32 | 33 | u3 34 | 35 | 3 36 | 37 | 38 | u2--u3 39 | 40 | 41 | 42 | u4 43 | 44 | 4 45 | 46 | 47 | u3--u4 48 | 49 | 50 | 51 | u5 52 | 53 | 5 54 | 55 | 56 | u4--u5 57 | 58 | 59 | 60 | u6 61 | 62 | 6 63 | 64 | 65 | u5--u6 66 | 67 | 68 | 69 | u7 70 | 71 | 7 72 | 73 | 74 | u6--u7 75 | 76 | 77 | 78 | u8 79 | 80 | 8 81 | 82 | 83 | u7--u8 84 | 85 | 86 | 87 | u9 88 | 89 | 9 90 | 91 | 92 | u8--u9 93 | 94 | 95 | 96 | u10 97 | 98 | 10 99 | 100 | 101 | u9--u10 102 | 103 | 104 | 105 | u11 106 | 107 | 11 108 | 109 | 110 | u10--u11 111 | 112 | 113 | 114 | u12 115 | 116 | 12 117 | 118 | 119 | u11--u12 120 | 121 | 122 | 123 | u13 124 | 125 | 13 126 | 127 | 128 | u12--u13 129 | 130 | 131 | 132 | u14 133 | 134 | 14 135 | 136 | 137 | u13--u14 138 | 139 | 140 | 141 | u15 142 | 143 | 15 144 | 145 | 146 | u14--u15 147 | 148 | 149 | 150 | u16 151 | 152 | 16 153 | 154 | 155 | u15--u16 156 | 157 | 158 | 159 | u17 160 | 161 | 17 162 | 163 | 164 | u16--u17 165 | 166 | 167 | 168 | u18 169 | 170 | 18 171 | 172 | 173 | u17--u18 174 | 175 | 176 | 177 | u19 178 | 179 | 19 180 | 181 | 182 | u18--u19 183 | 184 | 185 | 186 | u20 187 | 188 | 20 189 | 190 | 191 | u19--u20 192 | 193 | 194 | 195 | u21 196 | 197 | 21 198 | 199 | 200 | u20--u21 201 | 202 | 203 | 204 | u22 205 | 206 | 22 207 | 208 | 209 | u21--u22 210 | 211 | 212 | 213 | u23 214 | 215 | 23 216 | 217 | 218 | u22--u23 219 | 220 | 221 | 222 | u24 223 | 224 | 24 225 | 226 | 227 | u23--u24 228 | 229 | 230 | 231 | u25 232 | 233 | 25 234 | 235 | 236 | u24--u25 237 | 238 | 239 | 240 | u26 241 | 242 | 26 243 | 244 | 245 | u25--u26 246 | 247 | 248 | 249 | u27 250 | 251 | 27 252 | 253 | 254 | u26--u27 255 | 256 | 257 | 258 | u28 259 | 260 | 28 261 | 262 | 263 | u27--u28 264 | 265 | 266 | 267 | u29 268 | 269 | 29 270 | 271 | 272 | u28--u29 273 | 274 | 275 | 276 | u30 277 | 278 | 30 279 | 280 | 281 | u29--u30 282 | 283 | 284 | 285 | u31 286 | 287 | 31 288 | 289 | 290 | u30--u31 291 | 292 | 293 | 294 | u32 295 | 296 | 32 297 | 298 | 299 | u31--u32 300 | 301 | 302 | 303 | u33 304 | 305 | 33 306 | 307 | 308 | u32--u33 309 | 310 | 311 | 312 | u34 313 | 314 | 34 315 | 316 | 317 | u33--u34 318 | 319 | 320 | 321 | u35 322 | 323 | 35 324 | 325 | 326 | u34--u35 327 | 328 | 329 | 330 | u36 331 | 332 | 36 333 | 334 | 335 | u35--u36 336 | 337 | 338 | 339 | u37 340 | 341 | 37 342 | 343 | 344 | u36--u37 345 | 346 | 347 | 348 | u38 349 | 350 | 38 351 | 352 | 353 | u37--u38 354 | 355 | 356 | 357 | u39 358 | 359 | 39 360 | 361 | 362 | u38--u39 363 | 364 | 365 | 366 | u40 367 | 368 | 40 369 | 370 | 371 | u39--u40 372 | 373 | 374 | 375 | u41 376 | 377 | 41 378 | 379 | 380 | u40--u41 381 | 382 | 383 | 384 | u42 385 | 386 | 42 387 | 388 | 389 | u41--u42 390 | 391 | 392 | 393 | u43 394 | 395 | 43 396 | 397 | 398 | u42--u43 399 | 400 | 401 | 402 | u44 403 | 404 | 44 405 | 406 | 407 | u43--u44 408 | 409 | 410 | 411 | u45 412 | 413 | 45 414 | 415 | 416 | u44--u45 417 | 418 | 419 | 420 | u46 421 | 422 | 46 423 | 424 | 425 | u45--u46 426 | 427 | 428 | 429 | u47 430 | 431 | 47 432 | 433 | 434 | u46--u47 435 | 436 | 437 | 438 | u48 439 | 440 | 48 441 | 442 | 443 | u47--u48 444 | 445 | 446 | 447 | u49 448 | 449 | 49 450 | 451 | 452 | u48--u49 453 | 454 | 455 | 456 | u50 457 | 458 | 50 459 | 460 | 461 | u49--u50 462 | 463 | 464 | 465 | u51 466 | 467 | 51 468 | 469 | 470 | u50--u51 471 | 472 | 473 | 474 | u52 475 | 476 | 52 477 | 478 | 479 | u51--u52 480 | 481 | 482 | 483 | u53 484 | 485 | 53 486 | 487 | 488 | u52--u53 489 | 490 | 491 | 492 | u54 493 | 494 | 54 495 | 496 | 497 | u53--u54 498 | 499 | 500 | 501 | u55 502 | 503 | 55 504 | 505 | 506 | u54--u55 507 | 508 | 509 | 510 | u56 511 | 512 | 56 513 | 514 | 515 | u55--u56 516 | 517 | 518 | 519 | u57 520 | 521 | 57 522 | 523 | 524 | u56--u57 525 | 526 | 527 | 528 | u58 529 | 530 | 58 531 | 532 | 533 | u57--u58 534 | 535 | 536 | 537 | u59 538 | 539 | 59 540 | 541 | 542 | u58--u59 543 | 544 | 545 | 546 | u60 547 | 548 | 60 549 | 550 | 551 | u59--u60 552 | 553 | 554 | 555 | u61 556 | 557 | 61 558 | 559 | 560 | u60--u61 561 | 562 | 563 | 564 | u62 565 | 566 | 62 567 | 568 | 569 | u61--u62 570 | 571 | 572 | 573 | u63 574 | 575 | 63 576 | 577 | 578 | u62--u63 579 | 580 | 581 | 582 | u64 583 | 584 | 64 585 | 586 | 587 | u63--u64 588 | 589 | 590 | 591 | l1 592 | 593 | 1 594 | 595 | 596 | l1--u58 597 | 598 | 599 | 600 | l1--l1 601 | 602 | 603 | 604 | l2 605 | 606 | 2 607 | 608 | 609 | l1--l2 610 | 611 | 612 | 613 | l2--u50 614 | 615 | 616 | 617 | l3 618 | 619 | 3 620 | 621 | 622 | l2--l3 623 | 624 | 625 | 626 | l3--u42 627 | 628 | 629 | 630 | l4 631 | 632 | 4 633 | 634 | 635 | l3--l4 636 | 637 | 638 | 639 | l4--u34 640 | 641 | 642 | 643 | l5 644 | 645 | 5 646 | 647 | 648 | l4--l5 649 | 650 | 651 | 652 | l5--u26 653 | 654 | 655 | 656 | l6 657 | 658 | 6 659 | 660 | 661 | l5--l6 662 | 663 | 664 | 665 | l6--u18 666 | 667 | 668 | 669 | l7 670 | 671 | 7 672 | 673 | 674 | l6--l7 675 | 676 | 677 | 678 | l7--u10 679 | 680 | 681 | 682 | l8 683 | 684 | 8 685 | 686 | 687 | l7--l8 688 | 689 | 690 | 691 | l8--u2 692 | 693 | 694 | 695 | l9 696 | 697 | 9 698 | 699 | 700 | l8--l9 701 | 702 | 703 | 704 | l9--u60 705 | 706 | 707 | 708 | l10 709 | 710 | 10 711 | 712 | 713 | l9--l10 714 | 715 | 716 | 717 | l10--u52 718 | 719 | 720 | 721 | l11 722 | 723 | 11 724 | 725 | 726 | l10--l11 727 | 728 | 729 | 730 | l11--u44 731 | 732 | 733 | 734 | l12 735 | 736 | 12 737 | 738 | 739 | l11--l12 740 | 741 | 742 | 743 | l12--u36 744 | 745 | 746 | 747 | l13 748 | 749 | 13 750 | 751 | 752 | l12--l13 753 | 754 | 755 | 756 | l13--u28 757 | 758 | 759 | 760 | l14 761 | 762 | 14 763 | 764 | 765 | l13--l14 766 | 767 | 768 | 769 | l14--u20 770 | 771 | 772 | 773 | l15 774 | 775 | 15 776 | 777 | 778 | l14--l15 779 | 780 | 781 | 782 | l15--u12 783 | 784 | 785 | 786 | l16 787 | 788 | 16 789 | 790 | 791 | l15--l16 792 | 793 | 794 | 795 | l16--u4 796 | 797 | 798 | 799 | l17 800 | 801 | 17 802 | 803 | 804 | l16--l17 805 | 806 | 807 | 808 | l17--u62 809 | 810 | 811 | 812 | l18 813 | 814 | 18 815 | 816 | 817 | l17--l18 818 | 819 | 820 | 821 | l18--u54 822 | 823 | 824 | 825 | l19 826 | 827 | 19 828 | 829 | 830 | l18--l19 831 | 832 | 833 | 834 | l19--u46 835 | 836 | 837 | 838 | l20 839 | 840 | 20 841 | 842 | 843 | l19--l20 844 | 845 | 846 | 847 | l20--u38 848 | 849 | 850 | 851 | l21 852 | 853 | 21 854 | 855 | 856 | l20--l21 857 | 858 | 859 | 860 | l21--u30 861 | 862 | 863 | 864 | l22 865 | 866 | 22 867 | 868 | 869 | l21--l22 870 | 871 | 872 | 873 | l22--u22 874 | 875 | 876 | 877 | l23 878 | 879 | 23 880 | 881 | 882 | l22--l23 883 | 884 | 885 | 886 | l23--u14 887 | 888 | 889 | 890 | l24 891 | 892 | 24 893 | 894 | 895 | l23--l24 896 | 897 | 898 | 899 | l24--u6 900 | 901 | 902 | 903 | l25 904 | 905 | 25 906 | 907 | 908 | l24--l25 909 | 910 | 911 | 912 | l25--u64 913 | 914 | 915 | 916 | l26 917 | 918 | 26 919 | 920 | 921 | l25--l26 922 | 923 | 924 | 925 | l26--u56 926 | 927 | 928 | 929 | l27 930 | 931 | 27 932 | 933 | 934 | l26--l27 935 | 936 | 937 | 938 | l27--u48 939 | 940 | 941 | 942 | l28 943 | 944 | 28 945 | 946 | 947 | l27--l28 948 | 949 | 950 | 951 | l28--u40 952 | 953 | 954 | 955 | l29 956 | 957 | 29 958 | 959 | 960 | l28--l29 961 | 962 | 963 | 964 | l29--u32 965 | 966 | 967 | 968 | l30 969 | 970 | 30 971 | 972 | 973 | l29--l30 974 | 975 | 976 | 977 | l30--u24 978 | 979 | 980 | 981 | l31 982 | 983 | 31 984 | 985 | 986 | l30--l31 987 | 988 | 989 | 990 | l31--u16 991 | 992 | 993 | 994 | l32 995 | 996 | 32 997 | 998 | 999 | l31--l32 1000 | 1001 | 1002 | 1003 | l32--u8 1004 | 1005 | 1006 | 1007 | l33 1008 | 1009 | 33 1010 | 1011 | 1012 | l32--l33 1013 | 1014 | 1015 | 1016 | l33--u57 1017 | 1018 | 1019 | 1020 | l34 1021 | 1022 | 34 1023 | 1024 | 1025 | l33--l34 1026 | 1027 | 1028 | 1029 | l34--u49 1030 | 1031 | 1032 | 1033 | l35 1034 | 1035 | 35 1036 | 1037 | 1038 | l34--l35 1039 | 1040 | 1041 | 1042 | l35--u41 1043 | 1044 | 1045 | 1046 | l36 1047 | 1048 | 36 1049 | 1050 | 1051 | l35--l36 1052 | 1053 | 1054 | 1055 | l36--u33 1056 | 1057 | 1058 | 1059 | l37 1060 | 1061 | 37 1062 | 1063 | 1064 | l36--l37 1065 | 1066 | 1067 | 1068 | l37--u25 1069 | 1070 | 1071 | 1072 | l38 1073 | 1074 | 38 1075 | 1076 | 1077 | l37--l38 1078 | 1079 | 1080 | 1081 | l38--u17 1082 | 1083 | 1084 | 1085 | l39 1086 | 1087 | 39 1088 | 1089 | 1090 | l38--l39 1091 | 1092 | 1093 | 1094 | l39--u9 1095 | 1096 | 1097 | 1098 | l40 1099 | 1100 | 40 1101 | 1102 | 1103 | l39--l40 1104 | 1105 | 1106 | 1107 | l40--u1 1108 | 1109 | 1110 | 1111 | l41 1112 | 1113 | 41 1114 | 1115 | 1116 | l40--l41 1117 | 1118 | 1119 | 1120 | l41--u59 1121 | 1122 | 1123 | 1124 | l42 1125 | 1126 | 42 1127 | 1128 | 1129 | l41--l42 1130 | 1131 | 1132 | 1133 | l42--u51 1134 | 1135 | 1136 | 1137 | l43 1138 | 1139 | 43 1140 | 1141 | 1142 | l42--l43 1143 | 1144 | 1145 | 1146 | l43--u43 1147 | 1148 | 1149 | 1150 | l44 1151 | 1152 | 44 1153 | 1154 | 1155 | l43--l44 1156 | 1157 | 1158 | 1159 | l44--u35 1160 | 1161 | 1162 | 1163 | l45 1164 | 1165 | 45 1166 | 1167 | 1168 | l44--l45 1169 | 1170 | 1171 | 1172 | l45--u27 1173 | 1174 | 1175 | 1176 | l46 1177 | 1178 | 46 1179 | 1180 | 1181 | l45--l46 1182 | 1183 | 1184 | 1185 | l46--u19 1186 | 1187 | 1188 | 1189 | l47 1190 | 1191 | 47 1192 | 1193 | 1194 | l46--l47 1195 | 1196 | 1197 | 1198 | l47--u11 1199 | 1200 | 1201 | 1202 | l48 1203 | 1204 | 48 1205 | 1206 | 1207 | l47--l48 1208 | 1209 | 1210 | 1211 | l48--u3 1212 | 1213 | 1214 | 1215 | l49 1216 | 1217 | 49 1218 | 1219 | 1220 | l48--l49 1221 | 1222 | 1223 | 1224 | l49--u61 1225 | 1226 | 1227 | 1228 | l50 1229 | 1230 | 50 1231 | 1232 | 1233 | l49--l50 1234 | 1235 | 1236 | 1237 | l50--u53 1238 | 1239 | 1240 | 1241 | l51 1242 | 1243 | 51 1244 | 1245 | 1246 | l50--l51 1247 | 1248 | 1249 | 1250 | l51--u45 1251 | 1252 | 1253 | 1254 | l52 1255 | 1256 | 52 1257 | 1258 | 1259 | l51--l52 1260 | 1261 | 1262 | 1263 | l52--u37 1264 | 1265 | 1266 | 1267 | l53 1268 | 1269 | 53 1270 | 1271 | 1272 | l52--l53 1273 | 1274 | 1275 | 1276 | l53--u29 1277 | 1278 | 1279 | 1280 | l54 1281 | 1282 | 54 1283 | 1284 | 1285 | l53--l54 1286 | 1287 | 1288 | 1289 | l54--u21 1290 | 1291 | 1292 | 1293 | l55 1294 | 1295 | 55 1296 | 1297 | 1298 | l54--l55 1299 | 1300 | 1301 | 1302 | l55--u13 1303 | 1304 | 1305 | 1306 | l56 1307 | 1308 | 56 1309 | 1310 | 1311 | l55--l56 1312 | 1313 | 1314 | 1315 | l56--u5 1316 | 1317 | 1318 | 1319 | l57 1320 | 1321 | 57 1322 | 1323 | 1324 | l56--l57 1325 | 1326 | 1327 | 1328 | l57--u63 1329 | 1330 | 1331 | 1332 | l58 1333 | 1334 | 58 1335 | 1336 | 1337 | l57--l58 1338 | 1339 | 1340 | 1341 | l58--u55 1342 | 1343 | 1344 | 1345 | l59 1346 | 1347 | 59 1348 | 1349 | 1350 | l58--l59 1351 | 1352 | 1353 | 1354 | l59--u47 1355 | 1356 | 1357 | 1358 | l60 1359 | 1360 | 60 1361 | 1362 | 1363 | l59--l60 1364 | 1365 | 1366 | 1367 | l60--u39 1368 | 1369 | 1370 | 1371 | l61 1372 | 1373 | 61 1374 | 1375 | 1376 | l60--l61 1377 | 1378 | 1379 | 1380 | l61--u31 1381 | 1382 | 1383 | 1384 | l62 1385 | 1386 | 62 1387 | 1388 | 1389 | l61--l62 1390 | 1391 | 1392 | 1393 | l62--u23 1394 | 1395 | 1396 | 1397 | l63 1398 | 1399 | 63 1400 | 1401 | 1402 | l62--l63 1403 | 1404 | 1405 | 1406 | l63--u15 1407 | 1408 | 1409 | 1410 | l64 1411 | 1412 | 64 1413 | 1414 | 1415 | l63--l64 1416 | 1417 | 1418 | 1419 | l64--u7 1420 | 1421 | 1422 | 1423 | 1424 | -------------------------------------------------------------------------------- /docs/DES-key-schedule.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fffaraz/cppDES/a9f58e3f97825d44ac0d6a999ce4b6495369d0b6/docs/DES-key-schedule.png -------------------------------------------------------------------------------- /docs/DES-main-network.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fffaraz/cppDES/a9f58e3f97825d44ac0d6a999ce4b6495369d0b6/docs/DES-main-network.png -------------------------------------------------------------------------------- /docs/Data_Encription_Standard_Flow_Diagram.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | DES Flow Diagram 20 | Outline of data flow for DES 21 | 22 | 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 | 53 | E 54 | P 55 | S1 56 | S2 57 | S3 58 | S4 59 | S5 60 | S6 61 | S7 62 | S8 63 | Half Block (32 bits) 64 | Subkey (48 bits) 65 | 66 | 67 | 69 | 70 | -------------------------------------------------------------------------------- /docs/Riv85.txt: -------------------------------------------------------------------------------- 1 | TESTING IMPLEMENTATIONS OF DES 2 | ------------------------------ 3 | 4 | Ronald L. Rivest 5 | MIT Laboratory for Computer Science 6 | Cambridge, Mass. 02139 7 | 2/23/85 8 | 9 | 10 | ABSTRACT 11 | -------- 12 | 13 | We present a simple way to test the correctness of a DES implementation: 14 | Use the recurrence relation: 15 | 16 | X0 = 9474B8E8C73BCA7D (hexadecimal) 17 | 18 | X(i+1) = IF (i is even) THEN E(Xi,Xi) ELSE D(Xi,Xi) 19 | 20 | to compute a sequence of 64-bit values: X0, X1, X2, ..., X16. Here 21 | E(X,K) denotes the DES encryption of X using key K, and D(X,K) denotes 22 | the DES decryption of X using key K. If you obtain 23 | 24 | X16 = 1B1A2DDB4C642438 25 | 26 | your implementation does not have any of the 36,568 possible single-fault 27 | errors described herein. 28 | 29 | 30 | INTRODUCTION 31 | ------------ 32 | 33 | The Data Encryption Standard (DES) has been approved by a variety of 34 | organizations (e.g. the U.S. government) for use in cryptographic applications. 35 | The DES algorithm was published by the National Bureau of Standards [FIPS46]. 36 | 37 | The National Bureau of Standards has an ongoing program to validate the 38 | correctness of hardware DES implementations. Their testing procedure, 39 | described in [Ga80a], consists of 291 fixed test cases followed by twelve 40 | million randomly chosen test cases. A hardware implementation of DES that 41 | passes this test is awarded a "Validation Certficate". 42 | 43 | The above testing procedure provides good evidence that the DES hardware was 44 | correctly designed and functioning properly at the time of validation. 45 | However, the user of a DES implementation would like to maintain the currency 46 | of this validation -- this would be "maintenance testing" in contrast 47 | to the "validation" service provided by NBS. The purpose of maintenance 48 | testing is to ensure that the DES implementation is still correct, i.e. 49 | that no operational fault has appeared since the last time the the device 50 | was tested. Also note that NBS's validation procedure only inspects single 51 | instances of a hardware device; the user of a DES device wants to ensure that 52 | his device is correctly functioning (even though the design is OK, the device 53 | may have developed faults). 54 | 55 | One way to perform maintenance testing is to use two or more separate 56 | implementations and compare their results after each operation. If they 57 | ever differ, then one of the units has ceased to operate correctly. The 58 | difficulty with this approach is the cost of providing the redundant 59 | implementation. 60 | 61 | If a redundant implementation is infeasible, then the implementation may 62 | be testing by comparing its input-output behaviour with that of a correct 63 | implementation. This paper provides such a procedure. 64 | 65 | It is desired to have a relatively short test procedure, since the 66 | maintenance testing may be taking place in parallel with ordinary operations, 67 | and the cost of the test procedure may affect the unit's overall 68 | efficiency. The test specified in this paper only requires 16 DES operations. 69 | 70 | It is desirable to have an effective test procedure, in the 71 | sense that implementation errors are very likely to be caught. The test 72 | specified here will catch any of the 36,568 single-fault implementation 73 | errors specified later on. 74 | 75 | We next specify the DES implementation and error model, and then describe 76 | the experimental results that yielded the proposed maintenance test. 77 | 78 | 79 | DES IMPLEMENTATION 80 | ------------------ 81 | 82 | Our DES implementation follows the specifications for DES: 83 | 84 | DES(X,K,E) 85 | -- X is a 64-bit input value 86 | -- K is a 64-bit key 87 | -- E is TRUE if we are encrypting, else FALSE 88 | (1) Apply permutation IP to X, resulting in value LR (64 bits). 89 | (2) Apply selector PC1 to K, resulting in value CD (56 bits). 90 | (3) For ROUND = 1, 2, ..., 16: 91 | (3a) if E is TRUE then 92 | if SHIFTS[ROUND] = 1 93 | then apply permutation LSH1 to CD (in place). 94 | else apply permutation LSH2 to CD (in place). 95 | (3b) Apply selector PC2 to CD resulting in value KI (48 bits) 96 | (3c) If E is FALSE then 97 | if SHIFTS[17-ROUND] = 1 98 | then apply permutation RSH1 to CD (in place). 99 | else apply permutation RSH2 to CD (in place). 100 | (3d) Apply selector E to LR, resulting in value RE (48 bits). 101 | (3e) XOR RE with KI, resulting in value RX (48 bits). 102 | (3f) Break RX into 8 6-bit blocks, and replace the i-th block 103 | Yi with the result of looking up Yi in S-box i. 104 | Concatenate these 4-bit results together to get the 32-bit 105 | value SOUT. 106 | (3g) Apply permutation P to SOUT resulting in value FOUT (32 107 | bits). 108 | (3h) Replace the left half of LR with the XOR of FOUT and the 109 | left half of LR. 110 | (3i) If ROUND is not 16, apply permutation SWAP to LR (in 111 | place). 112 | (4) Apply permutation IPINV to LR resulting in value OUT (64 bits). 113 | Return OUT as the result of this DES operation. 114 | 115 | 116 | This implementation makes uses of the following tables and functions (for 117 | the details of these tables see [FIPS46]): 118 | 119 | (1) Permutations and selectors: IP, PC1, PC2, LSH1, LSH2, RSH1, RSH2, 120 | E, P, SWAP, IPINV. 121 | Each permutation or selector Q has a length LENGTH(Q) and a range 122 | 1...RANGE(Q). For permutations LENGTH(Q)=RANGE(Q). As an example, 123 | the selector PC2 = [14, 17, ..., 32] has length 48 and range 56, 124 | while permutation SWAP = [33, 34, ..., 32] has length 64 and range 125 | 64. 126 | 127 | (2) The table SHIFTS = [1, 1, 2, ..., 1] of length 16 specifying the 128 | number of left shifts of the key registers to perform during 129 | each round. 130 | 131 | (3) The XOR (exclusive-OR) functions used in steps (3e) and (3h), on 132 | input vectors of lengths 48 and 32, respectively. 133 | 134 | (4) The S-box tables used in step (3f). 135 | 136 | 137 | 138 | ERROR MODEL 139 | ----------- 140 | 141 | We now describe our model of implementation errors. We consider the following 142 | set of single-fault errors: 143 | 144 | (1) "Wiring errors": For each permutation or selector Q, we consider 145 | the possibility that each of the output elements might 146 | -- be stuck at 0 147 | -- be stuck at 1 148 | -- incorrectly specify some other value in 1...RANGE(Q). 149 | There are thus LENGTH(Q)*(RANGE(Q)+1) possible errors for Q. 150 | 151 | Permutation Number 152 | or of Possible 153 | Selector LENGTH RANGE Errors 154 | ------------ ------- ------ ----------- 155 | IP 64 64 4160 156 | PC1 56 64 3640 157 | PC2 48 56 2736 158 | LSH1 56 56 3192 159 | LSH2 56 56 3192 160 | RSH1 56 56 3192 161 | RHS2 56 56 3192 162 | E 48 32 1584 163 | P 32 32 1056 164 | SWAP 64 64 4160 165 | IPINV 64 64 4160 166 | ----------------------------------------------------------- 167 | Total Number of Possible Wiring Errors........34,264 168 | 169 | (2) Shift Errors: For each of the 16 positions of the SHIFTS table, 170 | one error possibility is that the table entry could be incorrectly 171 | specified (i.e. specify a shift of 1 when a shift of two was 172 | desired, or vice versa). 173 | 174 | Total Number of Possible Shifting Errors..........16 175 | 176 | (3) XOR errors: For each XOR gate (there are 48 used in step (3e) 177 | and 32 used in step (3h)) the following errors are possible: 178 | -- stuck at 0 179 | -- stuck at 1 180 | -- producing the negation of the desired result. 181 | 182 | Total Number of Possible XOR Errors..............240 183 | 184 | (4) SBOX errors: For each of the 8 * 64 * 4 = 2048 bits of the S-box 185 | tables, we consider the possibility that it might be incorrectly 186 | stored (i.e. a 0 is stored instead of a 1, or vice versa). 187 | 188 | Total Number of Possible SBOX Errors............2048 189 | 190 | ------------------------------------------------------------------- 191 | Total Number of Error Possibilities Considered........36,568 192 | 193 | We note that since our implementation is iterative, an error possibility 194 | that can affect one round of encryption can affect every round. 195 | 196 | 197 | DESIGN OF VALIDATION TEST 198 | ------------------------- 199 | 200 | A simple iterative test was desired, so that the only storage 201 | requirements were the desired starting value, the desired ending value, and 202 | the number of DES iterations to perform. Alternation of encryption and 203 | decryption was chosen in order to exercise both modes equally. The initial 204 | value, X0 = 9474B8E8C73BCA7D (hexadecimal) was chosen with a short search, 205 | in order to find a sequence that tested the S-box entries efficiently. 206 | To generate a pseudo-random sequence of test values, the output of 207 | one stage is used as both the key and data inputs for the next stage. 208 | That is, the pseudo-random sequence is defined by: 209 | 210 | X0 = 9474B8E8C73BCA7D 211 | 212 | X(i+1) = E(Xi,Xi) if i is even 213 | X(i+1) = D(Xi,Xi) if i is odd 214 | 215 | where E(X,K) denotes the encryption of the 64-bit value X with 64-bit key 216 | K, and D(X,K) denotes the corresponding decryption. Note that the key is 217 | a 64-bit value as specified in [FIPS46]; the 8 "parity" bits are discarded by 218 | selector PC1. 219 | 220 | DES was implemented in software on an IBM PC. The correctness of this 221 | implementation was checked using the 291 fixed tests given in [Ga77]. 222 | The various error possibilities were implemented, and an experiment was 223 | run to determine the least i such that comparing Xi to its expected value 224 | detected all the proposed single-fault errors. Note that only one comparison 225 | is needed; it is not necessary to compare the intermediate results with their 226 | expected values. The following table was obtained: 227 | 228 | i Xi Number of errors NOT detected 229 | -- ---------------- ----------------------------- 230 | 0 9474B8E8C73BCA7D 36,568 231 | 1 8DA744E0C94E5E17 14,170 232 | 2 0CDB25E3BA3C6D79 4,842 233 | 3 4784C4BA5006081F 2,866 234 | 4 1CF1FC126F2EF842 1,550 235 | 5 E4BE250042098D13 996 236 | 6 7BFC5DC6ADB5797C 652 237 | 7 1AB3B4D82082FB28 458 238 | 8 C1576A14DE707097 274 239 | 9 739B68CD2E26782A 180 240 | 10 2A59F0C464506EDB 126 241 | 11 A5C39D4251F0A81E 94 242 | 12 7239AC9A6107DDB1 72 243 | 13 070CAC8590241233 52 244 | 14 78F87B6E3DFECF61 20 245 | 15 95EC2578C2C433F0 4 246 | 16 1B1A2DDB4C642438 0 247 | 248 | We conclude that at most 16 DES operations suffice to test the correctness 249 | of a DES implementation, under our error model. 250 | 251 | We note that the number of rounds required was determined by the S-box tests. 252 | The other error types were all detected within at most 11 iterations. 253 | The number of DES operations required here is 3 less than in the set of S-box 254 | tests published by NBS[Ga77]; a short search discovered the given starting 255 | value. We leave it as an open problem to devise a test which uses 256 | substantially fewer DES operations. 257 | 258 | We note that the above test may not catch multiple faults, or faults other than 259 | those described in this paper. 260 | 261 | 262 | ACKNOWLEDGEMENTS 263 | ---------------- 264 | 265 | This work was supported by RSA Data Security, Inc. 266 | 267 | 268 | REFERENCES 269 | ---------- 270 | 271 | [FIPS46] "Specifications for the Data Encryption Standard." Federal 272 | Information Processing Standards Publication 46 (January 15, 1977). 273 | 274 | [Ga80a] Gait, Jason. "Validating the Correctness of Hardware Implementations 275 | of the NBS Data Encryption Standard". NBS Special Publication 500-20. 276 | (Rev. September 1980). 277 | 278 | [Ga80b] Gait, Jason. "Maintenance Testing for the Data Encryption Standard." 279 | NBS Special Publication 500-61. (August 1980). 280 | 281 | -------------------------------------------------------------------------------- /docs/The DES Algorithm Illustrated.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The DES Algorithm Illustrated 5 | 6 | [Email Reply] 7 | 8 |

The DES Algorithm Illustrated

9 |

by J. Orlin Grabbe

10 |
11 |

12 | The DES (Data Encryption Standard) algorithm is the 13 | most widely used encryption algorithm in the world. For 14 | many years, and among many people, "secret code making" and 15 | DES have been synonymous. And despite the recent coup by 16 | the Electronic Frontier Foundation in creating a $220,000 17 | machine to crack DES-encrypted messages, DES will live on in 18 | government and banking for years to come through a life- 19 | extending version called "triple-DES." 20 |

21 | How does DES work? This article explains the various 22 | steps involved in DES-encryption, illustrating each step by 23 | means of a simple example. Since the creation of DES, many 24 | other algorithms (recipes for changing data) have emerged 25 | which are based on design principles similar to DES. Once 26 | you understand the basic transformations that take place in 27 | DES, you will find it easy to follow the steps involved in 28 | these more recent algorithms. 29 |

30 | But first a bit of history of how DES came about is 31 | appropriate, as well as a look toward the future. 32 |

33 |

The National Bureau of Standards Coaxes the Genie from the Bottle

34 |

35 | On May 15, 1973, during the reign of Richard Nixon, the 36 | National Bureau of Standards (NBS) published a notice in the 37 | Federal Register soliciting proposals for cryptographic 38 | algorithms to protect data during transmission and storage. 39 | The notice explained why encryption was an important issue. 40 |

41 |

42 | Over the last decade, there has been an 43 | accelerating increase in the accumulations and 44 | communication of digital data by government, 45 | industry and by other organizations in the private 46 | sector. The contents of these communicated and 47 | stored data often have very significant value 48 | and/or sensitivity. It is now common to find data 49 | transmissions which constitute funds transfers of 50 | several million dollars, purchase or sale of 51 | securities, warrants for arrests or arrest and 52 | conviction records being communicated between law 53 | enforcement agencies, airline reservations and 54 | ticketing representing investment and value both 55 | to the airline and passengers, and health and 56 | patient care records transmitted among physicians 57 | and treatment centers. 58 |

59 | The increasing volume, value and confidentiality 60 | of these records regularly transmitted and stored 61 | by commercial and government agencies has led to 62 | heightened recognition and concern over their 63 | exposures to unauthorized access and use. This 64 | misuse can be in the form of theft or defalcations 65 | of data records representing money, malicious 66 | modification of business inventories or the 67 | interception and misuse of confidential 68 | information about people. The need for protection 69 | is then apparent and urgent. 70 |

71 | It is recognized that encryption (otherwise known 72 | as scrambling, enciphering or privacy 73 | transformation) represents the only means of 74 | protecting such data during transmission and a 75 | useful means of protecting the content of data 76 | stored on various media, providing encryption of 77 | adequate strength can be devised and validated and 78 | is inherently integrable into system architecture. 79 | The National Bureau of Standards solicits proposed 80 | techniques and algorithms for computer data 81 | encryption. The Bureau also solicits recommended 82 | techniques for implementing the cryptographic 83 | function: for generating, evaluating, and 84 | protecting cryptographic keys; for maintaining 85 | files encoded under expiring keys; for making 86 | partial updates to encrypted files; and mixed 87 | clear and encrypted data to permit labelling, 88 | polling, routing, etc. The Bureau in its role for 89 | establishing standards and aiding government and 90 | industry in assessing technology, will arrange for 91 | the evaluation of protection methods in order to 92 | prepare guidelines. 93 |

94 |

95 | NBS waited for the responses to come in. It received 96 | none until August 6, 1974, three days before Nixon's 97 | resignation, when IBM submitted a candidate that it had 98 | developed internally under the name LUCIFER. After 99 | evaluating the algorithm with the help of the National 100 | Security Agency (NSA), the NBS adopted a modification of the 101 | LUCIFER algorithm as the new Data Encryption Standard (DES) 102 | on July 15, 1977. 103 |

104 | DES was quickly adopted for non-digital media, such as 105 | voice-grade public telephone lines. Within a couple of 106 | years, for example, International Flavors and Fragrances was 107 | using DES to protect its valuable formulas transmitted over 108 | the phone ("With Data Encryption, Scents Are Safe at IFF," 109 | Computerworld 14, No. 21, 95 (1980).) 110 |

111 | Meanwhile, the banking industry, which is the largest 112 | user of encryption outside government, adopted DES as a 113 | wholesale banking standard. Standards for the wholesale 114 | banking industry are set by the American National Standards 115 | Institute (ANSI). ANSI X3.92, adopted in 1980, specified 116 | the use of the DES algorithm. 117 |

118 |

Some Preliminary Examples of DES

119 |

120 | DES works on bits, or binary numbers--the 0s and 1s 121 | common to digital computers. Each group of four bits makes 122 | up a hexadecimal, or base 16, number. Binary "0001" is 123 | equal to the hexadecimal number "1", binary "1000" is equal 124 | to the hexadecimal number "8", "1001" is equal to the 125 | hexadecimal number "9", "1010" is equal to the hexadecimal 126 | number "A", and "1111" is equal to the hexadecimal number 127 | "F". 128 |

129 | DES works by encrypting groups of 64 message bits, 130 | which is the same as 16 hexadecimal numbers. To do the 131 | encryption, DES uses "keys" where are also apparently 16 132 | hexadecimal numbers long, or apparently 64 bits long. 133 | However, every 8th key bit is ignored in the DES algorithm, 134 | so that the effective key size is 56 bits. But, in any 135 | case, 64 bits (16 hexadecimal digits) is the round number 136 | upon which DES is organized. 137 |

138 | For example, if we take the plaintext message 139 | "8787878787878787", and encrypt it with the DES key 140 | "0E329232EA6D0D73", we end up with the ciphertext 141 | "0000000000000000". If the ciphertext is decrypted with the 142 | same secret DES key "0E329232EA6D0D73", the result is the 143 | original plaintext "8787878787878787". 144 |

145 | This example is neat and orderly because our plaintext 146 | was exactly 64 bits long. The same would be true if the 147 | plaintext happened to be a multiple of 64 bits. But most 148 | messages will not fall into this category. They will not be 149 | an exact multiple of 64 bits (that is, an exact multiple of 150 | 16 hexadecimal numbers). 151 |

152 | For example, take the message "Your lips are smoother 153 | than vaseline". This plaintext message is 38 bytes (76 154 | hexadecimal digits) long. So this message must be padded 155 | with some extra bytes at the tail end for the encryption. 156 | Once the encrypted message has been decrypted, these extra 157 | bytes are thrown away. There are, of course, different 158 | padding schemes--different ways to add extra bytes. Here we 159 | will just add 0s at the end, so that the total message is a 160 | multiple of 8 bytes (or 16 hexadecimal digits, or 64 bits). 161 |

162 | The plaintext message "Your lips are smoother than 163 | vaseline" is, in hexadecimal, 164 |

165 | "596F7572206C6970 732061726520736D 6F6F746865722074 68616E2076617365 6C696E650D0A". 166 |

167 | (Note here that the first 72 hexadecimal digits represent 168 | the English message, while "0D" is hexadecimal for Carriage 169 | Return, and "0A" is hexadecimal for Line Feed, showing that 170 | the message file has terminated.) We then pad this message 171 | with some 0s on the end, to get a total of 80 hexadecimal 172 | digits: 173 |

174 | "596F7572206C6970 732061726520736D 6F6F746865722074 68616E2076617365 6C696E650D0A0000". 175 |

176 | If we then encrypt this plaintext message 64 bits (16 177 | hexadecimal digits) at a time, using the same DES key 178 | "0E329232EA6D0D73" as before, we get the ciphertext: 179 |

180 | "C0999FDDE378D7ED 727DA00BCA5A84EE 47F269A4D6438190 9DD52F78F5358499 828AC9B453E0E653". 181 |

182 | This is the secret code that can be transmitted or stored. 183 | Decrypting the ciphertext restores the original message 184 | "Your lips are smoother than vaseline". (Think how much 185 | better off Bill Clinton would be today, if Monica Lewinsky 186 | had used encryption on her Pentagon computer!) 187 |

188 |

How DES Works in Detail

189 |

190 | DES is a block cipher--meaning it operates on plaintext 191 | blocks of a given size (64-bits) and returns ciphertext 192 | blocks of the same size. Thus DES results in a permutation 193 | among the 2^64 (read this as: "2 to the 64th power") possible arrangements of 64 bits, each of 194 | which may be either 0 or 1. Each block of 64 bits is divided 195 | into two blocks of 32 bits each, a left half block L and a 196 | right half R. (This division is only used in certain 197 | operations.) 198 |

199 | Example: Let M be the plain text message M = 200 | 0123456789ABCDEF, where M is in hexadecimal (base 16) 201 | format. Rewriting M in binary format, we get the 64-bit 202 | block of text: 203 |

204 | M = 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
205 | L = 0000 0001 0010 0011 0100 0101 0110 0111
206 | R = 1000 1001 1010 1011 1100 1101 1110 1111 207 |

208 | The first bit of M is "0". The last bit is "1". We read 209 | from left to right. 210 |

211 | DES operates on the 64-bit blocks using key sizes of 56- 212 | bits. The keys are actually stored as being 64 bits long, 213 | but every 8th bit in the key is not used (i.e. bits numbered 214 | 8, 16, 24, 32, 40, 48, 56, and 64). However, we will 215 | nevertheless number the bits from 1 to 64, going left to 216 | right, in the following calculations. But, as you will see, 217 | the eight bits just mentioned get eliminated when we create 218 | subkeys. 219 |

220 | Example: Let K be the hexadecimal key K = 221 | 133457799BBCDFF1. This gives us as the binary key (setting 222 | 1 = 0001, 3 = 0011, etc., and grouping together every eight 223 | bits, of which the last one in each group will be unused): 224 |

225 | K = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001 226 |

227 | The DES algorithm uses the following steps: 228 |

229 |

Step 1: Create 16 subkeys, each of which is 48-bits long.

230 |

231 | The 64-bit key is permuted according to the following 232 | table, PC-1. Since the first entry in the table is "57", 233 | this means that the 57th bit of the original key K becomes 234 | the first bit of the permuted key K+. The 49th bit of the 235 | original key becomes the second bit of the permuted key. 236 | The 4th bit of the original key is the last bit of the 237 | permuted key. Note only 56 bits of the original key appear 238 | in the permuted key. 239 |

240 |

241 |                             PC-1
242 | 
243 |               57   49    41   33    25    17    9
244 |                1   58    50   42    34    26   18
245 |               10    2    59   51    43    35   27
246 |               19   11     3   60    52    44   36
247 |               63   55    47   39    31    23   15
248 |                7   62    54   46    38    30   22
249 |               14    6    61   53    45    37   29
250 |               21   13     5   28    20    12    4
251 | 
252 |

253 | Example: From the original 64-bit key 254 |

255 | K = 00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001 256 |

257 | we get the 56-bit permutation 258 |

259 | K+ = 1111000 0110011 0010101 0101111 0101010 1011001 1001111 0001111 260 |

261 | Next, split this key into left and right halves, C0 and 262 | D0, where each half has 28 bits. 263 |

264 | Example: From the permuted key K+, we get 265 |

266 | C0 = 1111000 0110011 0010101 0101111
267 | D0 = 0101010 1011001 1001111 0001111 268 |

269 | With C0 and D0 defined, we now create sixteen blocks Cn 270 | and Dn, 1<=n<=16. Each pair of blocks Cn and Dn is formed 271 | from the previous pair Cn-1 and Dn-1, respectively, for n = 272 | 1, 2, ..., 16, using the following schedule of "left shifts" 273 | of the previous block. To do a left shift, move each bit 274 | one place to the left, except for the first bit, which is 275 | cycled to the end of the block. 276 |

277 |

278 |                      Iteration     Number of
279 |                       Number      Left Shifts
280 | 
281 |                           1          1
282 |                           2          1
283 |                           3          2
284 |                           4          2
285 |                           5          2
286 |                           6          2
287 |                           7          2
288 |                           8          2
289 |                           9          1
290 |                          10          2
291 |                          11          2
292 |                          12          2
293 |                          13          2
294 |                          14          2
295 |                          15          2
296 |                          16          1
297 | 
298 |

299 | This means, for example, C3 and D3 are obtained from C2 and 300 | D2, respectively, by two left shifts, and C16 and D16 are 301 | obtained from C15 and D15, respectively, by one left shift. 302 | In all cases, by a single left shift is meant a rotation of 303 | the bits one place to the left, so that after one left shift 304 | the bits in the 28 positions are the bits that were 305 | previously in positions 2, 3,..., 28, 1. 306 |

307 | Example: From original pair pair C0 and D0 we obtain: 308 |

309 | C0 = 1111000011001100101010101111
310 | D0 = 0101010101100110011110001111 311 |

312 | C1 = 1110000110011001010101011111
313 | D1 = 1010101011001100111100011110 314 |

315 | C2 = 1100001100110010101010111111
316 | D2 = 0101010110011001111000111101 317 |

318 | C3 = 0000110011001010101011111111
319 | D3 = 0101011001100111100011110101 320 |

321 | C4 = 0011001100101010101111111100
322 | D4 = 0101100110011110001111010101 323 |

324 | C5 = 1100110010101010111111110000
325 | D5 = 0110011001111000111101010101 326 |

327 | C6 = 0011001010101011111111000011
328 | D6 = 1001100111100011110101010101 329 |

330 | C7 = 1100101010101111111100001100
331 | D7 = 0110011110001111010101010110 332 |

333 | C8 = 0010101010111111110000110011
334 | D8 = 1001111000111101010101011001 335 |

336 | C9 = 0101010101111111100001100110
337 | D9 = 0011110001111010101010110011 338 |

339 | C10 = 0101010111111110000110011001
340 | D10 = 1111000111101010101011001100 341 |

342 | C11 = 0101011111111000011001100101
343 | D11 = 1100011110101010101100110011 344 |

345 | C12 = 0101111111100001100110010101
346 | D12 = 0001111010101010110011001111 347 |

348 | C13 = 0111111110000110011001010101
349 | D13 = 0111101010101011001100111100 350 |

351 | C14 = 1111111000011001100101010101
352 | D14 = 1110101010101100110011110001 353 |

354 | C15 = 1111100001100110010101010111
355 | D15 = 1010101010110011001111000111 356 |

357 | C16 = 1111000011001100101010101111
358 | D16 = 0101010101100110011110001111 359 |

360 | We now form the keys Kn, for 1<=n<=16, by applying the 361 | following permutation table to each of the concatenated 362 | pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 of 363 | these. 364 |

365 |

366 |                               PC-2
367 | 
368 |                  14    17   11    24     1    5
369 |                   3    28   15     6    21   10
370 |                  23    19   12     4    26    8
371 |                  16     7   27    20    13    2
372 |                  41    52   31    37    47   55
373 |                  30    40   51    45    33   48
374 |                  44    49   39    56    34   53
375 |                  46    42   50    36    29   32
376 | 
377 |

378 | Therefore, the first bit of Kn is the 14th bit of CnDn, the 379 | second bit the 17th, and so on, ending with the 48th bit of 380 | Kn being the 32th bit of CnDn. 381 |

382 | Example: For the first key we have 383 | 384 | C1D1 = 1110000 1100110 0101010 1011111 1010101 0110011 0011110 0011110 385 |

386 | which, after we apply the permutation PC-2, becomes 387 |

388 | K1 = 000110 110000 001011 101111 111111 000111 000001 110010 389 |

390 | For the other keys we have 391 |

392 | K2 = 011110 011010 111011 011001 110110 111100 100111 100101
393 | K3 = 010101 011111 110010 001010 010000 101100 111110 011001
394 | K4 = 011100 101010 110111 010110 110110 110011 010100 011101
395 | K5 = 011111 001110 110000 000111 111010 110101 001110 101000
396 | K6 = 011000 111010 010100 111110 010100 000111 101100 101111
397 | K7 = 111011 001000 010010 110111 111101 100001 100010 111100
398 | K8 = 111101 111000 101000 111010 110000 010011 101111 111011
399 | K9 = 111000 001101 101111 101011 111011 011110 011110 000001
400 | K10 = 101100 011111 001101 000111 101110 100100 011001 001111
401 | K11 = 001000 010101 111111 010011 110111 101101 001110 000110
402 | K12 = 011101 010111 000111 110101 100101 000110 011111 101001
403 | K13 = 100101 111100 010111 010001 111110 101011 101001 000001
404 | K14 = 010111 110100 001110 110111 111100 101110 011100 111010
405 | K15 = 101111 111001 000110 001101 001111 010011 111100 001010
406 | K16 = 110010 110011 110110 001011 000011 100001 011111 110101
407 |

408 | So much for the subkeys. Now we look at the message itself. 409 |

410 |

Step 2: Encode each 64-bit block of data.

411 |

412 | There is an initial permutation IP of the 64 bits of 413 | the message data M. This rearranges the bits according to 414 | the following table, where the entries in the table show the 415 | new arrangement of the bits from their initial order. The 416 | 58th bit of M becomes the first bit of IP. The 50th bit of 417 | M becomes the second bit of IP. The 7th bit of M is the 418 | last bit of IP. 419 |

420 |

421 |                              IP
422 | 
423 |             58    50   42    34    26   18    10    2
424 |             60    52   44    36    28   20    12    4
425 |             62    54   46    38    30   22    14    6
426 |             64    56   48    40    32   24    16    8
427 |             57    49   41    33    25   17     9    1
428 |             59    51   43    35    27   19    11    3
429 |             61    53   45    37    29   21    13    5
430 |             63    55   47    39    31   23    15    7
431 | 
432 |

433 | Example: Applying the initial permutation to the block 434 | of text M, given previously, we get 435 |

436 | M = 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
437 | IP = 1100 1100 0000 0000 1100 1100 1111 1111 1111 0000 1010 1010 1111 0000 1010 1010 438 |

439 | Here the 58th bit of M is "1", which becomes the first bit 440 | of IP. The 50th bit of M is "1", which becomes the second 441 | bit of IP. The 7th bit of M is "0", which becomes the last 442 | bit of IP. 443 |

444 | Next divide the permuted block IP into a left half L0 445 | of 32 bits, and a right half R0 of 32 bits. 446 |

447 | Example: From IP, we get L0 and R0 448 |

449 | L0 = 1100 1100 0000 0000 1100 1100 1111 1111
450 | R0 = 1111 0000 1010 1010 1111 0000 1010 1010 451 |

452 | We now proceed through 16 iterations, for 1<=n<=16, using 453 | a function f which operates on two blocks--a data block of 454 | 32 bits and a key Kn of 48 bits--to produce a block of 32 455 | bits. Let + denote XOR addition, (bit-by-bit addition 456 | modulo 2). Then for n going from 1 to 16 we calculate 457 |

458 |

459 | Ln = Rn-1
460 | Rn = Ln-1 + f(Rn-1,Kn) 461 |
462 |

463 | This results in a final block, for n = 16, of L16R16. That 464 | is, in each iteration, we take the right 32 bits of the 465 | previous result and make them the left 32 bits of the 466 | current step. For the right 32 bits in the current step, we 467 | XOR the left 32 bits of the previous step with the 468 | calculation f . 469 |

470 | Example: For n = 1, we have 471 |

472 | K1 = 000110 110000 001011 101111 111111 000111 000001 110010
473 | L1 = R0 = 1111 0000 1010 1010 1111 0000 1010 1010
474 | R1 = L0 + f(R0,K1) 475 |

476 | It remains to explain how the function f works. To 477 | calculate f, we first expand each block Rn-1 from 32 bits to 478 | 48 bits. This is done by using a selection table that 479 | repeats some of the bits in Rn-1 . We'll call the use of 480 | this selection table the function E. Thus E(Rn-1) has a 32 481 | bit input block, and a 48 bit output block. 482 |

483 | Let E be such that the 48 bits of its output, written 484 | as 8 blocks of 6 bits each, are obtained by selecting the 485 | bits in its inputs in order according to the following 486 | table: 487 |

488 |

489 |                     E BIT-SELECTION TABLE
490 | 
491 |                  32     1    2     3     4    5
492 |                   4     5    6     7     8    9
493 |                   8     9   10    11    12   13
494 |                  12    13   14    15    16   17
495 |                  16    17   18    19    20   21
496 |                  20    21   22    23    24   25
497 |                  24    25   26    27    28   29
498 |                  28    29   30    31    32    1
499 | 
500 |

501 | Thus the first three bits of E(Rn-1) are the bits in 502 | positions 32, 1 and 2 of Rn-1 while the last 2 bits of E(Rn-1) are the bits in positions 32 and 1. 503 |

504 | Example: We calculate E(R0) from R0 as follows: 505 |

506 | R0 = 1111 0000 1010 1010 1111 0000 1010 1010
507 | E(R0) = 011110 100001 010101 010101 011110 100001 010101 010101 508 |

509 | (Note that each block of 4 original bits has been 510 | expanded to a block of 6 output bits.) 511 |

512 | Next in the f calculation, we XOR the output 513 | E(Rn-1) with the key Kn: 514 |

515 | Kn + E(Rn-1). 516 |

517 | Example: For K1 , E(R0), we have 518 |

519 | K1 = 000110 110000 001011 101111 111111 000111 000001 110010
520 | E(R0) = 011110 100001 010101 010101 011110 100001 010101 010101
521 | K1+E(R0) = 011000 010001 011110 111010 100001 100110 010100 100111. 522 |

523 | We have not yet finished calculating the function f . 524 | To this point we have expanded Rn-1 from 32 bits to 48 525 | bits, using the selection table, and XORed the result with 526 | the key Kn . We now have 48 bits, or eight groups of six 527 | bits. We now do something strange with each group of six 528 | bits: we use them as addresses in tables called "S boxes". 529 | Each group of six bits will give us an address in a 530 | different S box. Located at that address will be a 4 bit 531 | number. This 4 bit number will replace the original 6 bits. 532 | The net result is that the eight groups of 6 bits are 533 | transformed into eight groups of 4 bits (the 4-bit outputs 534 | from the S boxes) for 32 bits total. 535 |

536 | Write the previous result, which is 48 bits, in 537 | the form: 538 |

539 | Kn + E(Rn-1) =B1B2B3B4B5B6B7B8, 540 |

541 | where each Bi is a group of six bits. We now calculate 542 |

543 | S1(B1)S2(B2)S3(B3)S4(B4)S5(B5)S6(B6)S7(B7)S8(B8) 544 |

545 |

546 | where Si(Bi) referres to the output of the i-th S 547 | box. 548 |

549 | To repeat, each of the functions S1, S2,..., S8, takes 550 | a 6-bit block as input and yields a 4-bit block as output. 551 | The table to determine S1 is shown and explained below: 552 |

553 |

554 | 
555 |                              S1
556 | 
557 |                         Column Number
558 | Row
559 | No.    0  1   2  3   4  5   6  7   8  9  10 11  12 13  14 15
560 | 
561 |   0   14  4  13  1   2 15  11  8   3 10   6 12   5  9   0  7
562 |   1    0 15   7  4  14  2  13  1  10  6  12 11   9  5   3  8
563 |   2    4  1  14  8  13  6   2 11  15 12   9  7   3 10   5  0
564 |   3   15 12   8  2   4  9   1  7   5 11   3 14  10  0   6 13
565 | 
566 |

567 | If S1 is the function defined in this table and B is a block 568 | of 6 bits, then S1(B) is determined as follows: The first 569 | and last bits of B represent in base 2 a number in the 570 | decimal range 0 to 3 (or binary 00 to 11). Let that number 571 | be i. The middle 4 bits of B represent in base 2 a number 572 | in the decimal range 0 to 15 (binary 0000 to 1111). Let 573 | that number be j. Look up in the table the number in the i-th row and j-th column. It is a number in the range 0 to 15 574 | and is uniquely represented by a 4 bit block. That block is 575 | the output S1(B) of S1 for the input B. For example, for 576 | input block B = 011011 the first bit is "0" and the last bit 577 | "1" giving 01 as the row. This is row 1. The middle four 578 | bits are "1101". This is the binary equivalent of decimal 579 | 13, so the column is column number 13. In row 1, column 13 580 | appears 5. This determines the output; 5 is binary 0101, so 581 | that the output is 0101. Hence S1(011011) = 0101. 582 |

583 | The tables defining the functions S1,...,S8 are 584 | the following: 585 |

586 |

587 |                              S1
588 | 
589 |      14  4  13  1   2 15  11  8   3 10   6 12   5  9   0  7
590 |       0 15   7  4  14  2  13  1  10  6  12 11   9  5   3  8
591 |       4  1  14  8  13  6   2 11  15 12   9  7   3 10   5  0
592 |      15 12   8  2   4  9   1  7   5 11   3 14  10  0   6 13
593 | 
594 |                              S2
595 | 
596 |      15  1   8 14   6 11   3  4   9  7   2 13  12  0   5 10
597 |       3 13   4  7  15  2   8 14  12  0   1 10   6  9  11  5
598 |       0 14   7 11  10  4  13  1   5  8  12  6   9  3   2 15
599 |      13  8  10  1   3 15   4  2  11  6   7 12   0  5  14  9
600 | 
601 |                              S3
602 | 
603 |      10  0   9 14   6  3  15  5   1 13  12  7  11  4   2  8
604 |      13  7   0  9   3  4   6 10   2  8   5 14  12 11  15  1
605 |      13  6   4  9   8 15   3  0  11  1   2 12   5 10  14  7
606 |       1 10  13  0   6  9   8  7   4 15  14  3  11  5   2 12
607 | 
608 |                              S4
609 | 
610 |       7 13  14  3   0  6   9 10   1  2   8  5  11 12   4 15
611 |      13  8  11  5   6 15   0  3   4  7   2 12   1 10  14  9
612 |      10  6   9  0  12 11   7 13  15  1   3 14   5  2   8  4
613 |       3 15   0  6  10  1  13  8   9  4   5 11  12  7   2 14
614 | 
615 |                              S5
616 | 
617 |       2 12   4  1   7 10  11  6   8  5   3 15  13  0  14  9
618 |      14 11   2 12   4  7  13  1   5  0  15 10   3  9   8  6
619 |       4  2   1 11  10 13   7  8  15  9  12  5   6  3   0 14
620 |      11  8  12  7   1 14   2 13   6 15   0  9  10  4   5  3
621 | 
622 |                              S6
623 | 
624 |      12  1  10 15   9  2   6  8   0 13   3  4  14  7   5 11
625 |      10 15   4  2   7 12   9  5   6  1  13 14   0 11   3  8
626 |       9 14  15  5   2  8  12  3   7  0   4 10   1 13  11  6
627 |       4  3   2 12   9  5  15 10  11 14   1  7   6  0   8 13
628 | 
629 |                              S7
630 | 
631 |       4 11   2 14  15  0   8 13   3 12   9  7   5 10   6  1
632 |      13  0  11  7   4  9   1 10  14  3   5 12   2 15   8  6
633 |       1  4  11 13  12  3   7 14  10 15   6  8   0  5   9  2
634 |       6 11  13  8   1  4  10  7   9  5   0 15  14  2   3 12
635 | 
636 |                              S8
637 | 
638 |      13  2   8  4   6 15  11  1  10  9   3 14   5  0  12  7
639 |       1 15  13  8  10  3   7  4  12  5   6 11   0 14   9  2
640 |       7 11   4  1   9 12  14  2   0  6  10 13  15  3   5  8
641 |       2  1  14  7   4 10   8 13  15 12   9  0   3  5   6 11
642 | 
643 |

644 | Example: For the first round, we obtain as the 645 | output of the eight S boxes: 646 |

647 | K1 + E(R0) = 011000 010001 011110 111010 100001 100110 010100 100111. 648 |

649 | S1(B1)S2(B2)S3(B3)S4(B4)S5(B5)S6(B6)S7(B7)S8(B8) 650 | = 0101 1100 1000 0010 1011 0101 1001 0111 651 |

652 | The final stage in the calculation of f is to do a 653 | permutation P of the S-box output to obtain the final value 654 | of f: 655 |

656 | f = P(S1(B1)S2(B2)...S8(B8)) 657 |

658 | The permutation P is defined in the following table. P 659 | yields a 32-bit output from a 32-bit input by permuting the 660 | bits of the input block. 661 |

662 |

663 |                                 P
664 | 
665 |                          16   7  20  21
666 |                          29  12  28  17
667 |                           1  15  23  26
668 |                           5  18  31  10
669 |                           2   8  24  14
670 |                          32  27   3   9
671 |                          19  13  30   6
672 |                          22  11   4  25
673 | 
674 |

675 | Example: From the output of the eight S boxes: 676 |

677 | S1(B1)S2(B2)S3(B3)S4(B4)S5(B5)S6(B6)S7(B7)S8(B8) = 0101 1100 1000 0010 1011 0101 1001 0111 678 |

679 | we get 680 |

681 | f = 0010 0011 0100 1010 1010 1001 1011 1011 682 |

683 | R1 = L0 + f(R0 , K1 )
684 |

685 | = 1100 1100 0000 0000 1100 1100 1111 1111
686 | + 0010 0011 0100 1010 1010 1001 1011 1011
687 | = 1110 1111 0100 1010 0110 0101 0100 0100 688 |
689 |

690 | In the next round, we will have L2 = R1, which is the 691 | block we just calculated, and then we must calculate R2 =L1 + f(R1, K2), and so on for 16 rounds. At the end of the 692 | sixteenth round we have the blocks L16 and R16. We then 693 | reverse the order of the two blocks into the 64-bit block 694 |

695 | R16L16 696 |

697 | and apply a final permutation IP-1 as defined by 698 | the following table: 699 |

700 |

701 |                              IP-1
702 | 
703 |             40     8   48    16    56   24    64   32
704 |             39     7   47    15    55   23    63   31
705 |             38     6   46    14    54   22    62   30
706 |             37     5   45    13    53   21    61   29
707 |             36     4   44    12    52   20    60   28
708 |             35     3   43    11    51   19    59   27
709 |             34     2   42    10    50   18    58   26
710 |             33     1   41     9    49   17    57   25
711 | 
712 |

713 | That is, the output of the algorithm has bit 40 of the 714 | preoutput block as its first bit, bit 8 as its second bit, 715 | and so on, until bit 25 of the preoutput block is the last 716 | bit of the output. 717 |

718 | Example: If we process all 16 blocks using the method 719 | defined previously, we get, on the 16th round, 720 |

721 | L16 = 0100 0011 0100 0010 0011 0010 0011 0100
722 | R16 = 0000 1010 0100 1100 1101 1001 1001 0101 723 |

724 | We reverse the order of these two blocks and apply 725 | the final permutation to 726 |

727 | R16L16 = 00001010 01001100 11011001 10010101 01000011 01000010 00110010 00110100 728 |

729 | IP-1 = 10000101 11101000 00010011 01010100 00001111 00001010 10110100 00000101 730 |

731 | which in hexadecimal format is 732 |

733 | 85E813540F0AB405. 734 |

735 | This is the encrypted form of M = 0123456789ABCDEF: namely, 736 | C = 85E813540F0AB405. 737 |

738 | Decryption is simply the inverse of encryption, 739 | follwing the same steps as above, but reversing the order in 740 | which the subkeys are applied. 741 |

742 |

DES Modes of Operation

743 |

744 | The DES algorithm turns a 64-bit message block M into a 745 | 64-bit cipher block C. If each 64-bit block is encrypted 746 | individually, then the mode of encryption is called 747 | Electronic Code Book (ECB) mode. There are two other modes 748 | of DES encryption, namely Chain Block Coding (CBC) and 749 | Cipher Feedback (CFB), which make each cipher block 750 | dependent on all the previous messages blocks through an 751 | initial XOR operation. 752 |

753 |

Cracking DES

754 |

755 | Before DES was adopted as a national standard, during 756 | the period NBS was soliciting comments on the proposed 757 | algorithm, the creators of public key cryptography, Martin 758 | Hellman and Whitfield Diffie, registered some objections to 759 | the use of DES as an encryption algorithm. Hellman wrote: 760 | "Whit Diffie and I have become concerned that the proposed 761 | data encryption standard, while probably secure against 762 | commercial assault, may be extremely vulnerable to attack by 763 | an intelligence organization" (letter to NBS, October 22, 764 | 1975). 765 |

766 | Diffie and Hellman then outlined a "brute force" attack 767 | on DES. (By "brute force" is meant that you try as many of 768 | the 2^56 possible keys as you have to before decrypting the 769 | ciphertext into a sensible plaintext message.) They 770 | proposed a special purpose "parallel computer using one 771 | million chips to try one million keys each" per second, and 772 | estimated the cost of such a machine at $20 million. 773 |

774 | Fast forward to 1998. Under the direction of John 775 | Gilmore of the EFF, a team spent $220,000 and built a 776 | machine that can go through the entire 56-bit DES key space 777 | in an average of 4.5 days. On July 17, 1998, they announced 778 | they had cracked a 56-bit key in 56 hours. The computer, 779 | called Deep Crack, uses 27 boards each containing 64 chips, 780 | and is capable of testing 90 billion keys a second. 781 |

782 | Despite this, as recently as June 8, 1998, Robert Litt, 783 | principal associate deputy attorney general at the 784 | Department of Justice, denied it was possible for the FBI to 785 | crack DES: "Let me put the technical problem in context: 786 | It took 14,000 Pentium computers working for four months to 787 | decrypt a single message . . . . We are not just talking 788 | FBI and NSA [needing massive computing power], we are 789 | talking about every police department." 790 |

791 | Responded cryptograpy expert Bruce Schneier: " . . . 792 | the FBI is either incompetent or lying, or both." Schneier 793 | went on to say: "The only solution here is to pick an 794 | algorithm with a longer key; there isn't enough silicon in 795 | the galaxy or enough time before the sun burns out to brute- 796 | force triple-DES" (Crypto-Gram, Counterpane Systems, August 797 | 15, 1998). 798 |

799 |

Triple-DES

800 |

801 | Triple-DES is just DES with two 56-bit keys applied. 802 | Given a plaintext message, the first key is used to DES- 803 | encrypt the message. The second key is used to DES-decrypt 804 | the encrypted message. (Since the second key is not the 805 | right key, this decryption just scrambles the data further.) 806 | The twice-scrambled message is then encrypted again with the 807 | first key to yield the final ciphertext. This three-step 808 | procedure is called triple-DES. 809 |

810 | Triple-DES is just DES done three times with two keys 811 | used in a particular order. (Triple-DES can also be done 812 | with three separate keys instead of only two. In either 813 | case the resultant key space is about 2^112.) 814 |

815 | General References 816 |

817 | "Cryptographic Algorithms for Protection of Computer Data 818 | During Transmission and Dormant Storage," Federal Register 819 | 38, No. 93 (May 15, 1973). 820 |

821 | Data Encryption Standard, Federal Information Processing 822 | Standard (FIPS) Publication 46, National Bureau of 823 | Standards, U.S. Department of Commerce, Washington D.C. 824 | (January 1977). 825 |

826 | Carl H. Meyer and Stephen M. Matyas, Cryptography: A New 827 | Dimension in Computer Data Security, John Wiley & Sons, New 828 | York, 1982. 829 |

830 | Dorthy Elizabeth Robling Denning, Cryptography and Data 831 | Security, Addison-Wesley Publishing Company, Reading, 832 | Massachusetts, 1982. 833 |

834 | D.W. Davies and W.L. Price, Security for Computer Networks: 835 | An Introduction to Data Security in Teleprocessing and 836 | Electronics Funds Transfer, Second Edition, John Wiley & 837 | Sons, New York, 1984, 1989. 838 |

839 | Miles E. Smid and Dennis K. Branstad, "The Data Encryption 840 | Standard: Past and Future," in Gustavus J. Simmons, ed., 841 | Contemporary Cryptography: The Science of Information 842 | Integrity, IEEE Press, 1992. 843 |

844 | Douglas R. Stinson, Cryptography: Theory and Practice, CRC 845 | Press, Boca Raton, 1995. 846 |

847 | Bruce Schneier, Applied Cryptography, Second Edition, John 848 | Wiley & Sons, New York, 1996. 849 |

850 | Alfred J. Menezes, Paul C. van Oorschot, and Scott A. 851 | Vanstone, Handbook of Applied Cryptography, CRC Press, Boca 852 | Raton, 1997. 853 |

854 |

-30-
855 |

856 | 857 | This article appeared in Laissez Faire 858 | City Times, Vol 2, No. 28. 859 |

860 | Homepage: http://orlingrabbe.com/
861 | Laissez Faire City Times: http://zolatimes.com/ 862 |

863 | 864 | 865 | 866 | -------------------------------------------------------------------------------- /docs/fips46-3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fffaraz/cppDES/a9f58e3f97825d44ac0d6a999ce4b6495369d0b6/docs/fips46-3.pdf -------------------------------------------------------------------------------- /include/cppdes/des.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | using ui64 = uint64_t; 6 | using ui32 = uint32_t; 7 | using ui8 = uint8_t; 8 | 9 | class DES { 10 | public: 11 | DES(ui64 key); 12 | ui64 des(ui64 block, bool mode); 13 | 14 | ui64 encrypt(ui64 block); 15 | ui64 decrypt(ui64 block); 16 | 17 | static ui64 encrypt(ui64 block, ui64 key); 18 | static ui64 decrypt(ui64 block, ui64 key); 19 | 20 | protected: 21 | void keygen(ui64 key); // Key generation 22 | 23 | ui64 ip(ui64 block); // Initial Permutation 24 | ui64 fp(ui64 block); // Final Permutation 25 | 26 | void feistel(ui32& L, ui32& R, ui32 F); // Feistel function 27 | ui32 f(ui32 R, ui64 k); // F function 28 | 29 | private: 30 | ui64 sub_key[16]; // 48 bits each 31 | }; 32 | -------------------------------------------------------------------------------- /include/cppdes/des3.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cppdes/des.h" 4 | 5 | class DES3 { 6 | public: 7 | DES3(ui64 k1, ui64 k2, ui64 k3); 8 | ui64 encrypt(ui64 block); 9 | ui64 decrypt(ui64 block); 10 | 11 | private: 12 | DES des1; 13 | DES des2; 14 | DES des3; 15 | }; 16 | -------------------------------------------------------------------------------- /include/cppdes/des3cbc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cppdes/des3.h" 4 | 5 | // Triple DES in CBC mode 6 | class DES3CBC { 7 | public: 8 | DES3CBC(ui64 key1, ui64 key2, ui64 key3, ui64 iv); 9 | ui64 encrypt(ui64 block); 10 | ui64 decrypt(ui64 block); 11 | void reset(); 12 | 13 | private: 14 | DES3 des3; 15 | ui64 iv; 16 | ui64 last_block; 17 | }; 18 | -------------------------------------------------------------------------------- /include/cppdes/descbc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cppdes/des.h" 4 | 5 | // DES-CBC 6 | class DESCBC { 7 | public: 8 | DESCBC(ui64 key, ui64 iv); 9 | ui64 encrypt(ui64 block); 10 | ui64 decrypt(ui64 block); 11 | void reset(); 12 | 13 | private: 14 | DES des; 15 | ui64 iv; 16 | ui64 last_block; 17 | }; 18 | -------------------------------------------------------------------------------- /lib/des.cpp: -------------------------------------------------------------------------------- 1 | #include "cppdes/des.h" 2 | 3 | #include "des_key.h" 4 | #include "des_lookup.h" 5 | 6 | #pragma GCC optimize("unroll-loops") 7 | 8 | DES::DES(ui64 key) 9 | { 10 | keygen(key); 11 | } 12 | 13 | ui64 DES::encrypt(ui64 block) 14 | { 15 | return des(block, false); 16 | } 17 | 18 | ui64 DES::decrypt(ui64 block) 19 | { 20 | return des(block, true); 21 | } 22 | 23 | ui64 DES::encrypt(ui64 block, ui64 key) 24 | { 25 | DES des(key); 26 | return des.des(block, false); 27 | } 28 | 29 | ui64 DES::decrypt(ui64 block, ui64 key) 30 | { 31 | DES des(key); 32 | return des.des(block, true); 33 | } 34 | 35 | void DES::keygen(ui64 key) 36 | { 37 | // initial key schedule calculation 38 | ui64 permuted_choice_1 = 0; // 56 bits 39 | for (ui8 i = 0; i < 56; i++) { 40 | permuted_choice_1 <<= 1; 41 | permuted_choice_1 |= (key >> (64 - PC1[i])) & LB64_MASK; 42 | } 43 | 44 | // 28 bits 45 | ui32 C = (ui32)((permuted_choice_1 >> 28) & 0x000000000fffffff); 46 | ui32 D = (ui32)(permuted_choice_1 & 0x000000000fffffff); 47 | 48 | // Calculation of the 16 keys 49 | for (ui8 i = 0; i < 16; i++) { 50 | // key schedule, shifting Ci and Di 51 | for (ui8 j = 0; j < ITERATION_SHIFT[i]; j++) { 52 | C = (0x0fffffff & (C << 1)) | (0x00000001 & (C >> 27)); 53 | D = (0x0fffffff & (D << 1)) | (0x00000001 & (D >> 27)); 54 | } 55 | 56 | ui64 permuted_choice_2 = (((ui64)C) << 28) | (ui64)D; 57 | 58 | sub_key[i] = 0; // 48 bits (2*24) 59 | for (ui8 j = 0; j < 48; j++) { 60 | sub_key[i] <<= 1; 61 | sub_key[i] |= (permuted_choice_2 >> (56 - PC2[j])) & LB64_MASK; 62 | } 63 | } 64 | } 65 | 66 | /*! 67 | * \brief The DES function. 68 | * \param block 64 bit message. 69 | * \param mode false for encryption, true for decryption. 70 | * \return The encrypted/decrypted block. 71 | */ 72 | ui64 DES::des(ui64 block, bool mode) 73 | { 74 | // applying initial permutation 75 | block = ip(block); 76 | 77 | // dividing T' into two 32-bit parts 78 | ui32 L = (ui32)(block >> 32) & L64_MASK; 79 | ui32 R = (ui32)(block & L64_MASK); 80 | 81 | // 16 rounds 82 | for (ui8 i = 0; i < 16; i++) { 83 | ui32 F = mode ? f(R, sub_key[15 - i]) : f(R, sub_key[i]); 84 | feistel(L, R, F); 85 | } 86 | 87 | // swapping the two parts 88 | block = (((ui64)R) << 32) | (ui64)L; 89 | 90 | // applying final permutation 91 | return fp(block); 92 | } 93 | 94 | ui64 DES::ip(ui64 block) 95 | { 96 | // initial permutation 97 | ui64 result = 0; 98 | for (ui8 i = 0; i < 64; i++) { 99 | result <<= 1; 100 | result |= (block >> (64 - IP[i])) & LB64_MASK; 101 | } 102 | return result; 103 | } 104 | 105 | ui64 DES::fp(ui64 block) 106 | { 107 | // inverse initial permutation 108 | ui64 result = 0; 109 | for (ui8 i = 0; i < 64; i++) { 110 | result <<= 1; 111 | result |= (block >> (64 - FP[i])) & LB64_MASK; 112 | } 113 | return result; 114 | } 115 | 116 | void DES::feistel(ui32& L, ui32& R, ui32 F) 117 | { 118 | ui32 temp = R; 119 | R = L ^ F; 120 | L = temp; 121 | } 122 | 123 | // f(R,k) function 124 | ui32 DES::f(ui32 R, ui64 k) 125 | { 126 | // applying expansion permutation and returning 48-bit data 127 | ui64 s_input = 0; 128 | for (ui8 i = 0; i < 48; i++) { 129 | s_input <<= 1; 130 | s_input |= (ui64)((R >> (32 - EXPANSION[i])) & LB32_MASK); 131 | } 132 | 133 | // XORing expanded Ri with Ki, the round key 134 | s_input = s_input ^ k; 135 | 136 | // applying S-Boxes function and returning 32-bit data 137 | ui32 s_output = 0; 138 | for (ui8 i = 0; i < 8; i++) { 139 | // Outer bits 140 | char row = (char)((s_input & (0x0000840000000000 >> 6 * i)) >> (42 - 6 * i)); 141 | row = (row >> 4) | (row & 0x01); 142 | 143 | // Middle 4 bits of input 144 | char column = (char)((s_input & (0x0000780000000000 >> 6 * i)) >> (43 - 6 * i)); 145 | 146 | s_output <<= 4; 147 | s_output |= (ui32)(SBOX[i][16 * row + column] & 0x0f); 148 | } 149 | 150 | // applying the round permutation 151 | ui32 f_result = 0; 152 | for (ui8 i = 0; i < 32; i++) { 153 | f_result <<= 1; 154 | f_result |= (s_output >> (32 - PBOX[i])) & LB32_MASK; 155 | } 156 | 157 | return f_result; 158 | } 159 | -------------------------------------------------------------------------------- /lib/des3.cpp: -------------------------------------------------------------------------------- 1 | #include "cppdes/des3.h" 2 | 3 | DES3::DES3(ui64 k1, ui64 k2, ui64 k3) 4 | : des1(k1) 5 | , des2(k2) 6 | , des3(k3) 7 | { 8 | } 9 | 10 | ui64 DES3::encrypt(ui64 block) 11 | { 12 | return des3.encrypt(des2.decrypt(des1.encrypt(block))); 13 | } 14 | 15 | ui64 DES3::decrypt(ui64 block) 16 | { 17 | return des1.decrypt(des2.encrypt(des3.decrypt(block))); 18 | } 19 | -------------------------------------------------------------------------------- /lib/des3cbc.cpp: -------------------------------------------------------------------------------- 1 | #include "cppdes/des3cbc.h" 2 | 3 | DES3CBC::DES3CBC(ui64 key1, ui64 key2, ui64 key3, ui64 iv) 4 | : des3(key1, key2, key3) 5 | , iv(iv) 6 | , last_block(iv) 7 | { 8 | } 9 | 10 | ui64 DES3CBC::encrypt(ui64 block) 11 | { 12 | last_block = des3.encrypt(block ^ last_block); 13 | return last_block; 14 | } 15 | 16 | ui64 DES3CBC::decrypt(ui64 block) 17 | { 18 | ui64 result = des3.decrypt(block) ^ last_block; 19 | last_block = block; 20 | return result; 21 | } 22 | 23 | void DES3CBC::reset() 24 | { 25 | last_block = iv; 26 | } 27 | -------------------------------------------------------------------------------- /lib/des_key.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // clang-format off 4 | 5 | // Permuted Choice 1 Table [7*8] 6 | static const char PC1[] = { 7 | 57, 49, 41, 33, 25, 17, 9, 8 | 1, 58, 50, 42, 34, 26, 18, 9 | 10, 2, 59, 51, 43, 35, 27, 10 | 19, 11, 3, 60, 52, 44, 36, 11 | 12 | 63, 55, 47, 39, 31, 23, 15, 13 | 7, 62, 54, 46, 38, 30, 22, 14 | 14, 6, 61, 53, 45, 37, 29, 15 | 21, 13, 5, 28, 20, 12, 4 16 | }; 17 | 18 | // Permuted Choice 2 Table [6*8] 19 | static const char PC2[] = { 20 | 14, 17, 11, 24, 1, 5, 21 | 3, 28, 15, 6, 21, 10, 22 | 23, 19, 12, 4, 26, 8, 23 | 16, 7, 27, 20, 13, 2, 24 | 41, 52, 31, 37, 47, 55, 25 | 30, 40, 51, 45, 33, 48, 26 | 44, 49, 39, 56, 34, 53, 27 | 46, 42, 50, 36, 29, 32 28 | }; 29 | 30 | // Iteration Shift Array 31 | static const char ITERATION_SHIFT[] = { 32 | // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 33 | 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 34 | }; 35 | -------------------------------------------------------------------------------- /lib/des_lookup.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // clang-format off 4 | 5 | #define LB32_MASK 0x00000001 6 | #define LB64_MASK 0x0000000000000001 7 | #define L64_MASK 0x00000000ffffffff 8 | #define H64_MASK 0xffffffff00000000 9 | 10 | // Initial Permutation Table [8*8] 11 | static const char IP[] = { 12 | 58, 50, 42, 34, 26, 18, 10, 2, 13 | 60, 52, 44, 36, 28, 20, 12, 4, 14 | 62, 54, 46, 38, 30, 22, 14, 6, 15 | 64, 56, 48, 40, 32, 24, 16, 8, 16 | 57, 49, 41, 33, 25, 17, 9, 1, 17 | 59, 51, 43, 35, 27, 19, 11, 3, 18 | 61, 53, 45, 37, 29, 21, 13, 5, 19 | 63, 55, 47, 39, 31, 23, 15, 7 20 | }; 21 | 22 | // Inverse Initial Permutation Table [8*8] 23 | static const char FP[] = { 24 | 40, 8, 48, 16, 56, 24, 64, 32, 25 | 39, 7, 47, 15, 55, 23, 63, 31, 26 | 38, 6, 46, 14, 54, 22, 62, 30, 27 | 37, 5, 45, 13, 53, 21, 61, 29, 28 | 36, 4, 44, 12, 52, 20, 60, 28, 29 | 35, 3, 43, 11, 51, 19, 59, 27, 30 | 34, 2, 42, 10, 50, 18, 58, 26, 31 | 33, 1, 41, 9, 49, 17, 57, 25 32 | }; 33 | 34 | // Expansion Table [6*8] 35 | static const char EXPANSION[] = { 36 | 32, 1, 2, 3, 4, 5, 37 | 4, 5, 6, 7, 8, 9, 38 | 8, 9, 10, 11, 12, 13, 39 | 12, 13, 14, 15, 16, 17, 40 | 16, 17, 18, 19, 20, 21, 41 | 20, 21, 22, 23, 24, 25, 42 | 24, 25, 26, 27, 28, 29, 43 | 28, 29, 30, 31, 32, 1 44 | }; 45 | 46 | // Post S-Box Permutation [4*8] 47 | static const char PBOX[] = { 48 | 16, 7, 20, 21, 49 | 29, 12, 28, 17, 50 | 1, 15, 23, 26, 51 | 5, 18, 31, 10, 52 | 2, 8, 24, 14, 53 | 32, 27, 3, 9, 54 | 19, 13, 30, 6, 55 | 22, 11, 4, 25 56 | }; 57 | 58 | // The S-Box Tables [8*16*4] 59 | static const char SBOX[8][64] = { 60 | { // S1 61 | 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 62 | 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 63 | 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 64 | 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }, 65 | { // S2 66 | 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 67 | 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 68 | 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 69 | 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 }, 70 | { // S3 71 | 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 72 | 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 73 | 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 74 | 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 }, 75 | { // S4 76 | 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 77 | 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 78 | 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 79 | 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 }, 80 | { // S5 81 | 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 82 | 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 83 | 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 84 | 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 }, 85 | { // S6 86 | 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 87 | 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 88 | 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 89 | 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 }, 90 | { // S7 91 | 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 92 | 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 93 | 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 94 | 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 }, 95 | { // S8 96 | 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 97 | 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 98 | 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 99 | 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } 100 | }; 101 | -------------------------------------------------------------------------------- /lib/descbc.cpp: -------------------------------------------------------------------------------- 1 | #include "cppdes/descbc.h" 2 | 3 | DESCBC::DESCBC(ui64 key, ui64 iv) 4 | : des(key) 5 | , iv(iv) 6 | , last_block(iv) 7 | { 8 | } 9 | 10 | ui64 DESCBC::encrypt(ui64 block) 11 | { 12 | last_block = des.encrypt(block ^ last_block); 13 | return last_block; 14 | } 15 | 16 | ui64 DESCBC::decrypt(ui64 block) 17 | { 18 | ui64 result = des.decrypt(block) ^ last_block; 19 | last_block = block; 20 | return result; 21 | } 22 | 23 | void DESCBC::reset() 24 | { 25 | last_block = iv; 26 | } 27 | -------------------------------------------------------------------------------- /src/fileencryption.cpp: -------------------------------------------------------------------------------- 1 | #include "fileencryption.h" 2 | 3 | #include 4 | 5 | FileEncryption::FileEncryption(ui64 key1, ui64 key2, ui64 key3) 6 | : des(key1, key2, key3, (ui64)0x0000000000000000) 7 | { 8 | } 9 | 10 | int FileEncryption::encrypt(std::string input, std::string output) 11 | { 12 | return cipher(input, output, false); 13 | } 14 | 15 | int FileEncryption::decrypt(std::string input, std::string output) 16 | { 17 | return cipher(input, output, true); 18 | } 19 | 20 | int FileEncryption::cipher(std::string input, std::string output, bool mode) 21 | { 22 | std::ifstream ifile; 23 | std::ofstream ofile; 24 | ui64 buffer; 25 | 26 | if (input.length() < 1) 27 | input = "/dev/stdin"; 28 | if (output.length() < 1) 29 | output = "/dev/stdout"; 30 | 31 | ifile.open(input, std::ios::binary | std::ios::in | std::ios::ate); 32 | ofile.open(output, std::ios::binary | std::ios::out); 33 | 34 | ui64 size = ifile.tellg(); 35 | ifile.seekg(0, std::ios::beg); 36 | 37 | ui64 block = size / 8; 38 | if (mode) 39 | block--; 40 | 41 | for (ui64 i = 0; i < block; i++) { 42 | ifile.read((char*)&buffer, 8); 43 | 44 | if (mode) 45 | buffer = des.decrypt(buffer); 46 | else 47 | buffer = des.encrypt(buffer); 48 | 49 | ofile.write((char*)&buffer, 8); 50 | } 51 | 52 | if (mode == false) { 53 | // Amount of padding needed 54 | ui8 padding = 8 - (size % 8); 55 | 56 | // Padding cannot be 0 (pad full block) 57 | if (padding == 0) 58 | padding = 8; 59 | 60 | // Read remaining part of file 61 | buffer = (ui64)0; 62 | if (padding != 8) 63 | ifile.read((char*)&buffer, 8 - padding); 64 | 65 | // Pad block with a 1 followed by 0s 66 | ui8 shift = padding * 8; 67 | buffer <<= shift; 68 | buffer |= (ui64)0x0000000000000001 << (shift - 1); 69 | 70 | buffer = des.encrypt(buffer); 71 | ofile.write((char*)&buffer, 8); 72 | } else { 73 | // Read last line of file 74 | ifile.read((char*)&buffer, 8); 75 | buffer = des.decrypt(buffer); 76 | 77 | // Amount of padding on file 78 | ui8 padding = 0; 79 | 80 | // Check for and record padding on end 81 | while (!(buffer & 0x00000000000000ff)) { 82 | buffer >>= 8; 83 | padding++; 84 | } 85 | 86 | buffer >>= 8; 87 | padding++; 88 | 89 | if (padding != 8) 90 | ofile.write((char*)&buffer, 8 - padding); 91 | } 92 | 93 | ifile.close(); 94 | ofile.close(); 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /src/fileencryption.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "cppdes/des3cbc.h" 6 | 7 | class FileEncryption { 8 | public: 9 | FileEncryption(ui64 key1, ui64 key2, ui64 key3); 10 | int encrypt(std::string input, std::string output); 11 | int decrypt(std::string input, std::string output); 12 | int cipher(std::string input, std::string output, bool mode); 13 | 14 | private: 15 | DES3CBC des; 16 | }; 17 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "fileencryption.h" 2 | 3 | #include 4 | 5 | void usage() 6 | { 7 | std::cout << "Usage: cppDES -e/-d key1 key2 key3 [input-file] [output-file]" << std::endl; 8 | } 9 | 10 | int main(int argc, char** argv) 11 | { 12 | if (argc < 5) { 13 | usage(); 14 | return 1; 15 | } 16 | 17 | std::string enc_dec = argv[1]; 18 | if (enc_dec != "-e" && enc_dec != "-d") { 19 | usage(); 20 | return 2; 21 | } 22 | 23 | std::string input, output; 24 | if (argc > 5) 25 | input = argv[5]; 26 | if (argc > 6) 27 | output = argv[6]; 28 | 29 | ui64 key1 = strtoull(argv[2], nullptr, 16); 30 | ui64 key2 = strtoull(argv[3], nullptr, 16); 31 | ui64 key3 = strtoull(argv[4], nullptr, 16); 32 | 33 | FileEncryption fe(key1, key2, key3); 34 | 35 | if (enc_dec == "-e") 36 | return fe.encrypt(input, output); 37 | if (enc_dec == "-d") 38 | return fe.decrypt(input, output); 39 | 40 | return 0; 41 | } 42 | --------------------------------------------------------------------------------