4

I'm working on an integration with a partner that uses Java's BigInteger to generate a byte array as the key for a simple encryption algorithm. I'm trying to find the equivalent .NET code without much luck.

The Java code they're using is:

String keyString = "48B734DA47388C656913C9BF5146E186";
byte key[] = new BigInteger(keyString, 16).toByteArray();

which yields the following byte array:

[72, -73, 52, -38, 71, 56, -116, 101, 105, 19, -55, -65, 81, 70, -31, -122]

This alone is troubling because bytes in .NET range from 0-255 so the negative values are out of range.

The closest .NET code I came up with is:

string keyString = "48B734DA47388C656913C9BF5146E186";
byte[] key = BigInteger.Parse(keyString, NumberStyles.HexNumber).ToByteArray();

which yields the following byte array:

[134, 225, 70, 81, 191, 201, 19, 105, 101, 140, 56, 71, 218, 52, 183, 72]

At this point, I'm thinking a .NET equivalent simply isn't possible - especially because of the negative byte values.

I look forward to everyone's thoughts.

5
  • Try appending .Cast<sbyte>().ToArray(); after ToByteArray(). Should work. Commented May 28, 2013 at 17:30
  • which yields the following byte array - That is not a byte array. In any case, it is a System.SByte[]. Commented May 28, 2013 at 17:31
  • @HighCore The first byte array is a java byte array which are signed values. The second is .Net byte array which are not signed values. Commented May 28, 2013 at 17:32
  • rjygraham, See what this prints var s = String.Join(",", key.Reverse().Select(x => (sbyte)x).ToArray()); Commented May 28, 2013 at 17:34
  • OP, do you have any reason not to use sbyte instead of byte? Commented May 28, 2013 at 17:37

3 Answers 3

7

Using the LINQ method I4V offered in the comments solves it handily.

  sbyte[] key = BigInteger.Parse(keyString, NumberStyles.HexNumber).ToByteArray().Reverse().Select(x => (sbyte)x).ToArray();

That will get you the array you want. .NET uses unsigned bytes by default, whereas Java uses signed bytes by default. They have the same bit representation, so, depending on what you're doing with it, it may not actually matter which one is used, but, if you want a truly equivalent output in .NET, you'll need to use sbytes instead of bytes, as simply using byte means different things between the two languages.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I totally forgot about sbyte...couldn't see the forest for the trees because I was so focused on the Java syntax.
3

When I compare the 2 result from Java :

[72, -73, 52, -38, 71, 56, -116, 101, 105, 19, -55, -65, 81, 70, -31, -122]

from .Net

[134, 225, 70, 81, 191, 201, 19, 105, 101, 140, 56, 71, 218, 52, 183, 72]

I can tell that:

  1. They seem alike - .Net is just in revers order (A result of java's internal representation being Big Endian).
  2. In .Net result, there are no negative numbers.
  3. Comparing the result in reverse order show that -73 is 183 in .Net

So I think we can say that the result are the same, but need to check that the Endianness is the same and that signs are maintained.

Can you show the code that print those result ?

1 Comment

the endianness is for the revers order
2

It looks like the result from .Net is reversed from the java result, but they are the same values. The .Net byte is unsigned however, rather than the java byte which is signed (contains negatives as you noticed). You can just convert the result that you obtained to a signed byte (sbyte) and can reverse the order as well:

sbyte[] finalResult = (sbyte[])(Array)key.Reverse().ToArray();

Results in: [72, -73, 52, -38, 71, 56, -116, 101, 105, 19, -55, -65, 81, 70, -31, -122]

It should be noted that the bit representations are going to be the same, so if you are using it as a key for some encryption and manipulating it on the bit level you may not need to get the actual signed byte (sbyte) but it will likely make it easier to visualize the conversion from java to .Net if you do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.