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
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
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
>
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.
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.....
I know that so was my question to get the IV by which the file was
encrypted.
Can you give an example please?
Thanks