我使用Crypto ++来解密文件,因此我使用FileSource
作为我的来源,但我希望能够更改接收器,因此我可以实现以下内容:
std::string temp;
FileSource file("/path/to/file", false, new StringSink(temp));
file.Pump(14);
if (temp != "File Signature")
return false;
//change file's sink to new CTR_Mode<AES>::Decryption(meta_key, 32, meta_iv, new StringSink(metainfo))
file.Pump(256);
/* use metainfo */
//change file's sink to new CTR_Mode<AES>::Decryption(key, 32, iv, new StringSink(decoded))
while(!file.SourceExhausted())
{
file.Pump(512);
std::cout << decoded;
}
我怎样才能做到这一点?
答案 0 :(得分:1)
如何在Crypto ++中更改接收器?
Sink只是一个没有附加转换的过滤器。要更改接收器,只需更改前一个或父对象的附加过滤器。棘手的部分是访问过滤器链中两个或三个深度的过滤器。
使用以下内容。过滤器有两种附加过滤器的方法:Attach
和Detach
。它们都为对象附加了一个新的过滤器;但Attach
会在Detach
免费使用时返回旧过滤器。
另一个奇怪的是Redirector
。您可以使用它来破坏链中的所有权。它是StreamTransformationFilter filter
所需要的。基于堆栈的分配将作为局部变量免费提供,因此您不希望它作为链的一部分被释放。
FileSource file("/path/to/file", false, new StringSink(temp));
file.Pump(14);
if (temp != "File Signature")
return false;
CTR_Mode<AES>::Decryption decryptor;
StreamTransformationFilter filter(decryptor);
// Detach StringSink(temp), Attach StreamTransformationFilter(decryptor)
file.Detach(new Redirector(filter));
// Set Key and IV
decryptor.SetKeyWithIV(meta_key, 32, meta_iv);
// Detach nothing, Attach StringSink(metainfo)
filter.Detach(new StringSink(metainfo));
// FileSource → decryptor → metainfo
file.Pump(256);
// Set Key and IV
decryptor.SetKeyWithIV(key, 32, iv);
// Detach StringSink(metainfo), Attach StringSink(decoded)
filter.Detach(new StringSink(decoded));
while(!file.SourceExhausted())
{
// FileSource → decryptor → decoded
file.Pump(512);
std::cout << decoded;
}
这是另一种没有Redirector
的方法。它会隐藏指向StreamTransformationFilter
:
FileSource file("/path/to/file", false, new StringSink(temp));
file.Pump(14);
if (temp != "File Signature")
return false;
CTR_Mode<AES>::Decryption decryptor;
StreamTransformationFilter* filter = NULL;
// Detach StringSink(temp), Attach StreamTransformationFilter(decryptor)
file.Detach(filter = new StreamTransformationFilter(decryptor));
// Set Key and IV
decryptor.SetKeyWithIV(meta_key, 32, meta_iv);
// Detach nothing, Attach StringSink(metainfo)
filter->Detach(new StringSink(metainfo));
// FileSource → decryptor → metainfo
file.Pump(256);
// Set Key and IV
decryptor.SetKeyWithIV(key, 32, iv);
// Detach StringSink(metainfo), Attach StringSink(decoded)
filter->Detach(new StringSink(decoded));
while(!file.SourceExhausted())
{
// FileSource → decryptor → decoded
file.Pump(512);
std::cout << decoded;
}
您可能对Crypto ++ wiki上的Pipelining感兴趣。感兴趣的还有BufferedTransformation,它是用于流水线操作的基类。