<p>I have private key as 'cUN9LNcEC54HAbWAwUs6coPSc72TcQYzxf4qSqdHJPVKSapeHzFj' and payload as 'hello', now how to create the signature for this payload using private key in java with bitcoinj library?</p>
<pre><code>public static String createSignature1(String payload) throws NoSuchAlgorithmException {
    ECKey pubKey = ECKey.fromPrivate(publicKeyHash(&quot;cUN9LNcEC54HAbWAwUs6coPSc72TcQYzxf4qSqdHJPVKSapeHzFj&quot;.getBytes(StandardCharsets.UTF_8)));
    ECKey.ECDSASignature signatureBase64 = pubKey.sign(Sha256Hash.wrap(DigestUtils.sha256(Arrays.toString(payload.getBytes(UTF_8)))));
    Log.info(&quot;Signature is : &quot; + signatureBase64);
    Log.info(&quot;Base64 encoded Signature is : &quot; + byteArrayToHex(Base64.getEncoder().encode(signatureBase64.encodeToDER())));
    return byteArrayToHex(signatureBase64.encodeToDER());
}
</code></pre>
<p>The above code returning invalid signature.</p>

CC BY-SA 4.0
2021-01-21T19:08:05.680Z
https://bitcoin.stackexchange.com/questions/50562/how-to-sign-raw-transaction-given-a-private-key-and-sha-hash-in-java?rq=1 answer helped me to achieve this.
Bhala T R
CC BY-SA 4.0
2021-01-21T19:10:42.260Z
https://bitcoin.stackexchange.com/a/50579/100550 helped and saved time, thanks a lot @sven-williamson
Bhala T R
<private-key><signature><bitcoinj><java><library>
How to create signature for the payload (data)using private key in java with bitcoinj?
Bhala T R
# How to create signature for the payload (data)using private key in java with bitcoinj?

(Asked by: username_0 on 2021-01-21)

I have private key as 'cUN9LNcEC54HAbWAwUs6coPSc72TcQYzxf4qSqdHJPVKSapeHzFj' and payload as 'hello', now how to create the signature for this payload using private key in java with bitcoinj library?
public static String createSignature1(String payload) throws NoSuchAlgorithmException {
    ECKey pubKey = ECKey.fromPrivate(publicKeyHash("cUN9LNcEC54HAbWAwUs6coPSc72TcQYzxf4qSqdHJPVKSapeHzFj".getBytes(StandardCharsets.UTF_8)));
    ECKey.ECDSASignature signatureBase64 = pubKey.sign(Sha256Hash.wrap(DigestUtils.sha256(Arrays.toString(payload.getBytes(UTF_8)))));
    Log.info("Signature is : " + signatureBase64);
    Log.info("Base64 encoded Signature is : " + byteArrayToHex(Base64.getEncoder().encode(signatureBase64.encodeToDER())));
    return byteArrayToHex(signatureBase64.encodeToDER());
}

The above code returning invalid signature.

**Tags:** private-key, signature, bitcoinj, java, library

> **Comments:**
> **username_0**: https://bitcoin.stackexchange.com/questions/50562/how-to-sign-raw-transaction-given-a-private-key-and-sha-hash-in-java?rq=1 answer helped me to achieve this.  
> **username_0**: https://bitcoin.stackexchange.com/a/50579/100550 helped and saved time, thanks a lot @sven-williamson  

---

## 0 answers

