Encrypt and Decrypt text file conatins using AES::CBC mode

1,147 views
Skip to first unread message

Alok Sethi

unread,
Oct 31, 2013, 10:03:27 AM10/31/13
to cryptop...@googlegroups.com
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();
}

Fraser Hutchison

unread,
Oct 31, 2013, 3:53:32 PM10/31/13
to Alok Sethi, cryptop...@googlegroups.com
Hi there,

The cause of this error is where you have key[sizeof(key)] = 0; and iVector[sizeof(iVector)] = 0; in each of your functions.  You're trying to access an element one past the end of each array.

I guess you're trying to null-terminate these arrays, but there's no need here.  In fact, it's valid for either the key or IV to contain '0' char(s) anywhere within.

Cheers,
Fraser.
--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to cryptopp-user...@googlegroups.com.
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 cryptopp-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Alok Sethi

unread,
Nov 1, 2013, 6:38:39 AM11/1/13
to cryptop...@googlegroups.com, Alok Sethi
Hi Fraser,

Thanks for your quick reply. As per your suggestion I tried by removing these two lines. But getting the same error message.
Also I tried to set the memory to zero as below.

memset( key, 0x01, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iVector, 0x01, CryptoPP::AES::BLOCKSIZE );

It would be great if you could suggest any other alternative solutions.


Thanks,
Alok.

David Irvine,,,

unread,
Nov 1, 2013, 7:08:26 AM11/1/13
to Alok Sethi, cryptop...@googlegroups.com
Alok Sethi writes:

> « HTML content follows »
>
> Hi Fraser,
>
> Thanks for your quick reply. As per your suggestion I tried by removing these
> two lines. But getting the same error message.
> Also I tried to set the memory to zero as below.
>
> memset( key, 0x01, CryptoPP::AES::DEFAULT_KEYLENGTH );
> memset( iVector, 0x01, CryptoPP::AES::BLOCKSIZE );
>
> It would be great if you could suggest any other alternative solutions.
Apart from these stack errors, what are you trying to do, it seems you are
setting key and iv to zero, is this intentional. Should you not take a key
and iv in through your parameters and set these to the size of the constants
you have to create the key ivector. If you did want to create a key and iv
of zero then set each array member to zero, but it would be quite strange.

Apart from that you are setting one past the end of the byte array, the end
of an array size 64 is element 63.
> > To unsubscribe, send an email to <URL:javascript:>cryptopp-
> > user...@googlegroups.com.
> > More information about Crypto++ and this group is available at
> > <URL:http://www.cryptopp.com>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 <URL:javascript:>cryptopp-user...@googlegroups.com.
> > For more options, visit
> > <URL:https://groups.google.com/groups/opt_out>https://groups.google.com/g
> > roups/opt_out.
>
>
> --
> --
> You received this message because you are subscribed to the "Crypto++ Users"
> Google Group.
> To unsubscribe, send an email to cryptopp-user...@googlegroups.com.
> More information about Crypto++ and this group is available at
> <URL:http://www.cryptopp.com>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 cryptopp-user...@googlegroups.com.
> For more options, visit
> <URL:https://groups.google.com/groups/opt_out>https://groups.google.com/group
> s/opt_out.
>

Da Co

unread,
Nov 1, 2013, 10:51:17 AM11/1/13
to David Irvine, Alok Sethi, Crypto++ Users
I was not able to encrypt with these parameters set to zero. You need 16 meaningful bytes in key and IV.


To unsubscribe, send an email to cryptopp-users-unsubscribe@googlegroups.com.
More information about Crypto++ and this group is available at <URL:http://www.cryptopp.com>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 cryptopp-users+unsubscribe@googlegroups.com.
--
--
You received this message because you are subscribed to the "Crypto++ Users" Google Group.
To unsubscribe, send an email to cryptopp-users-unsubscribe@googlegroups.com.
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 cryptopp-users+unsubscribe@googlegroups.com.

David Irvine,,,

unread,
Nov 1, 2013, 10:52:42 AM11/1/13
to Da Co, David Irvine, Alok Sethi, Crypto++ Users
Da Co writes:

> « HTML content follows »
>
> I was not able to encrypt with these parameters set to zero. You need 16
> meaningful bytes in key and IV.
Excellent, I hope you are on the way now. I think oyu were makig use of
undefined behaviour, so setting al bytes will fix that.
> <URL:mailto:user...@googlegroups.com>user...@googlegroups.com.
>   > More information about Crypto++ and this group is available at   >
> <URL:<URL:http://www.cryptopp.com>http://www.cryptopp.com><URL:http://ww
> w.cryptopp.com>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 <URL:javascript:><URL:mailto:cryptopp-
> user...@googlegroups.com>cryptopp-user...@googlegroups.com.
>   > For more options, visit   >
> <URL:<URL:https://groups.google.com/groups/opt_out>https://groups.google
> .com/groups/opt_out><URL:https://groups.google.com/g>https://groups.goog
> le.com/g  > roups/opt_out.
>
>
>
> --
> --
> You received this message because you are subscribed to the "Crypto++
> Users" Google Group.
> To unsubscribe, send an email to <URL:mailto:cryptopp-users-
> unsub...@googlegroups.com>cryptopp-users-
> unsub...@googlegroups.com.
> More information about Crypto++ and this group is available at
> <URL:<URL:http://www.cryptopp.com>http://www.cryptopp.com><URL:http://ww
> w.cryptopp.com>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 <URL:mailto:cryptopp-users
> %2Bunsu...@googlegroups.com>cryptopp-
> users+un...@googlegroups.com.
> For more options, visit
> <URL:<URL:https://groups.google.com/groups/opt_out>https://groups.google
> .com/groups/opt_out><URL:https://groups.google.com/groups/opt_out>https:
> //groups.google.com/groups/opt_out.
>
>
>
> --
> --
> You received this message because you are subscribed to the "Crypto++
> Users" Google Group.
> To unsubscribe, send an email to <URL:mailto:cryptopp-users-
> unsub...@googlegroups.com>cryptopp-user...@googlegroups.com.
> More information about Crypto++ and this group is available at
> <URL:http://www.cryptopp.com>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 <URL:mailto:cryptopp-users
> %2Bunsu...@googlegroups.com>cryptopp-
> users+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages