c++ - How to loop over Blowfish Crypto++ -


i running crypto++ doing speed tests on encryption algorithms. trying time how long takes encrypt, decrypt data(eventually more file sizes , different algorithms). running problem cannot loop on code. in following code, using blowfish, when encryption part, gives me error:

hashverificationfilter: message hash or mac not valid 

what can fix this? need put in function? if so, how that?

/**  * g++ encryption_tests.cpp -o encryption_tests -lcryptopp -lpthread -l.  */ #include <iostream> #include <string> #include <iomanip> #include <fstream> #include <ctime>   #include "cryptoplusplus/osrng.h" using cryptopp::autoseededrandompool;  #include "cryptoplusplus/cryptlib.h" using cryptopp::exception;  #include "cryptoplusplus/hex.h" using cryptopp::hexencoder; using cryptopp::hexdecoder;  #include "cryptoplusplus/modes.h" #include "cryptoplusplus/aes.h"  #include "cryptoplusplus/filters.h" using cryptopp::stringsink; using cryptopp::stringsource; using cryptopp::authenticatedencryptionfilter; using cryptopp::authenticateddecryptionfilter; using namespace std;  #include "cryptoplusplus/filters.h" using cryptopp::stringsink; using cryptopp::stringsource; using cryptopp::authenticatedencryptionfilter; using cryptopp::authenticateddecryptionfilter;  #include "cryptoplusplus/blowfish.h" using cryptopp::blowfish;  #include "crypto++/eax.h" using cryptopp::eax;  #include "cryptoplusplus/secblock.h" using cryptopp::secbyteblock;  int main( int argc, char* argv[] ) {     // declaring variables     const int number_of_runs = 3;     const int number_of_txt_files = 9;     const int number_of_jpg_files = 6;     const int number_of_png_files = 6;     const int number_of_avi_files = 2;      string file_names_txt[number_of_txt_files] = { "10b.txt", "100b.txt", "1kb.txt", "10kb.txt", "100kb.txt", "1mb.txt", "5mb.txt", "10mb.txt", "20mb.txt" };     string file_names_jpg[number_of_jpg_files] = { "1kb.jpg", "10kb.jpg", "100kb.jpg", "1mb.jpg", "3mb.jpg", "5mb.jpg" };     string file_names_png[number_of_png_files] = { "100b.png", "500b.png","1kb.png", "10kb.png","1mb.png", "5mb.png" };     string file_names_avi[number_of_avi_files] = { "4mb.avi", "10mb.avi" };      int time_data [number_of_runs];     string plaintext, cipher, encoded, recovered, sample_files_path, data_file, line_contents;      string initial_cpp_time_data = "";     clock_t time_start, time_stop;     double run_time, time_difference, time_average = 0;      // loop run test number_of_runs times     ( int = 0 ; < number_of_runs ; i++ ) {         time_start = clock();      // class seeds using operating system provided rng     autoseededrandompool prng;     // generate random key     secbyteblock key(blowfish::default_keylength);     prng.generateblock(key, key.size());     // generate random initialization vector     byte iv[blowfish::blocksize];     prng.generateblock(iv, sizeof(iv));     // set key width     eax< blowfish >::encryption e;     e.setkeywithiv(key, key.size(), iv);      // grab data file want run test on     sample_files_path = "sample_files/" + file_names_txt[8];     ifstream initial_file_contents ( sample_files_path.c_str() );     if (initial_file_contents.is_open()) {         while ( getline( initial_file_contents, line_contents ) ) {             plaintext = plaintext + line_contents;             plaintext.push_back('\n');             initial_file_contents.close();         }     } else {         cout << "unable open file" << endl;     }      // encrypts plaintext     try {         stringsource(plaintext, true, new authenticatedencryptionfilter(e, new stringsink(cipher) ) );      } catch ( const cryptopp::exception& e ) {         cerr << e.what() << endl;         exit(1);     }           // decrypts test         try {             eax< blowfish >::decryption d;             d.setkeywithiv(key, key.size(), iv);             stringsource s(cipher, true, new authenticateddecryptionfilter( d, new stringsink(recovered) ) );         } catch ( const cryptopp::exception& e ) {             cerr << e.what() << endl;             exit(1);         }          // stop clock, calculate time difference, turn milliseconds         time_stop = clock();         time_difference = time_stop - time_start;         run_time = time_difference / ( clocks_per_sec / 1000 );         time_data[i] = run_time;         cout << "time_data[" << << "]: " << time_data[i] << " milliseconds" << endl;     }      //grab data old file     ifstream initial_cpp_time_data_file ( "cpp_time_data.txt" );     if (initial_cpp_time_data_file.is_open()) {         while ( getline( initial_cpp_time_data_file, line_contents ) ) {             initial_cpp_time_data = initial_cpp_time_data + line_contents;             initial_cpp_time_data.push_back('\n');         }             initial_cpp_time_data_file.close();     } else {         initial_cpp_time_data = "";     }      // created new file     ofstream time_data_file;     time_data_file.open("cpp_time_data.txt");      // insert old data first     time_data_file << initial_cpp_time_data << endl;      // show file test ran on , insert new data     time_data_file << sample_files_path << endl;     ( int = 0 ; < number_of_runs ; i++ ) {         time_data_file << "time_data[" << << "]: " << time_data[i] << " milliseconds" << endl;         time_average = time_average + time_data[i];     }     time_average = time_average / number_of_runs;     time_data_file << "the average time " << time_average << " milliseconds" << endl;     cout << "the average time " << time_average << " milliseconds" << endl;     time_data_file.close();     cout << "done!\n";      return 0; } 

