2017-02-14 21 views
2

我使用加密+對文件進行解密,所以我用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; 
} 

我該如何做到這一點?

回答

1

如何更改Crypto ++中的接收器?

接收器只是一個沒有附加轉換的過濾器。要更改接收器,只需更改前置對象或父對象的附加過濾器即可。棘手的部分是獲得過濾器鏈中兩到三個深度的過濾器。

使用下面的內容。過濾器有兩種附加過濾器的方法:AttachDetach。他們都附加一個新的過濾器的對象;但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,這是用於流水線的基類。