Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<sstream>
  4. #include<string>
  5. #include<cstring>
  6. using namespace std;
  7.  
  8. #include"cryptopp/cryptlib.h"
  9. #include"cryptopp/hex.h"
  10. #include"cryptopp/filters.h"
  11. #include"cryptopp/des.h"
  12. #include"cryptopp/modes.h"
  13.  
  14. using namespace CryptoPP;
  15.  
  16. void des_encryption_8(unsigned char *input, unsigned char *key, unsigned char *output)
  17. {
  18. DESEncryption desEncryptor;
  19. unsigned char xorBlock[8];
  20. memset(xorBlock,0,8);
  21. desEncryptor.SetKey(key,8);
  22. desEncryptor.ProcessAndXorBlock(input,xorBlock,output);
  23. }
  24.  
  25. int main(int argc, char * argv[])
  26. {
  27. //Files
  28. fstream pFile; //Input plaintext file
  29. fstream cFile; //Output ciphertext file
  30. fstream kFile; //Input key file
  31.  
  32. //Incorrect commandline usage
  33. if(argc!=4)
  34. {
  35. cout<<"usage:cbc_des_encode infile outfile keyfile"<<endl;
  36. }
  37.  
  38. //Open files
  39. pFile.open(argv[1],ios::in);
  40. cFile.open(argv[2],ios::out);
  41. kFile.open(argv[3],ios::in);
  42.  
  43. //Read plaintext
  44. stringstream buffer;
  45. buffer << pFile.rdbuf();
  46. string pString(buffer.str());
  47. cout<< "Plaintext: " << pString << endl;
  48. //unsigned char *plain[pString.size + 1];
  49. //strcpy((char*)plain, pString.c_str()); //Convert to unsigned char
  50.  
  51.  
  52. //Read Key
  53. stringstream buffer2;
  54. buffer2 << kFile.rdbuf();
  55. string kString(buffer2.str());
  56. cout << "Key: " << kString << endl;
  57. unsigned char *key[DES::DEFAULT_KEYLENGTH];
  58. strcpy((char*)key, kString.c_str()); //Convert to unsigned char
  59.  
  60. //Assign init vector
  61. unsigned char iv[DES::BLOCKSIZE] = {0x4c, 0xa0, 0x0f, 0xd6, 0xdb, 0xf1, 0xfb, 0x28}; //Test case 1
  62. //{0x0f, 0xf4, 0xc8, 0xd6, 0x1e, 0x80, 0x06, 0x18}; //Test case 2
  63.  
  64. //Encrypt
  65. int numblocks = (pString.size() / DES::BLOCKSIZE);
  66. //Pad
  67. if (pString.size() % DES::BLOCKSIZE != 0) {
  68. numblocks++; //Add an extra block of the message is not a multiple of 8 byts
  69. int difference = (numblocks * DES::BLOCKSIZE) - pString.size();
  70. for (int i = 0; i < difference; i++) {
  71. pString += difference;
  72. cout << difference << endl;
  73. }
  74. }
  75. cout << "number of block: " << numblocks << endl;
  76. cout << "padded plaintext: " << pString << endl;
  77.  
  78. unsigned char *plainblock[DES::BLOCKSIZE];
  79. for (int block = 0; block < numblocks; block++) {
  80. for (int i = block * 8; i < block * 8; i++) {
  81.  
  82. }
  83.  
  84. }
  85.  
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement