Search code examples
javabyteunsigned

How to pass unsigned bytes to SecretKeySpec()?


I need to use Java's SecretKeySpec() with an array of numbers greater than 127.

If I do the following:

new byte[] { (byte)0xD9, (byte)0xDF, (byte)0x85 }

the numbers get converted to bytes and they'll be negative.

Of course I can & them with 0xFF and save them in a short array and save their value, but then I won't be able to pass this array to SecretKeySpec(), because it receives only a bytes[] array.

Any ideas?


Solution

  • Seems like there's no need to do that. I do write signed values to byte[] array in Java, and do read them as unsigned in C# (e.g Java's -16 reads 240 in C#). But the decryption still works. Seems like this is taken care of in the Rijndael algorithm.