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();
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?
source share