Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /* Ryan Nichols
  2. * 2/19/17
  3. * CEG4750 - Undergraduate
  4. * Project 1 - CBC Mode DES Decoder
  5. */
  6. #include<iostream>
  7. #include<fstream>
  8. #include<sstream>
  9. #include<string>
  10. #include<cstring>
  11. using namespace std;
  12.  
  13. #include"cryptopp/cryptlib.h"
  14. #include"cryptopp/hex.h"
  15. #include"cryptopp/filters.h"
  16. #include"cryptopp/des.h"
  17. #include"cryptopp/modes.h"
  18.  
  19. using namespace CryptoPP;
  20.  
  21. void des_decryption_8(unsigned char *input, unsigned char *key, unsigned char *output)
  22. {
  23. DESDecryption desDecryptor;
  24. unsigned char xorBlock[8];
  25. memset(xorBlock,0,8);
  26. desDecryptor.SetKey(key,8);
  27. desDecryptor.ProcessAndXorBlock(input,xorBlock,output);
  28. }
  29.  
  30. int main(int argc, char * argv[])
  31. {
  32. //Assign init vector
  33. unsigned char iv[DES::BLOCKSIZE] = {0x4c, 0xa0, 0x0f, 0xd6, 0xdb, 0xf1, 0xfb, 0x28}; //Test case 1
  34. //unsigned char iv[DES::BLOCKSIZE] = {0x0f, 0xf4, 0xc8, 0xd6, 0x1e, 0x80, 0x06, 0x18}; //Test case 2
  35.  
  36. //Files
  37. fstream pFile; //Output plaintext file
  38. fstream cFile; //Input ciphertext file
  39. fstream kFile; //Input key file
  40.  
  41. //Incorrect commandline usage
  42. if(argc!=4)
  43. {
  44. cout<<"usage:cbc_des_encode infile outfile keyfile"<<endl;
  45. }
  46.  
  47. //Open files
  48. cFile.open(argv[1],ios::in);
  49. pFile.open(argv[2],ios::out);
  50. kFile.open(argv[3],ios::in);
  51.  
  52. //Read ciphertext
  53. stringstream buffer;
  54. buffer << cFile.rdbuf();
  55. string cString(buffer.str());
  56. cout<< "Ciphertext: " << cString << endl;
  57.  
  58. //Read Key
  59. stringstream buffer2;
  60. buffer2 << kFile.rdbuf();
  61. string kString(buffer2.str());
  62. cout << "Key: " << kString;
  63. unsigned char key[DES::DEFAULT_KEYLENGTH];
  64. for (int i = 0; i < DES::DEFAULT_KEYLENGTH; i++) {
  65. key[i] = kString.at(i);
  66. }
  67.  
  68. //Get block count
  69. int numblocks = (cString.size() / DES::BLOCKSIZE);
  70. cout << "number of blocks: " << numblocks << endl;
  71.  
  72.  
  73.  
  74. unsigned char cipherblock[numblocks][DES::BLOCKSIZE];
  75. unsigned char plainblock [numblocks][DES::BLOCKSIZE];
  76. unsigned char plaintext [numblocks * DES::BLOCKSIZE];
  77. unsigned char thisblock[DES::BLOCKSIZE];
  78. for (int block = numblocks - 1; block >= 0; block--) {
  79. for (int i = block * 8, j = 0; j < 8; i++, j++) {
  80. cipherblock[block][j] = (unsigned char)cString.at(i); //Get a block
  81. }
  82. unsigned char tempblock[DES::BLOCKSIZE];
  83. for(int i = 0; i < 8; i++)
  84. {
  85. tempblock[i] = cipherblock[block][i]; //Fill a temporary array which is passed to the decoding function
  86. }
  87. des_decryption_8(tempblock, key, thisblock); //decrpyt block, plaintext written to this block. Still need xored
  88.  
  89. for(int i = 0; i < 8; i++) {
  90. plainblock[block][i] = thisblock[i]; //Save decrypted blocks to xor them after
  91. }
  92.  
  93. }
  94. for(int i = numblocks-1; i > 0; i--) { //For all but the first block
  95. for (int j = 0; j < 8; j++){
  96. plainblock[i][j] ^= cipherblock[i-1][j]; //xor with previous ciphertext for CBC implementation
  97. }
  98. }
  99. for (int j = 0; j < 8; j++){
  100. plainblock[0][j] ^= iv[j]; //xor first block with the IV
  101. }
  102.  
  103. for (int block = 0; block < numblocks; block++) {
  104. for(int i = block *8, j =0; j<8; i++, j++)
  105. {
  106. plaintext[i]=plainblock[block][j]; //Recombine the plaintext
  107. }
  108. }
  109.  
  110. //Remove padding
  111. int padAmount = plaintext[numblocks * DES::BLOCKSIZE -1];
  112.  
  113. cout << "Plaintext: ";
  114. for (int i =0; i < (numblocks * DES::BLOCKSIZE) - padAmount; i++) {
  115. pFile<< plaintext [i]; //write plaintext to file
  116. cout << plaintext[i]; //write plaintext to console
  117. }
  118. cout <<endl;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement