BigInteger to byte [] without twos add-on?

I have a digital signature ( byte[] signedBytes ) in a byte[] array . I need to convert this to BigInteger ( bigSignedBytes ), use the resulting number for something, and then convert back to byte[] convertBackSignedBytes from BigInteger .

 byte[] signedBytes = rsa.sign(); BigInteger bigSigned = new BigInteger(signedBytes); convertBackSignedBytes = bigSigned.toByteArray(); //signedBytes and convertBackSignedBytes differ. 

The problem is that after re-converting from BigInteger back to byte[] convertBackSignedBytes - it is different from my original variable signedBytes . I know that BigInteger.toByteArray() returns two additions of byte[] value - which may be responsible.

So, how do I get the original bytes from BigInteger without the twos add-on?

Someone recommended this:

 byte[] convertBackSignedBytes = bigIntegerValue.toByteArray(); if (convertBackSignedBytes[0] == 0) { byte[] tmp = new byte[convertBackSignedBytes.length - 1]; System.arraycopy(convertBackSignedBytes, 1, tmp, 0, tmp.length); convertBackSignedBytes = tmp; } 

I tried it - it didn’t work. Still returns a different byte [] value from the original signedBytes .

If I try to verify the original signature, it succeeds.

But if I try to verify the use of the converted signature, this will not work. Thus, signedBytes and convertBackSignedBytes no longer match.

Any quick pointers please?

+1
source share
2 answers

If two additions really should be a problem (I'm not sure), you should solve it by always adding 0x00 bytes in front of the original byte array. This way you will always have a positive number.

But this, for example, can be a problem: if the original array starts with bytes 0x00 or 0xFF, some or all of them may disappear during the conversion process.

Therefore, instead of passing an integer, why don't you just pass a string like "0102affe01dead07cafe89babe"? Encoding bytes to and from a hexadecimal string should be simple.

If you absolutely must use a large integer, add 0x01 before encoding (this will still give a positive number) and delete it after decoding.

+1
source

failure may occur due to the fact that the length is different.

 import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class So15045472 { static void print(byte[] bytes) { List<Byte> bs=new ArrayList<Byte>(bytes.length); for(byte b:bytes) bs.add(b); System.out.println(bs); } static boolean compare(byte[] x,byte[] y) { if(x.length!=y.length) { System.out.println("unequal lengths"); return false; } for(int i=0;i<x.length;i++) if(x[i]!=y[i]) return false; return true; } static void check(byte[] bytes) { BigInteger x=new BigInteger(bytes); System.out.println(x); byte[] b=x.toByteArray(); print(bytes); print(b); boolean isEqual=compare(bytes,b); if(!isEqual) System.out.println("compare says "+isEqual); else System.out.println("ok"); } public static void main(String[] args) { byte[] b1=new byte[]{(byte)0x00,(byte)0xff}; check(b1); byte[] b2=new byte[]{(byte)0xff,(byte)0xff}; check(b2); } } 
0
source

Source: https://habr.com/ru/post/949758/


All Articles