【问题标题】:Get ECDSA signature with Crypto++使用 Crypto++ 获取 ECDSA 签名
【发布时间】:2025-12-23 18:50:12
【问题描述】:

我必须使用 Crypto++ 在变量中获取 ECDSA 签名。
我在启动 SignMessage 后尝试获取它,但签名为空。
我怎么能得到它?

【问题讨论】:

    标签: c++ crypto++ ecdsa


    【解决方案1】:

    您看过 Crypto++ 维基吗? Elliptic Curve Digital Signature Algorithm 上有很多东西。

    不清楚你在做什么或哪里出了问题,所以这里是来自 wiki 的复制和粘贴:

    签名:

    ECDSA<ECP, SHA1>::PrivateKey privateKey;
    privateKey.Load(...);
    
    AutoSeededRandomPool prng;
    string message = "Yoda said, Do or do not. There is no try.";
    string signature;
    
    StringSource ss1( message, true /*pump all*/,
        new SignerFilter( prng,
            ECDSA<ECP,SHA1>::Signer( privateKey ),
            new StringSink( signature )
        ) // SignerFilter
    ); // StringSource
    

    验证:

    ECDSA<ECP, SHA1>::PublicKey publicKey;
    publicKey.Load(...);
    
    // Result of the verification process
    bool result = false;
    
    // Exactly what was signed in the previous step
    string message = ...;
    // Output from the signing operation in the previous step
    string signature = ...;
    
    StringSource ss2( signature+message, true /*pump all*/,
        new SignatureVerificationFilter(
            ECDSA<ECP,SHA1>::Verifier(publicKey),
            new ArraySink( (byte*)&result, sizeof(result) )
        ) // SignatureVerificationFilter
    );
    
    // Verification failure?
    if( !result ) {...}
    

    如果您希望验证失败,请尝试:

    static const int VERIFICATION_FLAGS = SIGNATURE_AT_BEGIN | THROW_EXCEPTION;
    StringSource ss3( signature+message, true /*pump all*/,
        new SignatureVerificationFilter(
            ECDSA<ECP,SHA1>::Verifier(publicKey),
            NULL, /* No need for attached filter */
            VERIFICATION_FLAGS
        ) // SignatureVerificationFilter
    );
    

    【讨论】: