Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C++
#include "dll.h" 
#include "modes.h"
#include "aes.h"
#include "filters.h"
#include "iostream"
#include "cryptlib.h"


int main(int argc, char* argv[]) {


    //
    // Key and IV setup
    //
	
    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
    memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
    memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );


    //
    // String and Sink setup
    //
    std::string plaintext = "Now is the time for all good men to come to the aide...";
    string ciphertext;
    std::string decryptedtext;

    //
    // Dump Plain Text
    //
    std::cout << "Plain Text (" << plaintext.size() << " bytes)" <<    std::endl;
    std::cout << plaintext;
    std::cout << std::endl << std::endl;

    //
    // Create Cipher Text
    //
	
	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();


	

	
    //
    // Dump Cipher Text
    //
    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;

    //
    // Decrypt
    //
    
	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();

    //
    // Dump Decrypted Text
    //
	
    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
C++
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.
Posted
Updated 22-Feb-20 21:45pm
v2
Comments
nv3 20-Aug-13 16:30pm    
I assume you are not having trouble compiling it, because the error you mention is a runtime error that suggests that the heap is corrupted. So you seem to have trouble running it.

Is the code you are showing all the code, or just the part that you assumed to be relevant? On first sight all looks good to me.
Morbiddeath 20-Aug-13 18:47pm    
This is all of the code, sorry I don't know why I used the term compile but it compiles fine, the error pops on run time.
André Kraak 20-Aug-13 16:34pm    
Why are you using "plaintext.length() + 1" to specify the length of the message to be encrypted?
Try "plaintext.length()" instead and see what happens.
Morbiddeath 20-Aug-13 18:49pm    
I assume +1 is for the null character, but I had no luck after removing the +1. Still the same heap error.
Richard MacCutchan 21-Aug-13 3:14am    
Why not ask at the place that you copied the code from?

1 solution

I found the solution myself; while compiling the source code of CryptoPP, in the solution properties(cryptdll), under C/C++ menu there is a sub-menu called Code Generation, the field Runtime Library should be Multi-threaded Debug DLL (/MDd). Thanks for your time guys. Farewell.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900