-1

I want output an array of integers in java which has a index 5120, but in java only up to 256 index , while in C index to 5120 there is output not 0, I tried changing the loop from <255 to <65536 but instead got an error like the title of this question

Code in C :

    int Tbl0[256],Tbl1[256],Tbl2[256],Tbl3[256];
    int i;
    char SBox={ 0xab,0x88 .....};

unsigned char x, y;

for( i = 0; i < 256; i++ )
{
x = (unsigned char) SBox[i]; 

        y = (x << 1 ^ ((x & 0x80) != 0 ? 0x1B : 0x00));
Tbl0[i] = (int) ( x ^ y ) ^
( (int) x << 8 ) ^
( (int) x << 16 ) ^
( (int) y << 24 );

Tbl0[i] &= 0xFFFFFFFF;

    Tbl1[i] = ( ( ( Tbl0[i] << 24 ) & 0xFFFFFFFF ) | ( ( Tbl0[i] & 0xFFFFFFFF ) >>> 8 ) );





}
        printf("%d\n" , Tbl1[(char)257]);

Output :

1154899012

Java :

    int Tbl0[65536],Tbl1[65536],Tbl2[65536],Tbl3[65536];
        int i;
        char SBox={ 0xab,0x88 .....};
    
    char x;
byte y;
    
    for( i = 0; i < 256; i++ )
    {
    x = (char) SBox[i]; 
    
            y = (byte)(x << 1 ^ ((x & 0x80) != 0 ? 0x1B : 0x00));
    Tbl0[i] = (int) ( x ^ y ) ^
    ( (int) x << 8 ) ^
    ( (int) x << 16 ) ^
    ( (int) y << 24 );
    
    Tbl0[i] &= 0xFFFFFFFF;
    
        Tbl1[i] = ( ( ( Tbl0[i] << 24 ) & 0xFFFFFFFF ) | ( ( Tbl0[i] & 0xFFFFFFFF ) >>> 8 ) );
    
    
    
    
    }

        System.out.println(Tbl1[(char)257]);

Output :

0

I tried to print Tb11 [255] and the results are the same as those in C but after index > 256 the results are always 0, I tried changing the looping to :

for( i = 0; i< 65536; i++)

but I got an error : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 256 out of bounds for length 256

Atrs
  • 1
  • 2
  • The C code's `printf("%d\n" , Tbl1[257])` is *undefined behaviour*. How do expect to replicate the output `1154899012` in java? Also, in the C code `Tbl0[i] &= 0xFFFFFFFF;` does nothing useful. – Weather Vane Jul 22 '20 at 15:09
  • It is unclear how big the `SBox` array is but that may be your problem when you change your loop. – WJS Jul 22 '20 at 15:11
  • @WeatherVane sorry, its Tb11[(char) 257 ] not Tb11[257] – Atrs Jul 22 '20 at 15:16
  • @WJS its 256 in c , char sbox[256] – Atrs Jul 22 '20 at 15:16
  • @WeatherVane but why in c there a result and in java no? i confuse.. – Atrs Jul 22 '20 at 15:18
  • 2
    It is unlikely that people will want to read much of the code when it contains typos, the dialog going 'What about this' - 'that is a typo' and 'what about that' - 'that is another typo.' It's best to copy/paste *actual code*, and preferably the [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) with complete code that demonstrates the problem. – Weather Vane Jul 22 '20 at 15:27
  • Please show properly formatted code. Nobody likes to read poorly formatted code and if people don't read your code you get fewer answers, or none at all. – Jabberwocky Jul 22 '20 at 15:29
  • Your error is shown when accessing index 256 of a 256-length array, because Java arrays start at zero. – NomadMaker Jul 22 '20 at 15:34

1 Answers1

0

A few things.

  • You are only indexing up to 255 in your for loop so there will be nothing in any of the tables at 257.
  • When you change your for loop to 65536 your SBox will throw an exception because it is only of size 256.
  • array indices in Java are integers so why are you casting one to a char?
  • Your tables are not properly declared for Java so everything else is suspect.
WJS
  • 36,363
  • 4
  • 24
  • 39