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.

1 Answer 1


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. 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 to convert to Base58.

1 - Take a private key


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


3 - Perform SHA-256 hash on the extended key


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


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


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


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

  • would you tell me how you hash in step 3? because the strait hash is not the putout that you are showing May 29, 2020 at 9:02
  • 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? May 29, 2020 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.