相关疑难解决方法(0)

ECDSA 与 BouncyCastle 签署并使用 Crypto++ 进行验证

这是Java代码:

public static String sign(String data) throws Exception {
    KeyPair keyPair = loadKeyPair(System.getProperty("user.dir"), "ECDSA");
    Signature signature = Signature.getInstance("SHA256withECDSA", "BC");
    signature.initSign(keyPair.getPrivate(), new SecureRandom());

    byte[] message = data.getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();        
    String signatureStr = new BigInteger(1, sigBytes).toString(16);
    return signatureStr;
}
Run Code Online (Sandbox Code Playgroud)

然后是验证签名的C++代码

bool VerifyMessage( const ECDSA<ECP, SHA256>::PublicKey& key, const string& message, const string& signature )
{
    bool result = false;

    // Hexa encoding version, more readable
    std::string decodedSignature;
    StringSource(signature, true,
                    new HexDecoder(
                       new StringSink(decodedSignature)));

    StringSource(decodedSignature+message, true,
                    new SignatureVerificationFilter(ECDSA<ECP,SHA256>::Verifier(key),
                       new ArraySink((byte*)&result, sizeof(result)))); …
Run Code Online (Sandbox Code Playgroud)

c++ java bouncycastle crypto++ ecdsa

5
推荐指数
1
解决办法
3354
查看次数

标签 统计

bouncycastle ×1

c++ ×1

crypto++ ×1

ecdsa ×1

java ×1