如何在Crypto++中更改接收器
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++中更改接收器?
接收器只是一个没有附加转换的筛选器。若要更改接收器,只需更改前置对象或父对象的附加过滤器即可。棘手的部分是访问过滤器链中两三个深度的过滤器。
使用以下内容。过滤器有两种附加过滤器的方法:Attach
和Detach
。它们都为对象附加了一个新的过滤器;但CCD_ 4返回旧的滤波器,而CCD_
另一个奇怪的是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,它是用于流水线的基类。
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- hkdf.h 在 Crypto++ 库中找不到
- 解析 crypto++ 中的 GFP2Element
- 如何组织从提升日志接收器进行线程安全读取?
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- Crypto++ GetModulus() issue
- "ChaCha"尚未在此范围内使用Crypto++声明?
- 在Crypto++中向AES解密传递密钥
- 使用crypto++的HMAC解密植物文本
- 如何将控制台(stdout)添加为g3log中的接收器
- C# 中的 RSA 签名和 Crypto++ C++中的验证
- 如何通过 Crypto++ 和 RSA 对文件进行签名
- Crypto++ PKCS5_PBKDF2_HMAC类签名的原因?
- 为什么Crypto++ SecByteBlock下标有效
- 从字符串 Crypto++ 导入 RSA 公钥/私钥
- 如何使用boost.thread运行多个接收器,每个线程应该分配给每个接收器?
- 如何在Crypto++ ECDSA中从签名正文中获取签名长度
- Crypto++ CRC32 linker errór
- UDP 客户端(接收器)未按预期接收数据报
- 如何在Crypto++中更改接收器