I am reading in an image, encrypting it, then decrypting it. The goal is to be looping this eventually and recording the time it takes for the process to complete. Currently what I have it reads the file in, then it encrypts it, encrypts it, the creates another file based on the recovered data. I don't need to make another file with the decrypted picture. Previously I had been using StringSource
and StringSink
, but that only worked for text files. I received some help at How to read an image to a string for encrypting Crypto++ from and started using FileSink
and FileSource
.
What exactly is the difference between FileSink
, StringSink
, FileSource
and StringSource
?
Also, in following example, why does cipher need to be set to something? Previously when I was just using StringSource
, my string cipher was not initialized, but now that I am using FileSource
, it needs to be initialized for it to work.
int main(int argc, char* argv[])
{
AutoSeededRandomPool prng_blowfish;
SecByteBlock key_blowfish(Blowfish::DEFAULT_KEYLENGTH);
prng_blowfish.GenerateBlock(key_blowfish, key_blowfish.size());
byte iv_blowfish[Blowfish::BLOCKSIZE];
prng_blowfish.GenerateBlock(iv_blowfish, sizeof(iv_blowfish));
string ifilename = "sample_files/1MB.jpg";
string cipher = "1MB.enc";
string rfilename = "r1MB.jpg";
try
{
EAX<Blowfish>::Encryption e_blowfish;
e_blowfish.SetKeyWithIV(key_blowfish, key_blowfish.size(), iv_blowfish, sizeof(iv_blowfish));
std::ifstream ifile(ifilename.c_str(), ios::binary);
std::ifstream::pos_type size = ifile.seekg(0, std::ios_base::end).tellg();
ifile.seekg(0, std::ios_base::beg);
FileSource fs1(ifilename.c_str(), true, new AuthenticatedEncryptionFilter(e_blowfish, new FileSink(cipher.c_str())));
EAX<Blowfish>::Decryption d_blowfish;
d_blowfish.SetKeyWithIV(key_blowfish, key_blowfish.size(), iv_blowfish, sizeof(iv_blowfish));
FileSource fs2(cipher.c_str(), true, new AuthenticatedDecryptionFilter(d_blowfish, new FileSink(rfilename.c_str()), AuthenticatedDecryptionFilter::THROW_EXCEPTION));
}
catch (const Exception& ex)
{
cerr << ex.what() << endl;
}
return 0;
}