c++ - 'message hash or MAC not valid' exception after decryption -


i'm trying make program encrypts files (.jpg , .avi) using crypto++ libraries. aim make program encrypts video files using aes-256.

i did text examples of aes encryption here , ran (meaning library setup correctly). however, following simple code produces exception

hashverificationfilter: message hash or mac not valid 

code:

autoseededrandompool prng;  secbyteblock key(aes::default_keylength); prng.generateblock(key, key.size());  secbyteblock iv(aes::blocksize); prng.generateblock(iv, iv.size());  string ofilename = "testimage.png"; string efilename; string rfilename = "testimagerecovered.png";  try {      gcm< aes >::encryption e;     e.setkeywithiv(key, key.size(), iv, iv.size());      ifstream ofile(ofilename.c_str(), ios::binary);     ofile.seekg(0, ios_base::beg);      filesource fs1(ofilename.c_str(), true,             new authenticatedencryptionfilter(e,                     new stringsink(efilename)));      gcm< aes >::decryption d2;     d2.setkeywithiv(key, key.size(), iv, sizeof(iv));      stringsource fs2(efilename, true,             new authenticateddecryptionfilter( d2,                     new filesink (rfilename.c_str()),                     authenticateddecryptionfilter::throw_exception)); } catch(const exception &e) {     cerr << e.what() << endl;     exit(1); }  return 0; 

i suspect not implementing aes algorithm correctly. however, unable find solution last 2 days. i'm using eclipse luna on ubuntu 14.04.

ps have gone through following answers

how read image string encrypting crypto++

how loop on blowfish crypto++

please use iv.size() rather sizeof(iv) when try set d2.setkeywithiv, have done e.setkeywithiv. because in program, value of iv.size() 16, sizeof(iv) 24. work.

gcm< aes >::decryption d2; d2.setkeywithiv(key, key.size(), iv, iv.size()); //here misuse of sizeof(iv)  stringsource fs2(efilename, true,         new authenticateddecryptionfilter( d2,                 new filesink (rfilename.c_str()),                 authenticateddecryptionfilter::throw_exception)); 

the code has passed test above.


Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -