<p>The valid private key is <code>L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCn</code> not <code>L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh</code>. You mistyped and added &quot;kh&quot;</p>

CC BY-SA 4.0
2021-05-27T12:46:08.590Z
2021-05-27T12:46:08.590Z
MCCCS
<p>I´d like to construct an ECKey from a hardcoded private key but it´s not working...</p>
<h1>First try</h1>
<p>If I run the code in this <a href="https://github.com/bitcoinj/bitcoinj/blob/master/examples/src/main/java/org/bitcoinj/examples/SendRequest.java" rel="nofollow noreferrer">example</a> the key import is working just fine. But since I actually want so send money I need to import my own key...</p>
<p>I tried this private key L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh and used <a href="https://www.bitaddress.org/bitaddress.org-v3.3.0-SHA256-dec17c07685e1870960903d8f58090475b25af946fe95a734f88408cef4aa194.html" rel="nofollow noreferrer">this site</a> to generate it. When I try to make the private key an ECKey like this:</p>
<pre><code>ECKey key = ECKey.fromPrivate(Base58.decodeToBigInteger(&quot;L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh&quot;));
</code></pre>
<p>I´m getting the exception</p>
<pre><code>Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: private key exceeds 32 bytes: 304 bits
</code></pre>
<h1>Second try</h1>
<p>I tried the solution form <a href="https://bitcoin.stackexchange.com/questions/46737/how-can-i-hardcode-a-private-key-in-bitcoinj-eckey-constructor">this question</a> with this code</p>
<pre><code>String priv = &quot;L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh&quot;;
ECKey key1 = DumpedPrivateKey.fromBase58(TestNet3Params.get(), priv).getKey();
</code></pre>
<p>Which leads me to this exception</p>
<pre><code>Exception in thread &quot;main&quot; org.bitcoinj.core.AddressFormatException$WrongNetwork: Version code of address did not match acceptable versions for network: 128
at org.bitcoinj.core.DumpedPrivateKey.fromBase58(DumpedPrivateKey.java:59)
at com.javamaster.TicTacToeApplication.main(TicTacToeApplication.java:42)
</code></pre>
<h1>Third try</h1>
<p>I tried another solution form <a href="https://bitcoin.stackexchange.com/questions/46737/how-can-i-hardcode-a-private-key-in-bitcoinj-eckey-constructor">this question</a></p>
<pre><code>    byte[] b = priv.getBytes();
    System.out.println(priv);
    ECKey key3 = ECKey.fromPrivate(b, true);
</code></pre>
<p>It results in the same exception as from the second try.</p>
<p>What is wrong with the key? Should I use another way to generate the private key?</p>

<bitcoinj><java>
How to transform a private key into an ECKey?
Greta
# How to transform a private key into an ECKey?

(Asked by: username_0 on 2021-05-27)

I´d like to construct an ECKey from a hardcoded private key but it´s not working...

# First try

If I run the code in this [example](https://github.com/bitcoinj/bitcoinj/blob/master/examples/src/main/java/org/bitcoinj/examples/SendRequest.java) the key import is working just fine. But since I actually want so send money I need to import my own key...

I tried this private key L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh and used [this site](https://www.bitaddress.org/bitaddress.org-v3.3.0-SHA256-dec17c07685e1870960903d8f58090475b25af946fe95a734f88408cef4aa194.html) to generate it. When I try to make the private key an ECKey like this:

```
ECKey key = ECKey.fromPrivate(Base58.decodeToBigInteger("L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh"));

```

I´m getting the exception

```
Exception in thread "main" java.lang.IllegalArgumentException: private key exceeds 32 bytes: 304 bits

```

# Second try

I tried the solution form [this question](https://bitcoin.stackexchange.com/questions/46737/how-can-i-hardcode-a-private-key-in-bitcoinj-eckey-constructor) with this code

```
String priv = "L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh";
ECKey key1 = DumpedPrivateKey.fromBase58(TestNet3Params.get(), priv).getKey();

```

Which leads me to this exception

```
Exception in thread "main" org.bitcoinj.core.AddressFormatException$WrongNetwork: Version code of address did not match acceptable versions for network: 128
at org.bitcoinj.core.DumpedPrivateKey.fromBase58(DumpedPrivateKey.java:59)
at com.javamaster.TicTacToeApplication.main(TicTacToeApplication.java:42)

```

# Third try

I tried another solution form [this question](https://bitcoin.stackexchange.com/questions/46737/how-can-i-hardcode-a-private-key-in-bitcoinj-eckey-constructor)

```
    byte[] b = priv.getBytes();
    System.out.println(priv);
    ECKey key3 = ECKey.fromPrivate(b, true);

```

It results in the same exception as from the second try.

What is wrong with the key? Should I use another way to generate the private key?

**Tags:** bitcoinj, java


---

## 1 answers

### Answer by username_1 on 2021-05-27. Score: 1

The valid private key is `L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCn` not `L48r9sYN2dtsWHWwJ1Xez27W7a3X7RvG6obrmVgU2JLTFQeUCnkh`. You mistyped and added "kh"


---

