1

I have 16 bytes array data and need to convert that into similar output as OpenSSL BN_bin2bn provides. Per OpenSSL documents:

BN_bin2bn() converts the positive integer in big-endian form of length len at s into a BIGNUM and places it in ret. If ret is NULL, a new BIGNUM is created.

I am having hard time to do so. BN_bin2bn uses unsigned long to represent the value but in Java unsigned doesn't support. I tried below mentioned code to implement the same but in this only first 4 bytes of data matches with the bin2bn. Rest of data is different. Only possible reason is that in C unsigned long can store 16 bytes of data but in Java only 8 bytes can be stored.

For 5-8 bytes when i am trying to convert to unsigned then it crosses the 8 byte limit and giving negative value for the long variable.

I also tried to use BigInteger. Result is same. First 4 bytes are matching but after result doesn't match. Any help is much appreciated.. Here is the code:

byte[] arrayValue = hexToByteArray("53ABA93B9131C439D2942C7CBC483FA5"); 
long op1 = 0L; 
int n = 16 , m = 7;
int x = 0;
while (n-- > 0) {
    op1 = (op1 << 8L) | (0xFF & arrayValue[x++]);
    System.out.println(op1);
    if (m-- == 0) {
        op1 = 0L;
    }
}

The output is:

83
21419
5483433  
1403758907
359362280337
91996743766321
23551166404178372
6029098599469663289
210
53908
13800492
3532926076
904429075644
231533843364936
59272663901423679
-3272942114945089627

With Big Integer:

BigInteger op = BigInteger.ZERO;

while (n-- > 0) {
    int temp1 = 0xFF & arrayValue[k++];
    op = op.shiftLeft(8).or(new BigInteger(String.valueOf(temp1)));
    System.out.println(op);
}

The output is:

83
21419
5483433
1403758907
359362280337 
91996743766321
23551166404178372
6029098599469663289
210
53908
13800492
3532926076
904429075644
231533843364936

Output of bin2bn method:

83
21419
5483433
1403758907
2879994769
2839253297
999371204
2435957817
210
53908
13800492
3532926076
2485943484
746372168
2092714047
3158851493
jww
  • 97,681
  • 90
  • 411
  • 885
Tushar Goel
  • 35
  • 2
  • 6
  • Possibly related: [BigInteger to byte array](http://stackoverflow.com/q/4407779) – jww Jun 05 '15 at 18:36
  • Also, `System.out.println(...);` should probably be in hex so you can immediately discover when things go awry in *53ABA93B9131C439D2942C7CBC483FA5*. Also, be sure to test the hex value *0xABC*. You might get unexpected results because it can be interpreted as `0xAB 0x0C` or `0x0A 0xBC`. In a morbid sort of humorous way, the way you handle the odd nibble (one 4-bit nibble) could mean `1 != 0x1` depending on endianess. I just suffered the same in [Crypto++'s `Integer class](http://www.cryptopp.com/wiki/Integer_Patch). – jww Jun 05 '15 at 18:47
  • I have figured out that both are giving same result but in different format. When i called bn2dec() on the big number i received in openssl api then i get same output as Java Big Integer is giving. But i want to get the output in same format as C is giving so that same operation can be performed and final result matches with same format. – Tushar Goel Jun 08 '15 at 09:37

0 Answers0