<p>Most private keys are essentially just random numbers, so if all you want is a private key, you just need to use a high-entropy random function to generate your private key that's 256 bits. </p>

<p>Converting those bits into something a normal bitcoin wallet can understand (like WIF) is a little trickier, but the process is spelled out <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow">here</a>. I'm recapping the actual process here so you can use the Java library <code>java.security.MessageDigest</code> for SHA256 and you can use <a href="https://code.google.com/p/bitcoinj/source/browse/core/src/main/java/com/google/bitcoin/core/Base58.java" rel="nofollow">this code snippet</a> to convert to Base58.</p>

<p>1 - Take a private key</p>

<pre><code>0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
</code></pre>

<p>2 - Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. Also add a 0x01 byte at the end if the private key will correspond to a compressed public key</p>

<pre><code>800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D
</code></pre>

<p>3 - Perform SHA-256 hash on the extended key</p>

<pre><code>8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592
</code></pre>

<p>4 - Perform SHA-256 hash on result of SHA-256 hash</p>

<pre><code>507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714
</code></pre>

<p>5 - Take the first 4 bytes of the second SHA-256 hash, this is the checksum</p>

<pre><code>507A5B8D
</code></pre>

<p>6 - Add the 4 checksum bytes from point 5 at the end of the extended key from point 2</p>

<pre><code>800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D
</code></pre>

<p>7 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the Wallet Import Format</p>

<pre><code>5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ
</code></pre>

CC BY-SA 4.0
2020-05-29T09:02:54.050Z
would you tell me how you hash in step 3? 
because the strait hash is not the putout that you are showing
Masoud Darzi
CC BY-SA 4.0
2020-05-29T09:03:56.520Z
actually I know that you must hash the binary, and I did that too, but the results are not the same, so can you please tell me how to make the step 3 achievement?
Masoud Darzi
CC BY-SA 3.0
2015-02-26T02:52:52.360Z
2015-02-26T02:52:52.360Z
Jimmy Song
<p>I'm looking for a way to generate private keys in Java. I'd like to make this a standalone program, so I'd rather not use a library like bitcoinj. From my research I've only seen people recommend using bitcoinj. Has anyone created anything like this? Thanks.</p>

<private-key><address-generation><java>
Generating private keys in Java
busterroni
# Generating private keys in Java

(Asked by: username_0 on 2015-02-25)

I'm looking for a way to generate private keys in Java. I'd like to make this a standalone program, so I'd rather not use a library like bitcoinj. From my research I've only seen people recommend using bitcoinj. Has anyone created anything like this? Thanks.

**Tags:** private-key, address-generation, java


---

## 1 answers

### Answer by username_1 on 2015-02-26. Score: 3 | Accepted answer

Most private keys are essentially just random numbers, so if all you want is a private key, you just need to use a high-entropy random function to generate your private key that's 256 bits.

Converting those bits into something a normal bitcoin wallet can understand (like WIF) is a little trickier, but the process is spelled out [here](https://en.bitcoin.it/wiki/Wallet_import_format). I'm recapping the actual process here so you can use the Java library `java.security.MessageDigest` for SHA256 and you can use [this code snippet](https://code.google.com/p/bitcoinj/source/browse/core/src/main/java/com/google/bitcoin/core/Base58.java) to convert to Base58.

1 - Take a private key

```
0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

```

2 - Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. Also add a 0x01 byte at the end if the private key will correspond to a compressed public key

```
800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

```

3 - Perform SHA-256 hash on the extended key

```
8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

```

4 - Perform SHA-256 hash on result of SHA-256 hash

```
507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714

```

5 - Take the first 4 bytes of the second SHA-256 hash, this is the checksum

```
507A5B8D

```

6 - Add the 4 checksum bytes from point 5 at the end of the extended key from point 2

```
800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D

```

7 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the Wallet Import Format

```
5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

```

> **Comments:**
> **username_2**: would you tell me how you hash in step 3?  because the strait hash is not the putout that you are showing  
> **username_2**: actually I know that you must hash the binary, and I did that too, but the results are not the same, so can you please tell me how to make the step 3 achievement?  

---

