【问题标题】:SecByteBlock to ECDSA<ECP, SHA256>::PublicKey and reverse in CryptoPPSecByteBlock 到 ECDSA<ECP, SHA256>::PublicKey 和 CryptoPP 中的反向
【发布时间】:2020-10-15 23:07:47
【问题描述】:

正如标题所说,有没有办法将 SecByteBlock 中的密钥转换为 ECDSA::PublicKey 和相反?

根据我的阅读,我刚刚设法将 PublicKey 转换为字符串:

    PubicKey pubKey;
    string s;
    StringSink ss(s);
    pubKey.Save(ss);

【问题讨论】:

  • 它是一个公钥,为什么你的密钥首先是SecByteBlock?您可能应该从中获取字节缓冲区,然后创建 x & y 坐标以创建公共点...

标签: c++ cryptography crypto++


【解决方案1】:
 //To string

 string encoded;


    // Pretty print key
    encoded.clear();

      
        StringSource(key, key.size(), true,
                     new HexEncoder(
                             new StringSink(encoded)
                     ) // HexEncoder
        ); // StringSource

PublicKey string_to_publicKey(string keyInHex){
    string decodedKey;
    HexDecoder decoder;
    decoder.Put( (byte*)keyInHex.data(), keyInHex.size() );
    decoder.MessageEnd();

    word64 size = decoder.MaxRetrievable();
    if(size && size <= SIZE_MAX)
    {
        decodedKey.resize(size);
        decoder.Get((byte*)&decodedKey[0], decodedKey.size());
    }
    cout << "Decoded : " << decodedKey << endl;
    StringSource ss(decodedKey, true);
    PublicKey pubKey;
    pubKey.Load(ss);

    return pubKey;
}
string public_key_to_string(PublicKey publicKey){
    string ss;
    StringSink sink(ss);
    publicKey.Save(sink);
    string encoded;
    HexEncoder encoder;
    encoder.Put((byte*)ss.data(), ss.size());
    encoder.MessageEnd();
    word64 size = encoder.MaxRetrievable();
    if(size)
    {
        encoded.resize(size);
        encoder.Get((byte*)&encoded[0], encoded.size());
    }
    std::cout << "key in hex: " << encoded;

    return encoded;
}

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2014-11-12
    • 2014-11-27
    • 2014-11-26
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多