Reputation: 4501
I have a function which takes as input a long value and returns a long value.
static long MyBox(long S) // input is a 48-bit integer stored in 64-bit signed "long"
{
// Split I into eight 6-bit chunks
int Sa=(int)((S>>42));
int Sb=(int)((S>>36)&63);
int Sc=(int)((S>>30)&63);
int Sd=(int)((S>>24)&63);
int Se=(int)((S>>18)&63);
int Sf=(int)((S>>12)&63);
int Sg=(int)((S>>6)&63);
int Sh=(int)(S&63);
// Apply the S-boxes
byte Oa=S1Table[Sa];
byte Ob=S2Table[Sb];
byte Oc=S3Table[Sc];
byte Od=S4Table[Sd];
byte Oe=S5Table[Se];
byte Of=S6Table[Sf];
byte Og=S7Table[Sg];
byte Oh=S8Table[Sh];
// Combine answers into 32-bit output stored in 64-bit signed "long"
long O=(long)Oa<<28 | (long)Ob<<24 | (long)Oc<<20 | (long)Od<<16 | (long)Oe<<12 | (long)Of<<8 | (long)Og<<4 | (long)Oh;
return(O);
}
I pass a variable to this method however i get ArrayIndexOutOfBoundsException. Would anyone know why? This is how i pass it.
long testPassing=0x1234567887654321L;
long result = MyBox (testPassing);
and after this step I get an arrayoutofbounds exception.
Any help would be appreciated!! Thank You.
Upvotes: 0
Views: 241
Reputation: 79838
Your problem is the value you're passing into the method. long testPassing=0x1234567887654321L;
is bigger than a 48 bit integer - it's actually 61 significant bits. So when you write int Sa=(int)((S>>42));
you get something with 19 significant bits. But you're using that to index your array, which presumably has only 64 entries, because (according to the comments in your code), you're looking up 6-bit chunks in it.
If you want to ignore the first 16 bits of your long
, you could write int Sa=(int)((S>>42)&63);
which effectively throws away everything other than the 6 bits that you're trying to isolate.
Upvotes: 1
Reputation: 4501
The problem was that the long values i initialised were longer than 16.
Upvotes: 0
Reputation: 44834
So you have eight arrays
byte Oa=S1Table[Sa];
byte Ob=S2Table[Sb];
byte Oc=S3Table[Sc];
byte Od=S4Table[Sd];
byte Oe=S5Table[Se];
byte Of=S6Table[Sf];
byte Og=S7Table[Sg];
byte Oh=S8Table[Sh];
what is the value of Sh, ensure that your S8Table (and others) are this size - I bet they are not.
Upvotes: 0