<p>If all you are after is getting an <code>ECKey</code> object for that private key, then Bitcoinj has a <code>DumpedPrivateKey</code> class for just such an occasion.</p>

<pre><code>import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.params.MainNetParams;

public class Example {

    public static void main(String[] args) {
        String priv = "L4...5";
        ECKey key = DumpedPrivateKey.fromBase58(MainNetParams.get(), priv).getKey();
    }

}
</code></pre>

<p>If you would like to use an <code>ECKey</code> constructor explicitly, then you should be able to get the code you need from here: </p>

<blockquote>
  <p><a href="https://github.com/bitcoinj/bitcoinj/blob/release-0.14/core/src/main/java/org/bitcoinj/core/DumpedPrivateKey.java#L70-L91" rel="nofollow">https://github.com/bitcoinj/bitcoinj/blob/release-0.14/core/src/main/java/org/bitcoinj/core/DumpedPrivateKey.java#L70-L91</a></p>
</blockquote>

<hr>

<p>With respect to the constructor you listed, it is a protected constructor, so it's probably not one you would be looking to use. It lists a private key and public key parameters. The private key may be null if the <code>ECKey</code> instance is only going to be used for verifying signatures, not actually producing them. The public key must not be null, and it saves time by not needing to re-compute the public key, which is a CPU intensive operation.</p>

<p>Note that most of the <code>public ECKey</code> constructors are deprecated. The preferred way of creating the <code>ECKey</code> objects now is to use the static factory methods.</p>

CC BY-SA 4.0
2021-04-01T11:16:11.250Z
Thank you very very very very much... worked fine for me...
ultra.deep
CC BY-SA 3.0
2016-07-29T13:56:59.750Z
2016-07-29T14:06:12.340Z
2016-07-29T14:06:12.340Z
morsecoder
<p>You can call the public static method of ECKeyclass,  fromPrivate(byte[] privKeyBytes, boolean compressed). The public key is calculated from it (this is slow), either compressed or not. Eg., </p>

<pre><code>        String privateKey = "Your Private Key";
        byte[] b = privateKey.getBytes();
        ECKey key = ECKey.fromPrivate(b, true);
</code></pre>

<p>or </p>

<pre><code>BigInteger privKey = Base58.decodeToBigInteger(privateKey );
key = ECKey.fromPrivate(privKey);
</code></pre>

<p><a href="https://bitcoinj.github.io/javadoc/0.13/org/bitcoinj/core/ECKey.html" rel="nofollow noreferrer">https://bitcoinj.github.io/javadoc/0.13/org/bitcoinj/core/ECKey.html</a></p>

CC BY-SA 3.0
2018-01-13T15:56:59.183Z
2018-01-13T22:36:56.043Z
2018-01-13T22:36:56.043Z
bitsabhi
<p>Can someone please explain how to use the following constructor (listed <a href="https://bitcoinj.github.io/javadoc/0.12/org/bitcoinj/core/ECKey.html" rel="nofollow noreferrer">here</a>):</p>
<pre><code>ECKey(BigInteger priv, org.spongycastle.math.ec.ECPoint pub) 
</code></pre>
<p>Say my private key is: <code>priv = &quot;L4...5&quot;</code>.</p>
<ol>
<li><p>How do I get the big integer representation of it (in other words, and if I understood correctly, is there a function to go from <code>check58</code> to <code>biginteger</code>?) , and</p>
</li>
<li><p>what about the second parameter?</p>
</li>
</ol>
<p>Thanks</p>

<private-key><bitcoinj><private-key-import>
how can I hardcode a private key in bitcoinJ - ECKey constructor?
hartmut
# how can I hardcode a private key in bitcoinJ - ECKey constructor?

(Asked by: username_0 on 2016-07-26)

Can someone please explain how to use the following constructor (listed here):
ECKey(BigInteger priv, org.spongycastle.math.ec.ECPoint pub) 

Say my private key is: priv = "L4...5".

How do I get the big integer representation of it (in other words, and if I understood correctly, is there a function to go from check58 to biginteger?) , and

what about the second parameter?


Thanks

**Tags:** private-key, bitcoinj, private-key-import


---

## 2 answers

### Answer by username_1 on 2016-07-29. Score: 2 | Accepted answer

If all you are after is getting an ECKey object for that private key, then Bitcoinj has a DumpedPrivateKey class for just such an occasion.
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.params.MainNetParams;

public class Example {

    public static void main(String[] args) {
        String priv = "L4...5";
        ECKey key = DumpedPrivateKey.fromBase58(MainNetParams.get(), priv).getKey();
    }

}

If you would like to use an ECKey constructor explicitly, then you should be able to get the code you need from here: 

https://github.com/bitcoinj/bitcoinj/blob/release-0.14/core/src/main/java/org/bitcoinj/core/DumpedPrivateKey.java#L70-L91


With respect to the constructor you listed, it is a protected constructor, so it's probably not one you would be looking to use. It lists a private key and public key parameters. The private key may be null if the ECKey instance is only going to be used for verifying signatures, not actually producing them. The public key must not be null, and it saves time by not needing to re-compute the public key, which is a CPU intensive operation.
Note that most of the public ECKey constructors are deprecated. The preferred way of creating the ECKey objects now is to use the static factory methods.

> **Comments:**
> **username_2**: Thank you very very very very much... worked fine for me...  

---

### Answer by username_3 on 2018-01-13. Score: 0

You can call the public static method of ECKeyclass,  fromPrivate(byte[] privKeyBytes, boolean compressed). The public key is calculated from it (this is slow), either compressed or not. Eg., 
        String privateKey = "Your Private Key";
        byte[] b = privateKey.getBytes();
        ECKey key = ECKey.fromPrivate(b, true);

or 
BigInteger privKey = Base58.decodeToBigInteger(privateKey );
key = ECKey.fromPrivate(privKey);

https://bitcoinj.github.io/javadoc/0.13/org/bitcoinj/core/ECKey.html


---

