Random IV from an encrypted file

12 views
Skip to first unread message

Rash

unread,
Aug 27, 2007, 8:23:19 AM8/27/07
to Crypto++ Users
Hello All,
I encrypt a file using AES::CBC mode with a random IV. Something
like this

void encryptFile(const char* password, const char* inputFileName,
const char* outputFileName)
{
byte pass[ AES::BLOCKSIZE ]; // digest of password
byte iv[ AES::BLOCKSIZE ]; // Initial Vector (IV)

AutoSeededRandomPool *rng;
rng = new AutoSeededRandomPool; // random number generator

// digest password
delete new StringSource( password, true,
new HashFilter(*(new SHA256), new ArraySink(pass,
AES::BLOCKSIZE)) );

// random Initial Vector
rng->GenerateBlock(iv, AES::BLOCKSIZE);

// create object for encrypting
AES::Encryption aesEncryption(pass,
CryptoPP::AES::DEFAULT_KEYLENGTH);
CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption,
iv);

StreamTransformationFilter *encryptor;
encryptor = new StreamTransformationFilter(cbcEncryption, new
FileSink(outputFileName) );

// "bind" a file and encrypt one
delete new FileSource(inputFileName, true, encryptor);
}

Now how do I get the same Initialization Vector (IV) to decrypt the
encrypted file or how do I write the decryptFile function:

void decryptFile(const char* password, const char*inputFileName,const
char* outputFileName)
{
//how to get the IV that was used for encrypting the file.
byte pass[ AES::BLOCKSIZE ];
byte iv[ AES::BLOCKSIZE ];

FileSource *source;
try
{
source = new FileSource(inputFileName, false);

delete new StringSource( password, true,
new HashFilter(*(new SHA256), new
ArraySink(pass,AES::BLOCKSIZE)) );

CryptoPP::AES::Decryption aesDecryption(pass,
CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption
cbcDecryption( aesDecryption, iv );

// "bind" decryptor to output file
source->Attach( new StreamTransformationFilter(cbcDecryption,
new FileSink((outputFileName) ) ));

// push the rest data
source->PumpAll();
}
catch(CryptoPP::Exception &e)
{
delete source ;
throw;
}
delete source ;
}

The above decryptFile function doesnt decrypt the first block. I dont
know why?
Please help.

Please help as this is really important to me.
Thanks

Jeffrey Walton

unread,
Aug 27, 2007, 10:12:03 AM8/27/07
to Rash, Crypto++ Users
Hi Rash,

There's lots of sample code out there. Have you looked at any of it?
See http://www.cryptopp.com/fom-serve/cache/1.html and
http://www.cryptopp.com/wiki/.

> delete new FileSource(inputFileName, true, encryptor);

This is interesting to me. What is your motivation for creating this
on the Heap, rather than the Stack?

Jeff

Parch

unread,
Aug 27, 2007, 8:47:06 PM8/27/07
to Crypto++ Users
Another comment on the code, just for C++ style/safety:

source = new FileSource(inputFileName, false);

might throw an exception. At which point the value of source would
still be undefined, and it would be dangerout to call

delete source;

in the catch exception block.

On Aug 28, 12:12 am, "Jeffrey Walton" <noloa...@gmail.com> wrote:
> Hi Rash,
>
> There's lots of sample code out there. Have you looked at any of it?

> Seehttp://www.cryptopp.com/fom-serve/cache/1.htmlandhttp://www.cryptopp.com/wiki/.


>
> > delete new FileSource(inputFileName, true, encryptor);
>
> This is interesting to me. What is your motivation for creating this
> on the Heap, rather than the Stack?
>
> Jeff
>

Parch

unread,
Aug 27, 2007, 9:40:47 PM8/27/07
to Crypto++ Users
As to the actual question...
You are using a random initialization vector every time you encrypt
the file. Can you verify that every time you encrypt the file, the
encrypted output is different?

Mouse

unread,
Aug 27, 2007, 10:20:03 PM8/27/07
to cryptop...@googlegroups.com
Obviously, the main problem is that he doesn't pass the (randomly generated) IV to the decryption program. One (not the optimal) way to pass IV is to use a separate file.

Rash

unread,
Aug 27, 2007, 11:34:42 PM8/27/07
to Crypto++ Users
<COMMENTS>

There's lots of sample code out there. Have you looked at any of it?
Seehttp://www.cryptopp.com/fom-serve/cache/1.htmlandhttp://www.cryptopp.com/wiki/.
<END COMMENTS>

That doesn't solve my problem.
Can you please point to a specific example where we encrypt a file
with a random IV and then get the same IV while
decrypting it.

<COMMENTS>


This is interesting to me. What is your motivation for creating this
on the Heap, rather than the Stack?

<END COMMENTS>
delete new FileSource (inputFileName, true, encryptor);
There is nothing special about this code. I only used this style as
sometimes
the encryption was throwing exception. It occurred only rare. To fix
the problem
I created on FileSource on the heap and deleted immediately.

Rash

unread,
Aug 27, 2007, 11:36:22 PM8/27/07
to Crypto++ Users
Can you explain how that can be possible? According to me "source"
will be initialized once it is inside the try block.

On Aug 28, 5:47 am, Parch <Parchan...@gmail.com> wrote:
> Another comment on the code, just for C++ style/safety:
>
> source = new FileSource(inputFileName, false);
>
> might throw an exception. At which point the value of source would
> still be undefined, and it would be dangerout to call
>
> delete source;
>
> in the catch exception block.
>
> On Aug 28, 12:12 am, "Jeffrey Walton" <noloa...@gmail.com> wrote:
>
> > Hi Rash,
>
> > There's lots of sample code out there. Have you looked at any of it?

> > Seehttp://www.cryptopp.com/fom-serve/cache/1.htmlandhttp://www.cryptopp.....

Rash

unread,
Aug 27, 2007, 11:37:52 PM8/27/07
to Crypto++ Users
As to the actual question...
You are using a random initialization vector every time you encrypt
the file. Can you verify that every time you encrypt the file, the
encrypted output is different?

I know that so was my question to get the IV by which the file was
encrypted.

Rash

unread,
Aug 27, 2007, 11:43:27 PM8/27/07
to Crypto++ Users
One (not the optimal) way to pass IV is to use a separate file.

Can you give an example please?

Thanks

Parch

unread,
Aug 29, 2007, 6:03:13 AM8/29/07
to Crypto++ Users
In C++ constructors aren't meant to return NULL if they fail. They are
meant to throw an exception - and unwind (undo) all the partially
completed actions of construction if possible.

Parch

unread,
Aug 29, 2007, 6:05:44 AM8/29/07
to Crypto++ Users
It's just bytes. How you move them from encryptor to decryptor is up
to you.

Rash

unread,
Aug 30, 2007, 2:44:59 AM8/30/07
to Crypto++ Users
Can you please tell me how?
Reply all
Reply to author
Forward
0 new messages