如何更改Crypto ++中的接收器?
接收器只是一个没有附加转换的过滤器。要更改接收器,只需更改前置对象或父对象的附加过滤器即可。棘手的部分是获得过滤器链中两到三个深度的过滤器。
使用下面的内容。过滤器有两种附加过滤器的方法: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;
}
你可能会在加密+维基兴趣Pipelining。另外感兴趣的可能是BufferedTransformation,这是用于流水线的基类。
来源
2017-02-14 17:20:39
jww