Java Integer to Byte Array

Java integer to byte array

using Java NIO's ByteBuffer is very simple:

byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();

for (byte b : bytes) {
System.out.format("0x%x ", b);
}

output:


0x65 0x10 0xf3 0x29

Convert integer into byte array (Java)

Have a look at the ByteBuffer class.

ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(0xAABBCCDD);

byte[] result = b.array();

Setting the byte order ensures that result[0] == 0xAA, result[1] == 0xBB, result[2] == 0xCC and result[3] == 0xDD.

Or alternatively, you could do it manually:

byte[] toBytes(int i)
{
byte[] result = new byte[4];

result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);

return result;
}

The ByteBuffer class was designed for such dirty hands tasks though. In fact the private java.nio.Bits defines these helper methods that are used by ByteBuffer.putInt():

private static byte int3(int x) { return (byte)(x >> 24); }
private static byte int2(int x) { return (byte)(x >> 16); }
private static byte int1(int x) { return (byte)(x >> 8); }
private static byte int0(int x) { return (byte)(x >> 0); }

Java - Convert int to Byte Array of 4 Bytes?

You can convert yourInt to bytes by using a ByteBuffer like this:

return ByteBuffer.allocate(4).putInt(yourInt).array();

Beware that you might have to think about the byte order when doing so.

Converting integer array to byte array

convert int to byte[].
create this function:

byte[] toBytes(int i)
{
byte[] result = new byte[4];

result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);

return result;
}

Run in a loop on your int[], for each cell call to the function and add result to an array of bytes.

List of Integers into byte array

There is byteValue() method available in Number class. Number is extended by Integer, Double, Float etc.

List<Integer> list = getNumbers();

Iterator<Integer> iterator = list.iterator();

while(iterator.hasNext())
{
Integer i = iterator.next()
byteArray[index] = i.byteValue();
}

You can also use java.nio.MappedByteBuffer class for block copy. see http://docs.oracle.com/javase/7/docs/api/java/nio/MappedByteBuffer.html.

MappedByteBuffer is equivalent to Buffer.BlockCopy() in .NET

Convert byte array to BigInteger incorrect

Lets go through your code and packet stream structure.

You have the following binary stream encoded as a hex string:

C70001A100408031313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131E5AA194C3DBA71F9A38AF85D43C130A462BDCBACF85ED702B5E2FECB47197DAA433E7B5ACF5EF09A07C5DDE79B7A2EED76B91090CB981E13DBE316997FCF5DC51BE40B2D02D912457D97924B94094227F8BC65DD61FE78060B55BEAAA31C64E3B36863B407F7183DABA9D98F0C9061DED36AD16407F70C4925951BAA8807EC95

According to your code:

  • bytes from index 7 to 71 are your exponent bytes,
  • bytes from index 71 till the end of the stream are your modulus bytes.

IIUC, you want to extract the exponent and the modulus from the above byte string.


The issue you are facing stems from the fact that in cryptography, the numbers are typically unsigned integers, with a protocol-defined byte order, whereas Java's BigIntegers are big-endian, signed integers.

In order to convert an arbitrary integer (signed/unsigned, big-endian/little-endian) into a BigInteger, one has to perform some machinations.

In your case, it's enough to simply reverse the byte arrays, as suggested in the comments:

int packageLength = 199;

byte[] response = IOUtils.toByteArray(is, packageLength);

byte[] rsaPubExponentBytes = Arrays.copyOfRange(response, 7, 71);
byte[] rsaModuloBytes = Arrays.copyOfRange(response, 71, packageLength);

// missing step!
reverse(rsaPubExponentBytes);
reverse(rsaModuloBytes);

BigInteger rsaPubExponentNumber = new BigInteger(1, rsaPubExponentBytes);
BigInteger rsaModuloNumber = new BigInteger(1, rsaModuloBytes);
// ...
public static void reverse(byte[] arr) {
for (int i = 0; i < arr.length / 2) {
int tmp = arr[i]
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}

I've ran into a similar issue when working on a project of mine. You might find my library, SRP-6 Variables, helpful:

byte[] stream = new Hex("C70001A100408031313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131313131E5AA194C3DBA71F9A38AF85D43C130A462BDCBACF85ED702B5E2FECB47197DAA433E7B5ACF5EF09A07C5DDE79B7A2EED76B91090CB981E13DBE316997FCF5DC51BE40B2D02D912457D97924B94094227F8BC65DD61FE78060B55BEAAA31C64E3B36863B407F7183DABA9D98F0C9061DED36AD16407F70C4925951BAA8807EC95").asArray();

BigInteger exponent =
new SRP6CustomIntegerVariable(
Bytes.wrapped(Arrays.copyOfRange(stream, 7, 71)),
ByteOrder.LITTLE_ENDIAN
).asNonNegativeBigInteger();
BigInteger modulus =
new SRP6CustomIntegerVariable(
Bytes.wrapped(Arrays.copyOfRange(stream, 71, stream.length)),
ByteOrder.LITTLE_ENDIAN
).asNonNegativeBigInteger();


Related Topics



Leave a reply



Submit