【发布时间】:2018-03-11 00:03:22
【问题描述】:
我已经在我的程序中成功使用了这几行代码:
string tmp;
StringSource(msg, true, new PK_EncryptorFilter(*rng, *encryptor, new CryptoPP::HexEncoder(new StringSink(tmp))));
return tmp;
所以你知道 Crypto++ 对象是很好创建的等等。
现在我想加密整个二进制文件并将其保存到相邻的文件中:
FileSource(file.c_str(), true, new PK_EncryptorFilter(*rng, *encryptor, new FileSink((file+".xx").c_str(), true)),true);
但是最后一行崩溃并显示调试错误,指出 abort() 已被调用。
寻找错误,我尝试将 FileSource 调用的第二个参数更改为 false,导致以下代码:
FileSource(file.c_str(), false, new PK_EncryptorFilter(*rng, *encryptor, new FileSink((file+".xx").c_str(), true)),true);
然后错误消失了,但目标文件的权重为 0 字节,没有读取/写入任何内容。
不知道有什么可以解决问题的关键,所以,希望有人能帮忙一点。
编辑:我正在使用 Visual Studio 2013 Pro。
EDIT2:我进一步寻找错误。
这有效,文件二进制内容正确打印在屏幕上:
string s;
FileSource file2("C:\\test.jpg", true, new StringSink(s));
std::cout << s << std::endl;
但这不起作用并以提到的崩溃结束。
string s;
FileSource file2("C:\\test.jpg", true, new PK_EncryptorFilter(*rng, *encryptor, new StringSink (s)));
std::cout << s << std::endl;
这很奇怪,因为正如我在帖子开头所说的那样,在另一种方法中使用相同的 PK_EncryptorFilter 过滤器没有问题。
无论如何,我把我的整个班级都贴在这里,以便清楚地了解发生了什么:
RSASystem::RSASystem()
{
std::string pubkey = "...OMITED...";
rng = new AutoSeededRandomPool;
CryptoPP::HexDecoder decoder;
decoder.Put((byte*)pubkey.c_str(), pubkey.size());
decoder.MessageEnd();
CryptoPP::HexDecoder decoder2;
decoder2.Put((byte*)pubkey.c_str(), pubkey.size());
decoder2.MessageEnd();
verifier = new RSASSA_PKCS1v15_SHA_Verifier;
encryptor = new RSAES_OAEP_SHA_Encryptor;
verifier->AccessKey().Load(decoder);
encryptor->AccessKey().Load(decoder2);
}
string RSASystem::encrypt(string msg)
{
string tmp;
StringSource(msg, true, new PK_EncryptorFilter(*rng, *encryptor, new CryptoPP::HexEncoder(new StringSink(tmp))));
return tmp;
}
void RSASystem::encryptFile(string file)
{
FileSource(file.c_str(), true, new PK_EncryptorFilter(*rng, *encryptor, new FileSink((file+".xx").c_str(), true)),true);
}
编辑 3:用 try..catch() 包围代码后,出现此错误:
RSA/OAEP-MGF1(SHA-1): message length of 490986 exceeds the maximum of 214 for this public key
现在我认为很容易解决。
【问题讨论】:
-
你能得到 Crypto++ wiki 上提供的示例代码吗? (我想知道它是否是图书馆的问题,维基代码应该是好的)。
-
我会在几个小时内尝试一下,或者,我现在不在家...
-
我现在知道这不是安慰,但你已经接近解决这个问题了,因为它只是源和汇之间的区别。这可能是路径或文件名问题。一定要抓住
CryptoPP::Exception,看看会发生什么。 -
好的,用 try..catch() 包围代码我明白了:
RSA/OAEP-MGF1(SHA-1): message length of 490986 exceeds the maximum of 214 for this public key终于有道理了。我该如何解决这个问题? -
甚至不用理会上面的(2)或security.stackexchange.com/questions/44702/…。只需使用 ECIES 或 DLIES/DHAES 等集成加密方案。集成方案使用公钥加密,利用对称批量加密并提供身份验证标签。因为身份验证标签内置在方案中,您将不需要需要单独的签名者。
标签: c++ encryption crypto++