0

I am developing an app for Android. In this app, you must use the algorithm for encryption / decryption Rijndael.

To use this algorithm, I need to mount the IVs. In the case is an array of bytes. So far so good.

The problem is that I have to use the same IVs from a Web Service written in C #.

C # is thus:

byte[] bytIV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62 };

When I try to do the same in Java, an error occurs and I suggested adding a cast a few numbers. What makes the code this way:

byte[] bytIV = { 121, (byte) 241, 10, 1, (byte) 132, 74, 11, 39, (byte) 255, 91, 45, 78, 14, (byte) 211, 22, 62 };

Still did not work.

Does anyone have a suggestion of what I do?

Thanks!


In the example that I spent here, I used (cast) instead of (byte). Example corrected. Note: There was (cast) in my code. In the code was (byte).

Taynã Bonaldo
  • 621
  • 9
  • 17
  • First of all, please accept answers or followup on your questions. Secondly, the values within the IV should be identical to the ones in C# now, so the problem is probably elsewhere. So the next question becomes - and this is the all time obvious one - what does "Still did not work." mean? Exceptions? Bad decryptions? Burned down CPU? Flying pigs? – Maarten Bodewes Jul 30 '12 at 00:17

3 Answers3

0

byte is 8 bit signed. You are setting on the array values larger than that. 241 for example. That's why you need to add the cast, and the result making a simple test is:

byte b2 = (byte) 241;           
System.out.println("" + b2);

output = -15

hence your problems

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
0

Byte is 8-bit signed, from -128 to 127. So casting 241 will give -15, which are the same bits as 241 for an unsigned byte in C#. I don't see the problem. But casting to byte is with (byte)241, not (cast)241.

Ilod
  • 146
  • 2
0

In Java a byte has a range that it can hold between -128 to 127. Any number larger than 128 or lower than -127 cant fit into the byte which is why its asking for a cast. In Java you add a cast like this (int) or (double) will cast the number to either an int or a double respectively. To fix your problem you have to change the array to a short or int array instead of a byte array. Then remove the casts that are in the declaration. Either of these below should work for you.

int[] bytIV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 
                             91, 45, 78, 14, 211, 22, 62 };

or

short[] bytIV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 
                             91, 45, 78, 14, 211, 22, 62 };
Jason Crosby
  • 3,533
  • 4
  • 28
  • 49
  • Excuse me, do not think I quite understand your example. Should I use an array of int or short in place of the array of bytes? That's it? – Taynã Bonaldo Jul 23 '12 at 15:03
  • Hi Jason, The problem is that soon after creating this array of bytes, I have to pass it as parameter to a variable of type IvParameterSpec, which only accept parameters of type byte []. – Taynã Bonaldo Jul 23 '12 at 15:36
  • You cant reliably cast a number larger than 128 into a byte it will loose accuracy. Where are the numbers in the array coming from? Is there any chance that other numbers can be used? – Jason Crosby Jul 23 '12 at 19:43