1

我想用 Java 对数据进行签名,然后用 C++ 对其进行验证。当我在 c++ 中使用 bitcoin-core/secp256k1 库对数据进行签名,然后在 c++ 中对其进行验证时,它可以完美运行。但是,当我用 Java 对其进行签名然后将其传输到 c++ 时,它就不再起作用了。

        secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);

        secp256k1_pubkey pubkey;
        secp256k1_ecdsa_signature sig;

        signature_len = 71;
        // I paste in the generated signature from java
        const char* signature2 = "3045022100C5C52074B7F86130D52FC6286D20E95D974D2A9457B8F39223FA571CA1419B5602203B5DA5922770EE472A2A6235B2FC07C1125763DC4C896A6561D4FD959FAA4C7D";
        char signature3[72];
        hex2bin(signature2, signature3);
        memcpy(signature, signature3, signature_len);
        
        // I paste in the generated public key from java
        const char* pub_key2 = "0300F447B363996717EB807D950BC8C35C648F1AD27E98AD9E23487F4A4E7EEA2F";
        char pub_key3[33];
        hex2bin(pub_key2, pub_key3);
        memcpy(public_key, pub_key3, 33);
        
        std::string data = Vendor::sha256("BANANA");
        memcpy(hash, data.c_str(), 32);

        if (secp256k1_ec_pubkey_parse(ctx, &pubkey, public_key, 33) == 0) {
            LOG_WNL("Error parsing public key!");
        }

        if (secp256k1_ecdsa_signature_parse_der(ctx, &sig, signature, signature_len) == 0) {
            LOG_WNL("Error parsing signature!");
        }

        int res = secp256k1_ecdsa_verify(ctx, &sig, hash, &pubkey);


        return res == 1;

Java代码:

            Signature dsa = Signature.getInstance("SHA256withECDSA");

            dsa.initSign(privateKey);

            String str = "BANANA";

            Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
            ecdsaSign.initSign(privateKey);
            ecdsaSign.update(str.getBytes("ASCII"));

            byte[] signature = ecdsaSign.sign();
            String sig = getHex(signature);

            return sig;

我会非常感谢一个答案!

4

0 回答 0