Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比特币与消息验证_Java_Message_Signature_Verification_Bitcoinj - Fatal编程技术网

Java 比特币与消息验证

Java 比特币与消息验证,java,message,signature,verification,bitcoinj,Java,Message,Signature,Verification,Bitcoinj,给定三条信息:消息(字符串)、签名(字符串)和公共地址(字符串),我想验证签名。在Javascript、Python和PHP库中,这是一个简单的方法调用。然而,在BitcoinJ(Java)中,我无法提出一个简单的解决方案或示例 首先,我只想验证一个签名。BitcoinJ有些过分,但它是我能找到的唯一一个Java库。对于我需要的东西,它似乎也没有直接的方法调用。有一个方法调用需要公钥,但我有公共地址。网络上的讨论表明,公钥可以从签名中获取。然而,这似乎并不像听起来那么直截了当。Java也需要字节

给定三条信息:消息(字符串)、签名(字符串)和公共地址(字符串),我想验证签名。在Javascript、Python和PHP库中,这是一个简单的方法调用。然而,在BitcoinJ(Java)中,我无法提出一个简单的解决方案或示例

首先,我只想验证一个签名。BitcoinJ有些过分,但它是我能找到的唯一一个Java库。对于我需要的东西,它似乎也没有直接的方法调用。有一个方法调用需要公钥,但我有公共地址。网络上的讨论表明,公钥可以从签名中获取。然而,这似乎并不像听起来那么直截了当。Java也需要字节而不是字符串。听起来很容易,但努力并没有奏效。我找不到任何例子

那么,有人能给我举一个Java(BitcoinJ或其他)中的简单消息验证示例,其中包含上述三条信息吗?蒂亚

发布我自己的答案:

String loginSig = ""; // base64 encoded signature
String pubAddress = ""; // bitcoin public address
String message = "Hello World";

ECKey result = new ECKey().signedMessageToKey(message, loginSig);

if (pubAddress.equals(result.toAddress(NetworkParameters.prodNet()).toString())) {
    // success!
}

下面是对mohrt应答的更正,没有不推荐的调用,也没有根据提供的地址猜测网络

/**
 * Validate a signature.
 *
 * @param address   Bitcoin address
 * @param signature Signature content
 * @param message   Signed message
 * @return true if the signature of a given message by the given address is correct.
 */
public boolean isValidSignature(String address, String signature, String message) {
    try {
        return ECKey.signedMessageToKey(message, signature).toAddress(Address.fromBase58(null, address).getParameters()).toString().equals(address);
    } catch (Exception e) {
        return false;
    }
}

ECKey.signedMessageToKey是一个静态方法,所以行应该是:ECKey result=ECKey.signedMessageToKey(message,loginSig);