【发布时间】:2014-06-17 11:31:23
【问题描述】:
我正在读取图像,对其进行加密,然后对其进行解密。目标是最终循环并记录该过程完成所需的时间。目前我拥有它读取文件,然后对其进行加密,加密,然后根据恢复的数据创建另一个文件。我不需要用解密的图片制作另一个文件。以前我一直在使用StringSource 和StringSink,但这仅适用于文本文件。我在How to read an image to a string for encrypting Crypto++ 获得了一些帮助,并开始使用FileSink 和FileSource。
FileSink、StringSink、FileSource 和 StringSource 之间到底有什么区别?
另外,在下面的例子中,为什么需要设置密码?以前我使用StringSource时,我的字符串密码没有初始化,但现在我使用FileSource,需要初始化它才能工作。
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;
}
【问题讨论】:
-
@jww 我把你的代码读入了文件!不过我有一些问题。
标签: c++ encryption file-io binaryfiles crypto++