A BitCoin Key Generator in Java

@page { margin: 0.79in }
p { margin-bottom: 0.1in; line-height: 120% }
pre.cjk { font-family: “Nimbus Mono L”, monospace }

A BitCoin Key Generator in Java

I suppose the reader is familiar with bitcoin and cryptocurrencies etc. However just to sum it up , the bitcoin private key is based 
on a 256 bit long number , which should be completely random. From this random number the private and public keys are generated following principles of elliptic curve cryptography. Now the important part for the end user is that – A private key in the 
WIF (wallet import format) is required for most programms and to receive money at least a bitcoin address is required. So most wallet programs let you import the key in WIF and calculate the rest for you , as well as doing all the other jobs and you just
have to remember passwords. ( for the complete newbie the one with  the 5 don't show any one and the one starting with the 1 or 3 is what required to receive money)

Now I decided to use Java for various reasons with the bitter thought it is going to be completely different for Android. Nevertheless I started.

The most troublesome part was finding the right parts on the net, Java's not so funny way with dealing with different data types and some guys with an attitude like “a klingon does not comment his code”. 

* avoid using charAt() to convert between data types like Hex-String to String and so on … there are better ways

The SHA256 is required to generate the WIF and it has to be used
twice. To implement the SHA256 by yourself is horribly difficult and there is no need because you can use 

	import java.security.MessageDigest;

and the functions 

     md = MessageDigest.getInstance("SHA-256"); 
     md.update(m.getBytes());
     o=md.digest();

would do the job for you. 🙂 No ! It would be a piece of cake if it would be that easy. 

When using this code in the function you have to add 
	
	throws Exception

But that's only just half of the funny because you can not 
call it then with let's say

	SHA256hash a;
     a = new SHA256hash();
     a.exec(m);
Because a.exec(m); gets you an error message. 
Try the following 

	try{a.exec(m);}catch(Exception e){}

and it shall be done. 

The other terrible thing is String to byte array conversion. 
The update() and digest() function require a byte array as input and they will not work correctly if the byte array is not of the right size. 

I completely abandoned a quick solution and calculated byte by byte from the hex-string. However be aware that Java will always
think numbers above 127 are negative and add a lot of messy 1s 
, so use &255 to get rid of it. 

	i = (int) byte[j] & 255;

The other part was the Base58 encoding scheme for WIF and bitcoin addresses. Why to hell didn't Satoshi just use four other characters instead of Il0O and thus could have kept base64 which does not require huge numbers. We will never know. 

However when using big numbers Base58 is quite easily implemented
with modular division and there is no need to use someone else 
guy's not so well documented Base58 implementation. Just use 
BigInteger and add 

	import java.math.*; 

That's actually what I did and for the guys who did not care 
- I'm sorry, but I would have liked it. 

So after so much reading about it , why not take a look and try
it out at 

	https://github.com/MatthiasLiszt/BitCoinOfflineKeyGenerator
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s