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.

On Aug 28, 1:36 pm, Rash <[EMAIL PROTECTED]> wrote:
> 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 <[EMAIL PROTECTED]> 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" <[EMAIL PROTECTED]> 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.....
>
> > > >    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
>
> > > On 8/27/07, Rash <[EMAIL PROTECTED]> wrote:
>
> > > > 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


--~--~---------~--~----~------------~-------~--~----~
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.
-~----------~----~----~----~------~----~------~--~---

Reply via email to