c++ - crypto++ ECIES BERDecodePrivateKey 的问题

标签 c++ cryptography public-key-encryption crypto++ ecies

我正在尝试将带有 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/

相关文章:

php - 使用 PHP 的非对称加密

python - 为简单的 RSA 加密示例添加随机性

c++ - 为什么 CWE 认为 rand() 具有潜在危险

android - 我应该在 iOS Android 应用程序上使用什么加密方法 : AES128 or 3DES

c++ - Eigen's unsupported/CXX11/Tensor模块的编译错误

C++ 如何使用 find 函数在 char 数组中查找 char?

python - 无法在 python 中的 AES.MODE_CTR 密码中使用 256 位计数器

使用 BigInteger 作为 key 的 Java AES 加密/解密

c++ - 对字节字符串进行 URL 编码?

c++ - 我的 C++ 应用程序无法连接到 Informix DB 服务器