Hi, I have copied this piece of code from another site and have trouble compiling it; I'm new to cryptography and whilst I tried my best to get this to work, i keep getting "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" I know this error has to do with the heap but I didn't perform any Delete in these few lines of code.
#include "dll.h"
#include "modes.h"
#include "aes.h"
#include "filters.h"
#include "iostream"
#include "cryptlib.h"
int main(int argc, char* argv[]) {
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
std::string plaintext = "Now is the time for all good men to come to the aide...";
string ciphertext;
std::string decryptedtext;
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( unsigned int i = 0; i < ciphertext.size(); i++ ) {
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout << std::endl << std::endl;
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
CryptoPP::StreamTransformationFilter * stfDecryptor=new CryptoPP::StreamTransformationFilter(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor->Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor->MessageEnd();
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
return 0;
}
While it is worth mentioning if I comment these two lines I don't get the heap error anymore
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
I did try to debug it, and the error pops right before the last return 0, I think while the class Destructor is being called the de-allocation is causing this issue?
Any help would be greatly appreciated.