Question: How can I convert a BigInteger in Java to match the Botan BigInt encoding?
I have communication between Java and a C++ application using Botan. Botan has a BigInt class, comparable to BigInteger. However, the encodings differ when converting to a byte array.
In Botan, the BigInt is encoded as follows:
void BigInt::binary_encode(uint8_t output[]) const
{
//bytes just returns the # of bytes, in my case its 32 always
const size_t sig_bytes = bytes();
for(size_t i = 0; i != sig_bytes; ++i)
output[sig_bytes-i-1] = byte_at(i);
}
In Java, its encoded:
public byte[] toByteArray() {
int byteLen = bitLength()/8 + 1;
byte[] byteArray = new byte[byteLen];
for (int i=byteLen-1, bytesCopied=4, nextInt=0, intIndex=0; i >= 0; i--) {
if (bytesCopied == 4) {
nextInt = getInt(intIndex++);
bytesCopied = 1;
} else {
nextInt >>>= 8;
bytesCopied++;
}
byteArray[i] = (byte)nextInt;
}
return byteArray;
}