作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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;
}
我怎样才能做到这一点?
最佳答案
How to change sink in Crypto++?
Sink 只是一个没有附加转换的 Filter。要更改接收器,您只需更改前置对象或父对象的附加过滤器。棘手的部分是访问过滤器链中两到三个深度的过滤器。
使用类似下面的东西。过滤器有两种附加过滤器的方法:Attach
和Detach
。他们都为对象附加了一个新的过滤器;但是 Attach
返回旧过滤器,而 Detach
释放它。
另一个奇怪的地方是 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(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 感兴趣在 Crypto++ 维基上。同样感兴趣的可能是 BufferedTransformation ,这是用于流水线操作的基类。
关于c++ - 如何在 Crypto++ 中更改接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42221820/
我是一名优秀的程序员,十分优秀!