我正在尝试将带有DEREncodePrivateKey的ECIES num0 PrivateKey存储到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);发布于 2020-01-19 02:19:50
最终发现了问题所在,正如@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*/)操作
https://stackoverflow.com/questions/59732415
复制相似问题