0

i tried to fix the problem multiple time but i didn't get the proble that produces the exception which is "--------------------Configuration: -------------------- E

xception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 56
    at testing.compressionPBox(testing.java:61)
    at testing.parityDrop(testing.java:39)
    at testing.roundKeyGenerator(testing.java:23)
    at testing.main(testing.java:14)"

simply my program creates an array of 64 length ,then fill it by value from 1 to 64 then use the method roundKeyGenerator(roundArray) ; to executes the method ,when the method executes it creates an array of 56 length then use parityDrop method to return an array of length 56 form the initial array which has length of 64 ,but how to creates and drop some indexes !! simply i creates a predefined table which when i call the table method by index from initially start from 0 the method takes this index and sees what is the value of table_cell[index]=value_X then return value_x to the caller metod and put vaule_X as an index of the intailArray which has the size 64 and takes its value ,then stores it in the returned array depending where did its pointer stop and i think the pointers is the problem i think the second array of 56 length exceed the limit then the exception produced.

   static int [] roundArray=new int [64];    
    public static void main(String [] arg) 
         { 
          for(int i=0;i<roundArray.length;i++)
           roundArray[i]=i+1;
          roundKeyGenerator(roundArray) ;      
         } 



     public static int [] roundKeyGenerator(int [] k) //takes an int array of length 64 repesents the key in binary representation
        {
         int [] key=new int [56];
         key=parityDrop(k); 
         return key;
        }
        public static int [] parityDrop(int [] key)//takes an int array of length 64 repesents the key and return the key in an arrayof length 56 
        {
         int index;
         int [] result=new int[56];
          for(int i=0;i<key.length;i++)
          { 
           index=compressionPBox(i);//return cell value from straightPBoxTable
           result [i]= key[index-1];
          }

         return result;

        }
        public static int compressionPBox(int i){

          int cell[] =new int [56];
          cell[0] =57 ;cell[1] =49 ;cell[2]=41  ;cell[3] =33 ;cell[4] =25 ;cell[5] =17 ;cell[6] =9 ;cell[7] =1;  
          cell[8] =58 ;cell[9] =50 ;cell[10]=42 ;cell[11]=34 ;cell[12]=26 ;cell[13]=18 ;cell[14]=10 ;cell[15]=2;
          cell[16]=59 ;cell[17]=51 ;cell[18]=43 ;cell[19]=35 ;cell[20]=27 ;cell[21]=19 ;cell[22]=11 ;cell[23]=3;
          cell[24]=60 ;cell[25]=52 ;cell[26]=44 ;cell[27]=36 ;cell[28]=63 ;cell[29]=55 ;cell[30]=47 ;cell[31]=39;
          cell[32]=31 ;cell[33]=23 ;cell[34]=15 ;cell[35]=7 ;cell[36]=62 ;cell[37]=54 ;cell[38]=46 ;cell[39]=38;
          cell[40]=30 ;cell[41]=22 ;cell[42]=14 ;cell[43]=6 ;cell[44]=61 ;cell[45]=53 ;cell[46]=45 ;cell[47]=37;
          cell[48]=29 ;cell[49]=21 ;cell[50]=13 ;cell[51]=5 ;cell[52]=28 ;cell[53]=20 ;cell[54]=12 ;cell[55]=4;
         return cell[i];
         }// predefined permutation rule
1
  • What's your question? Format your stack trace. Commented Dec 15, 2010 at 15:43

5 Answers 5

4
  for(int i=0;i<key.length;i++)
  { 
   index=compressionPBox(i);//return cell value from straightPBoxTable
   result [i]= key[index-1];
  }

You can't index a negative array. index = 0 on the first loop. key[-1] is an error.

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

4 Comments

true. But that's another problem that will appear once he fixes the one he reported :)
This in not the answer to the stack trace that he posted.
Index can not be 0, in that code, the minimum value would be cell[7] =1 I think the right answer is Jesper's
@Pau: You're quite right. Can I suggest that using i and index that way really asks to be misread in the way that Falmarri misread it.
2

Your arrays are of different sizes - one with 56 and one with 64. There comes the problem.

Comments

2
package org.ferencszaszdi.sistemaNit;
public class SistemaNit {
     public static void main(String args[]) {
        String nit = "3894140-6";
    String valor[]={};        
    int num= nit.length();
        int num2 = num - 2;
        int numero = num;
        int in = 0;
        int va = 0;
        for(int inicio = 0;inicio<=num;inicio++) {
            valor[in] = nit.substring(in);
            in++;
    }
    int in2 = 0;
    for(int inicio2 = 0;inicio2<=num2;inicio2++) {
        int valor2 = Integer.parseInt(valor[in2]);
            int val = numero * valor2;
            va = va + val;
            numero--;
            in2++;
        }
        String residuo = nit.substring(num);
        int residuo2 = Integer.parseInt(residuo);
        int nume = va%11;
        if (nume == residuo2){
           System.out.println ("El nit es correcto");
        }
        else
        {
           System.out.println ("El nit es incorrecto");
         }
    }
}

Comments

1

Your array roundArray has length 64, but inside the methods roundKeyGenerator, parityDrop and compressionPBox you have arrays of length 56.

You are calling the method parityDrop with k and then loop over it. In parityDrop you call compressionPBox with the variable i, which will be between 0 and 63 (the length of key). But in compressionPBox you're returning cell[i], which will ofcourse fail when i is >= 56.

Note: To solve problems like this, carefully look at the stack trace of the exception. It tells you exactly at what line in what source file the error happens. Go to that spot in your source code and trace back what went wrong. You can also use a debugger to step through the code to follow what happens. IDEs such as Eclipse and NetBeans have very good built-in debuggers.

3 Comments

i am not verey sure ,but i fixed the loop to be i<result.length and when i executed the cod it works and no exceptions but i am not sure about the result i am having do you think it is ok or there is another problem such as i store wrong values token from the table in result
after rereading you good answer i think it will works,because as you said "You are calling the method parityDrop with k and then loop over it. In parityDrop you call compressionPBox with the variable i, which will be between 0 and 63 (the length of key). But in compressionPBox you're returning cell[i], which will ofcourse fail when i is >= 56" but now the loop will loops result.length == 56 times !! am i rigth :/
Remember that an array of length 56 has the indices 0 - 55. So if you index it with 56, you'll get an exception.
0

Your error is in compressionPBox. Here's how it gets there:

  • main calls roundKeyGenerator passing in roundArray (an int[64]) for the k argument.
  • roundKeyGenerator calls parityDrop, passing in k for the key argument.
  • parityDrop loops from 0 (inclusive) to key.length (exclusive), and so will loop from 0..63 (inclusive), calling compressionPBox with each of those values for the i argument.
  • compressionPBox uses i to index into cell, which is an int[56].

...and so that's why your ArrayIndexOutOfBoundsException is reporting an error on index 56. Once i reaches 56, you can't index into cell with that value anymore.

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.