Question

i get this error message when i try to break in 2 parts (with "break point" the character "@")

  • part_fixed
  • part_user

    a secret key "final key" saved as string.

           service_codeEncrypted_key = new SecretKeySpec(service_codeEncrypted, 0, 8, "DES");
    
    
            System.out.println("");
            System.out.println("Secret code as secret key :" + service_codeEncrypted_key);
            System.out.println("");
            String final_key = "service_codeEncrypted_key";//key has the form xxx.xxx.xxx.xxx@yyyyy
            String[] parts = final_key.split("@") ;
            String part_fixed = parts[0]; // xxx.xxx.xxx.xxx
            String part_user = parts[1]; // yyyy
            System.out.println("");             
            System.out.println("Service Code decrypted : " + new String(service_codeDecrypted)); // Print the decrypted Text
            System.out.println("");
            // System.out.println("Code for the card : " + part_user); // Print the decrypted Text          
            System.out.println("Finish!!!"); // Print the decrypted Text
            System.out.println("-----------------------------------------------------------------------------------"); // Print the decrypted Text
            System.out.println("");
            System.out.println("");
    

The error as shown in the console in the following :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at JEncrytion.main(JEncrytion.java:182)

where line 182 is : String part_fixed = parts[0];

The acual key has the form xxx.xxx.xxxxxx.xxx@yyyyy, and i want all the x's in one part and all the y in the second one.

Any ideas?

Was it helpful?

Solution

String final_key = "service_codeEncrypted_key"; // here is the problem

Replace with the following

String final_key = service_codeEncrypted_key; 

and consider this one too

   String part_fixed ="";
   String part_user ="";
   if(parts!=null){
       part_fixed = parts[0]; // xxx.xxx.xxx.xxx
       part_user = parts[1]; // yyyy
   }

OTHER TIPS

final_key (value = "service_codeEncrypted_key") string does not contain @ and hence the result of split will be an array of single element i.e the original string. So accesing the second element in that array as mentioned here:

parts[1]; 

will throw an ArrayIndexOutOfBoundsException

The logical problem in your code is that you wanted to assign the service_codeEncrypted_key variable to final_key but instead you did the string assignment. So as mentioned by Prabhakaran in his answer, replace this:

String final_key = "service_codeEncrypted_key";

with

String final_key = service_codeEncrypted_key;

That is right your key doesn't contain '@' symbol.

You need to code something like this.

if(final_key.contains("@")){
String[] parts = final_key.split("@") ;
    String part_fixed = parts[0]; // xxx.xxx.xxx.xxx
    String part_user = parts[1]; // yyyy
}
String final_key = "service_codeEncrypted_key";

doesn't contain the @ character and so the parts[] has no element.

Try to use

String[] parts = service_codeEncrypted_key.split("@");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top