at each iteration of loop, have call:

  • cipher.clear()
  • recovered.clear()

otherwise, stringsink's keep adding end of previous value. fail on 2nd , subsequent iterations of loop (the 1st should ok).

also, there no resynchronize, can't call e.resynchronize(iv) restart cipher. have call e.setkeywithiv(key, key.size(), iv) @ each iteration of loop.

below, not duplicate reuse problem. encryption object reused, while decryption object created new each iteration. result of running program:

$ ./cryptopp-test.exe plain text: string 1 recovered text: string 1 plain text: string 2 recovered text: string 2 plain text: string 3 recovered text: string 3 plain text: string 4 recovered text: string 4 plain text: string 5 recovered text: string 5 

autoseededrandompool prng;  secbyteblock key(blowfish::default_keylength); prng.generateblock( key, key.size() );  byte iv[ blowfish::blocksize ]; prng.generateblock( iv, sizeof(iv) );  vector<string> vv; vv.push_back("string 1"); vv.push_back("string 2"); vv.push_back("string 3"); vv.push_back("string 4"); vv.push_back("string 5");  string plain, cipher, recovered;  try {      eax< blowfish >::encryption e1;     e1.setkeywithiv( key, key.size(), iv, sizeof(iv) );      for(unsigned = 0; < vv.size(); i++)     {         /*********************************\         \*********************************/          plain = vv[i];         cout << "plain text: " << plain << endl;          e1.setkeywithiv( key, key.size(), iv, sizeof(iv) );          cipher.clear();         stringsource ss1(plain, true,                          new authenticatedencryptionfilter( e1,                              new stringsink( cipher )                          )  ); // stringsource          /*********************************\         \*********************************/          eax< blowfish >::decryption d2;         d2.setkeywithiv( key, key.size(), iv, sizeof(iv) );          recovered.clear();         stringsource ss2(cipher, true,                          new authenticateddecryptionfilter( d2,                              new stringsink( recovered ),                              authenticateddecryptionfilter::throw_exception                          ) ); // stringsource          cout << "recovered text: " << recovered << endl;     }  } catch (const exception& ex) {     cerr << ex.what() << endl; } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -