Hi,

I want to encrypt and decrypt file contains using AES::CBC mode. I tried 
the following for encryption and decryption.
It works fine but I get the below two exceptions.

1. Run-Time Check Failure #2 - Stack around the variable 'iVector' was 
corrupted.
2. Run-Time Check Failure #2 - Stack around the variable 'key' was 
corrupted.

I don't know exactly where I am doing wrong. Please help me to fix this 
problem.

To encrypt the file here is my code :-
==================================
void Encrypt(string inFileName, string outFileName)
{

    byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
    key[sizeof(key)] = 0;

    byte iVector[CryptoPP::AES::BLOCKSIZE];
    iVector[sizeof(iVector)] = 0;
    
    ifstream in(inFileName.c_str(), ios::binary);

    const string orgStrObj((istreambuf_iterator<char>(in)), 
istreambuf_iterator<char>());
    in.close();
    
    CryptoPP::AES::Encryption cipher(key, sizeof(key));
    
    CryptoPP::CBC_Mode_ExternalCipher::Encryption encryption(cipher, 
iVector);
    CryptoPP::StreamTransformationFilter strFilter(encryption, new 
CryptoPP::StringSink(encryptStr));
    strFilter.Put(reinterpret_cast<const byte*>(orgStrObj.c_str()), 
orgStrObj.size());
    strFilter.MessageEnd();
    
     ofstream out(outFileName.c_str(), ios::binary);
    out.write(encryptStr.c_str(), encryptStr.size());
    out.close();
}


To decrypt the file here is my code :-
================================
void Crypto::Decrypt(string inFileName, string outFileName)
{
    byte key[CryptoPP::AES::DEFAULT_KEYLENGTH];
    key[sizeof(key)] = 0;

    byte iVector[CryptoPP::AES::BLOCKSIZE];
    iVector[sizeof(iVector)] = 0;
    
    ifstream in(inFileName.c_str(), ios::binary);

    const string encryptStrObj((istreambuf_iterator<char>(in)), 
istreambuf_iterator<char>());
    in.close();
  
    CryptoPP::AES::Decryption cipher(key, sizeof(key));

    CryptoPP::CBC_Mode_ExternalCipher::Decryption decryption(cipher, 
iVector);
    CryptoPP::StreamTransformationFilter strFilter(decryption, new 
CryptoPP::StringSink(decryptStr));
    strFilter.Put(reinterpret_cast<const byte*>(encryptStrObj.c_str()), 
encryptStrObj.size());
    strFilter.MessageEnd();
    
    ofstream out(outFileName.c_str(), ios::binary);
    out.write(decryptStr.c_str(), decryptStr.size());
    out.close();
}

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to