[英]How to change sink in Crypto++
I'm using Crypto++ to decrypt a file, so I use FileSource
as my source but I want to be able to change sink, so I can achieve something like following:我正在使用 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 can I achieve this?我怎样才能做到这一点?
How to change sink in Crypto++?
如何更改 Crypto++ 中的接收器?
A Sink is just a Filter that has no attached transformation. Sink 只是一个没有附加转换的过滤器。 To change a sink, you just change the attached filter of the predecessor or parent object.
要更改接收器,您只需更改前置对象或父对象的附加过滤器。 The tricky part is getting access to a filter that's two or three deep in the filter chain.
棘手的部分是访问过滤器链中两到三个深度的过滤器。
Use something like the following.使用类似以下内容。 Filters have two methods for attaching filters:
Attach
and Detach
.过滤器有两种附加过滤器的方法:
Attach
和Detach
。 They both attach a new filter to the object;他们都为对象附加了一个新的过滤器; but
Attach
returns the old filter while Detach
free's it.但是
Attach
返回旧过滤器,而Detach
free 的它。
The other oddity is the Redirector
.另一个奇怪的是
Redirector
。 You can use it to break ownership in a chain.您可以使用它来破坏链中的所有权。 Its kind of needed for the
StreamTransformationFilter filter
.它是
StreamTransformationFilter filter
所需要的。 The stack based allocation will be free'd as a local variable, so you don't want it free'd as part of a chain, too.基于堆栈的分配将作为局部变量被释放,因此您不希望它也作为链的一部分被释放。
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;
}
Here's another way to do it without the Redirector
.这是没有
Redirector
的另一种方法。 It stashes away a pointer to the StreamTransformationFilter
:它隐藏了一个指向
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;
}
You might be interested in Pipelining on the Crypto++ wiki.您可能对 Crypto++ wiki 上的流水线感兴趣。 Also of interest might be BufferedTransformation , which is the base class used for pipelining.
同样感兴趣的可能是BufferedTransformation ,它是用于流水线的基类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.