我正在尝试将带有 DEREncodePrivateKey 的 ECIES num0 PrivateKey 存储到 std::string 并将其重新加载到 num1 PrivateKey 对象中以进行测试。
问题是当 key 在第二个 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 编码的私有(private)指数不能确定 privateKey Object 的曲线 OID,因此在 BER 解码和导入 key 之前,我们需要以某种方式确定对象的 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*/)
对于现有对象
关于c++ - crypto++ ECIES BERDecodePrivateKey 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732415/