String to bytes to Big Int conversion VICE VERSA error

Viewed 265

im trying to convert a hash to a big integer so i can do some calculations with it. Im successful in that but on the other side i need to be able to convert the big integer back to the hash to do verification. But i cant do the conversion back succesfully, below is my code and the output. Please do advise and assist..

This is the code

//convert hash to bytes then to big int to mod pow
BigInteger hashy = new 
BigInteger(hash.getBytes(StandardCharsets.ISO_8859_1));
System.out.println("hashy: " +hashy);
//test if convert back to big int works
String convertedBackHash = (hashy.toByteArray()).toString();                
System.out.println("hash to hashy to hash: " + convertedBackHash);

This is the output

hash: d1bb961ac85f6d9ae66b469b5cabe83f9f88e4ee
hashy: 835979497806227327262557895525398820611883198135120938003873334575862278693562754177889725605221
hash to hashy to hash: [B@60e53b93

Anyone knows what went wrong?

2 Answers
String convertedBackHash = (hashy.toByteArray()).toString();                

You are using Array#toString method which represents array object in hex format.

If you want to convert your bytes to string

String result = new String(hashy.toByteArray());  
System.out.println("hash to hashy to hash: " + result);

For cryptographic functions you generally need to convert the byte array (the hash) to a positive integer and back. This can be done by thinking of the byte array as a constant sized, unsigned, big endian integer.

PKCS#1 specifies this for RSA calculations, for instance, where OS2IP (octet string to integer primitive) converts from a byte array to an integer and I2OSP (integer to octet string primitive) converts back from the integer to an octet string. An octet string is nothing more than a byte array. Neither is particularly tricky to implement, but the fact that Java hasn't got the function build in (hint!) and that Java cannot resize arrays makes I2OSP more tricky than you might expect. Fortunately I already supplied the answer here - it took me some time to find it again.

Now your input hash seems to be hex encoded. This is not what you would want to turn into an integer. You want to turn the bytes represented by the byte array into an integer. So you first need to hex decode the bytes. There are plenty of libraries that implement hex encoding and there are answers here on Stackoverflow as well. Conversely, after I2OSP you want to encode the bytes back to hexadecimals if you want to view them. This way you only have to deal with an integer of half the size as well.

So BigInteger i = os2ip(Hex.decode(hashString)), then do stuff with i and when you've got it back again, String hashString = Hex.encodeToString(i2osp(i)).

Related