back to listing index

java - Convert arbitrary size of byte[] to BigInteger[] and then safely convert back to exactly the same byte[], any clues? - Stack Overflow

[web search]
Original source (stackoverflow.com)
Clipped on: 2012-08-03

I believe conversion exactly to BigInteger[] would be optimal in my case. Anyone had done or found this written in Java and willing to share?

So imagine I have arbitrary size byte[] = {0xff,0x3e,0x12,0x45,0x1d,0x11,0x2a,0x80,0x81,0x45,0x1d,0x11,0x2a,0x80,0x81} How do I convert it to array of BigInteger's and then be able to recover it back the original byte array safely?

ty in advance.

Image (Asset 1/3) alt=58.6k976149
asked Mar 24 '10 at 12:15
Image (Asset 2/3) alt=17912

69% accept rate
1
 
Do you want to convert each byte to a distinct BigInteger, or ... ? – Péter Török Mar 24 '10 at 12:22
 
 
Does that byte array actually represent a big integer number or is it just any random amount of bytes? If it's the latter, then converting it to a BigInteger is A Bad Thing™. – Joachim Sauer Mar 24 '10 at 12:23
 
 
------------ Peter, I dont want to convert any single byte to single BigInteger, coz it sounds a bit overutilizing. ------------ Joachim, I need it as BigInteger, because i need to use BigInteger's convinient methods. – PatlaDJ Mar 24 '10 at 12:32
 
 
@PatlaDJ: which convenience methods do you want to use? – Joachim Sauer Mar 24 '10 at 12:39
 
 
BigInteger modPow(BigInteger exponent, BigInteger m) is the method is want to use. I wrote my own simple implementation of the RSA algorithm, and I found BigInteger the most convinient type to use for this. Correct me if I'm wrong? – PatlaDJ Mar 24 '10 at 12:47
add / show 2 more comments

1 Answer

up vote 5 down vote accepted

Use BigInteger.toByteArray() and BigInteger(byte[]).

According to the javadoc, the latter ...

Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger. The input array is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.

If your byte-wise representation is different, you may need to apply some extra transformations.

EDIT - if you need to preserve leading (i.e. non-significant) zeros, do the following:

  1. When you convert from the byte array to a BigInteger, also make a note of the size of the byte array. This information is not encoded in the BigInteger value.

  2. When you convert from the BigInteger to a byte array, sign-extend the byte array out to the same length as the original byte array.

EDIT 2 - if you want to turn a byte array into an array of BigIntegers with at most N bytes in each one, you need to create a temporary array of size N, repeatedly 1) fill it with bytes from the input byte array (with left padding at the end) and 2) use it to create BigInteger values using the constructor above. Maybe 20 lines of code?

But I'm frankly baffled that you would (apparently) pick a value for N based on memory usage rather than based on the mathematical algorithm you are trying to implement.

answered Mar 24 '10 at 12:23
Image (Asset 3/3) alt=130k549173
 
 
Yes, it's different I have arbitrary byte array length, and that is a kind of mess. Seems like I really have to write those extra transformations my own. I didn't really found them ready written anywhere with google for this particular case :( TY – PatlaDJ Mar 24 '10 at 12:35
 
 
If the byte-array is arbitrary data (which I'd guess it is), then this is definitely not a good idea. Leading 0x00 bytes (or 0xFF bytes, if the entire value is negative) will be silently discarded when doing that. – Joachim Sauer Mar 24 '10 at 12:35
1
 
@PatiaDJ - I don't understand your point. Those methods work with arbitrary length byte arrays!! – Stephen C Mar 24 '10 at 12:45
 
 
Stephen, BigInteger(byte[]) will not create an array of BigIntegers. Which methods you refer, or I'm missing something here ? – PatlaDJ Mar 24 '10 at 12:55
2
 
So how is the conversion supposed to know how many bytes to load into each BigInteger? – Stephen C Mar 24 '10 at 13:22
add / show 7 more comments

Your Answer

 
community wiki

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