如何更改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