简体   繁体   English

在 Crypto++ 中使用 ECDSA 的正确方法是什么

[英]What is the correct way to use ECDSA in Crypto++

When I verify a signature using ECDSA verifier in Crypto++, if the public key is incorrect the method just crash the application.当我在 Crypto++ 中使用 ECDSA 验证器验证签名时,如果公钥不正确,该方法只会使应用程序崩溃。 Should I try catch the exception?我应该尝试捕获异常吗? What is the best way to handle this?处理这个问题的最佳方法是什么?

Thanks!谢谢!

... should I try catch the exception? ...我应该尝试捕获异常吗? What is the best way to handle this?处理这个问题的最佳方法是什么?

It depends on how you want to do it.这取决于你想怎么做。 I think there are three options.我认为有三种选择。

The information below is from Elliptic Curve Digital Signature Algorithm and SignatureVerificationFilter on the Crypto++ wiki.以下信息来自 Crypto++ wiki 上的椭圆曲线数字签名算法SignatureVerificationFilter

First, you can catch the SignatureVerificationFailed exception if you like:首先,如果您愿意,可以捕获SignatureVerificationFailed异常:

try
{
    DSA::Verifier verifier(publicKey);
    StringSource ss2(message+signature, true,
        new SignatureVerificationFilter(
            verifier, NULL, THROW_EXCEPTION
            /* SIGNATURE_AT_END */
       )
    );

    std::cout << "Verified signature on message" << std::endl;
}
catch (SignatureVerificationFailed& ex)
{
    std::cerr << "Failed to verify signature on message" << std::endl;
}

Second, you can get the result as a boolean value.其次,您可以获得 boolean 值的结果。 Notice lack of THROW_EXCEPTION :注意缺少THROW_EXCEPTION

bool result = false;
StringSource ss(message+signature, true,
    new SignatureVerificationFilter(
        verifier,
        new ArraySink(
            (byte*)&result, sizeof(result)),
        PUT_RESULT | SIGNATURE_AT_END
   )
);

if(result)
    std::cout << "Verified signature on message" << std::endl;
else
    std::cerr << "Failed to verify signature on message" << std::endl;

Third, you can forgo pipelines and just call VerifyMessage on the Verifier object:第三,您可以放弃管道,只需在Verifier object 上调用VerifyMessage

bool result = verifier.VerifyMessage(ConstBytePtr(message), BytePtrSize(message), ConstBytePtr(signature), BytePtrSize(signature));
if(result)
    std::cout << "Verified signature on message" << std::endl;
else
    std::cerr << "Failed to verify signature on message" << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM