[英]How to change sink in Crypto++
我正在使用 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;
}
我怎樣才能做到這一點?
如何更改 Crypto++ 中的接收器?
Sink 只是一個沒有附加轉換的過濾器。 要更改接收器,您只需更改前置對象或父對象的附加過濾器。 棘手的部分是訪問過濾器鏈中兩到三個深度的過濾器。
使用類似以下內容。 過濾器有兩種附加過濾器的方法: Attach
和Detach
。 他們都為對象附加了一個新的過濾器; 但是Attach
返回舊過濾器,而Detach
free 的它。
另一個奇怪的是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 上的流水線感興趣。 同樣感興趣的可能是BufferedTransformation ,它是用於流水線的基類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.