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
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
Post a Comment