我正在尝试将具有DEREncodePrivateKey的ECIES num0私有密钥存储到std :: string并将其重新加载到num1 PrivateKey对象中以进行测试。问题是,当密钥在第二个PrivateKey对象中加载了BERDecodePrivateKey时,无法对其进行验证(也在未经验证的情况下测试了加密和解密,也没有解密)
这是代码
using namespace CryptoPP;
CryptoPP::AutoSeededRandomPool prng;
ECIES<ECP>::PrivateKey pp;
pp.Initialize(prng, ASN1::secp256k1());
/* returns true*/
bool val=pp.Validate(prng, 3);
std::string saves;
StringSink savesink(saves);
pp.DEREncodePrivateKey(savesink);
/*additional unnecessary steps to make sure the key is written completely */
savesink.MessageEnd();
savesink.Flush(true);
ECIES<ECP>::PrivateKey pro;
StringSource savesSource(saves, true);
pro.BERDecodePrivateKey(savesSource,true,savesSource.MaxRetrievable());
/*here the exception is thrown */
pro.ThrowIfInvalid(prng, 3);
终于找到了问题所在正如@ maarten-bodewes在评论中提到的那样,DER编码的私有指数无法确定privateKey对象的曲线OID,因此在BER解码和导入密钥之前,我们需要以某种方式确定该对象的OID;最简单的方法是在初始化新对象时确定它上面的代码更改为:
ECIES<ECP>::PrivateKey pro;
StringSource savesSource(saves, true);
auto rett = savesSource.MaxRetrievable();
pro.Initialize(prng, ASN1::secp256k1());
pro.BERDecodePrivateKey(savesSource,true,savesSource.MaxRetrievable());
还您是现有对象的AccessGroupParameters().Initialize(/*OID*/);
或Initialize(/*OID*/)