ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

java.lang.ArrayStoreException in java program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.lang.ArrayStoreException in java program

    Hello,

    Well Ive tried everything I know.

    I am passing an encrypted encoded url to a java program to have it decrypted decoded.

    I am passing only the string of the above mentioned URL.

    I have tried many versions of constructors/main methods and such. But, it's rejecting everything with the above error. I have a test program that does the same thing as my JNI only it has a main method to be called from the command line and it works correctly.

    Am I defining the constructor correctly to receive the parm?IS my RPG sending the parm correctly

    My jni program


    Code:
    D new_AES         PR                  like(AES)          
    D                                           ExtProc(*java:     
    D                                          'pashaAES':        
    D                                          *CONSTRUCTOR)      
    D AESString                           like(jString)
    
    D decrypt         PR                  like(jString)     
    D                                     extproc(*java:    
    D                                     'pashaAES':       
    D                                     'decrypt')        
    D argBytes                            like(jString)          
    
    eval      xString = new_String(%trim(SCENCODED))
    eval      AES = new_AES(xString)                    
    eval      xString = new_String(%trim(SCENCODED))    
    eval      yString = decrypt(AES:xString)    // returns    java.lang.ArrayStoreException in java program
    My java program

    Code:
    public class pashaAES  {
    
    
     public pashaAES(String input) throws Exception {
    	
     }
      
     public String decrypt(String input) throws Exception{
    	 
    	String IV = "my secret code";
    	String Key = "my super secret code";
    		
    	String input2 = input.trim();
    	
    	String decodedUrl = URLDecoder.decode(input2, "UTF-8");
    	
    	byte[] decodedIV = Base64.decodeBase64(IV);
    	byte[] decodedKey = Base64.decodeBase64(Key);  
    	byte[] decodedinput =  Base64.decodeBase64(decodedUrl);
    	
    	
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IBMJCE");
        SecretKeySpec key = new SecretKeySpec(decodedKey, "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(decodedIV));
        System.out.println("cipher = " + new String(cipher.doFinal(decodedinput),"UTF-8"));
        return new String(cipher.doFinal(decodedinput),"UTF-8");
      }
    
    }

  • #2
    Re: java.lang.ArrayStoreException in java program

    Disclaimer - not familiar with calling java from RPG but a couple of things I notice.
    In the RPG you are passing two parameters to decrypt() but it has only one defined, which one should it be getting.
    In the RPG what value does AES have after calling new_AES() - it looks like to me that it may be blank because the constructor in the Java doesn't do anything.

    Just a WAG but what happens if you do yString = decrypt(xString);

    Comment


    • #3
      Re: java.lang.ArrayStoreException in java program

      Thank You very much for your reply.

      There are two in the RPG prototype.

      The java program accepts an input parm and returns a value, so I have to have two parms in the prototype. I believe that when it returns the value back to the RPG program is where my error is occuring.

      I dont understand how it's returning this message.

      Comment


      • #4
        Re: java.lang.ArrayStoreException in java program

        The ArrayStoreException is a Java Exception (not an RPG error) that means that the code is trying to add an object of the wrong type to the array.

        I suspect the error is in the Java code somewhere... but all we know right now is that it's occurring during the call to 'decrypt'. Can you step through the Java code and determine which line of the Java code it's occuring in?

        Comment


        • #5
          Re: java.lang.ArrayStoreException in java program

          You can get an exception trace for a failed Java call if you addenvvar QIBM_RPG_JAVA_EXCP_TRACE with the value 'Y'. The trace will show the Java stack at the time of the exception. The trace output will go to stderr.

          For example, I called the decode() method in the Integer class with a bad integer value ('abc'), and got this exception trace, showing that decode() called valueOf() which called parseInt() which threw the NumberFormatException:
          Code:
          Exception in thread "main" java.lang.NumberFormatException: For input string: 
           at java.lang.NumberFormatException.forInputString(NumberFormatException.java:
           at java.lang.Integer.parseInt(Integer.java:460)                              
           at java.lang.Integer.valueOf(Integer.java:539)                               
           at java.lang.Integer.decode(Integer.java:969)
          An easy way to see the stderr output is to submit the program call to batch. Just add the ENVVAR to your interactive job before you submit the call, and specify CPYENVVAR(*YES) on the SBMJOB.

          Comment


          • #6
            Re: java.lang.ArrayStoreException in java program

            Scott M, the RPG syntax for calling an instance method is to pass the instance as the first parameter. So the RPG call decrypt(AES:xstring) is equivalent to the Java call AES.decrypt(xstring).

            Comment


            • #7
              Re: java.lang.ArrayStoreException in java program

              I have that in my envvar, but it doesnt generate anything.

              QIBM_RPG_JAVA_EXCP_TRACE 'Y'


              Environment variable . . . . . . > 'QIBM_RPG_JAVA_PROPERTIES'
              New value . . . . . . . . . . . '-Djava.version=1.6;-Dos400.run.verbose=true
              ;-Dos400.stdout=file:/ty/stdout.log;-Dos400.stderr=file:/ty/stderr.log;'

              How can I debug the java program from the i5 debugger?

              Comment


              • #8
                Re: java.lang.ArrayStoreException in java program

                I just have to say I dont know what the problem is. I can say though, that there are NOT enough java debugging tools on the i5 to be able to resolve the issue. You cannot see the value of string variables or anything. I am basically shooting in the dark.

                IBM needs to develop an effective debugging tool to be able to work out these issues. What we do have is quite ineffective. Im going to continue to work this out, but not with the help of IBM

                Comment


                • #9
                  Re: java.lang.ArrayStoreException in java program

                  Originally posted by davisty
                  Hello,

                  I have a test program that does the same thing as my JNI only it has a main method to be called from the command line and it works correctly.
                  Hi
                  It looks like you are going to store the wrong type of object into an array of objects.
                  Would you mind to show your working test program?
                  is your test program working on i5 environtment or windows?
                  Thanks

                  Comment


                  • #10
                    Re: java.lang.ArrayStoreException in java program

                    Thank You very much for your reply

                    Im only using a byte arrays in the program. Im using that for base64 and decrypting

                    Im not using an array[] in the program at all.

                    This program has been modified many time ...It's run on the i5.

                    import java.security.MessageDigest;
                    import java.util.Arrays;
                    import javax.crypto.KeyGenerator;
                    import javax.crypto.SecretKey;
                    import javax.crypto.spec.SecretKeySpec;
                    import javax.crypto.spec.IvParameterSpec;

                    import javax.crypto.Cipher;
                    import javax.crypto.spec.IvParameterSpec;
                    import javax.crypto.spec.SecretKeySpec;
                    import java.util.Arrays;
                    import org.apache.commons.codec.binary.Base64;


                    public class newAES10 {
                    static String IV = "my super secret code";
                    static String Key = "my super duper secret key";
                    static String plaintext = "==Roll Tide==";

                    static String ST = "MsUoMDbN7pk6loNoqtBH%2bOf8A9WvV2YJPytgKA2KT8GOrj% 2bZGGhRGAK0PHa9m%2bKOAgQe%2bRCMqtD4Fkj6xUTbRHtnUe6 n3QZMwjkFACvErhw%3d";

                    static String ST1 = "MsUoMDbN7pk6loNoqtBH%2bOf8A9WvV2YJPytgKA2KT8ExpIB K75f2Gdp3%2fUImtZhGsgjIh%2fS7NcrBbqJF%2fpgpTxCAp8Z RPuYKTYGsXKRo8rA%3d";

                    static String ST2 = "MsUoMDbN7pk6loNoqtBH%2bOf8A9WvV2YJPytgKA2KT8ERE90 jmtnRv5GOPUv4Cp19MWOvZDBY67Vi8rbRmPdCijYdoy74rfQxy PHCsykZQG45dQh4LAii5kqJDClpxE0G";



                    static byte[] decodedIV;
                    static byte[] decodedKey;
                    static byte[] decodedST;
                    private static String input;
                    private static String input1;
                    private static String input2;

                    public static void main(String [] args) {

                    decodedIV = Base64.decodeBase64(IV);

                    decodedKey = Base64.decodeBase64(Key);

                    try{
                    input = java.net.URLDecoder.decode(ST, "UTF-8");
                    } catch (Exception e) {
                    System.out.println("Error in Decoding URL = " + ST);
                    }

                    try{
                    input1 = java.net.URLDecoder.decode(ST1, "UTF-8");
                    } catch (Exception e) {
                    System.out.println("Error in Decoding URL = " + ST1);
                    }

                    try{
                    input2 = java.net.URLDecoder.decode(ST2, "UTF-8");
                    } catch (Exception e) {
                    System.out.println("Error in Decoding URL = " + ST2);
                    }

                    byte[] decodedST = Base64.decodeBase64(input);
                    byte[] decodedST1 = Base64.decodeBase64(input1);
                    byte[] decodedST2 = Base64.decodeBase64(input2);



                    System.out.println("===== Decoded Strings =====");
                    System.out.println("Original String IV: " + IV );
                    System.out.println("Original String Key: " + Key);
                    System.out.println("Original String ST: " + ST);
                    System.out.println(" ");

                    System.out.println("decodedIV String : " + decodedIV.toString());
                    System.out.println("decodedKey String : " + decodedKey.toString());
                    System.out.println("decodedKey String : " + decodedST.toString());
                    System.out.println("decodedKey String : " + decodedST1.toString());
                    System.out.println("decodedKey String : " + decodedST2.toString());

                    try {
                    System.out.println("==Java==");
                    System.out.println("plain: " + plaintext);

                    byte[] cipher = encrypt(plaintext, decodedKey);


                    System.out.print("cipher: ");
                    for (int i=0; i<cipher.length; i++)
                    System.out.print(new Integer(cipher[i])+" ");
                    System.out.println("");

                    String decrypted = decrypt(cipher, decodedKey);

                    System.out.println("decrypt: " + decrypted);


                    // decode security token
                    String decrypted1 = decrypt1(decodedST, decodedKey);
                    System.out.println("decrypt1: " + decrypted1);

                    String decrypted2 = decrypt1(decodedST1, decodedKey);
                    System.out.println("decrypt2: " + decrypted2);

                    String decrypted3 = decrypt1(decodedST2, decodedKey);
                    System.out.println("decrypt3: " + decrypted3);

                    String decrypted4 = decrypt2(input2, decodedKey);
                    System.out.println("decrypt4: " + decrypted4);




                    } catch (Exception e) {
                    e.printStackTrace();
                    }
                    }
                    public static byte[] encrypt(String plainText, byte[] Key) throws Exception {
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IBMJCE");
                    SecretKeySpec key = new SecretKeySpec(decodedKey, "AES");
                    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(decodedIV));
                    return cipher.doFinal(plainText.getBytes("UTF-8"));
                    }

                    public static String decrypt(byte[] cipherText, byte[] Key) throws Exception{
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IBMJCE");
                    SecretKeySpec key = new SecretKeySpec(Key, "AES");
                    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(decodedIV));
                    return new String(cipher.doFinal(cipherText),"UTF-8");
                    }

                    public static String decrypt1(byte[] decoded, byte[] Key) throws Exception{
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IBMJCE");
                    SecretKeySpec key = new SecretKeySpec(Key, "AES");
                    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(decodedIV));
                    return new String(cipher.doFinal(decoded),"UTF-8");
                    }
                    public static String decrypt2(String input, byte[] Key) throws Exception{
                    byte[] decoded = Base64.decodeBase64(input);
                    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "IBMJCE");
                    SecretKeySpec key = new SecretKeySpec(Key, "AES");
                    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(decodedIV));
                    return new String(cipher.doFinal(decoded),"UTF-8");
                    }


                    }
                    Last edited by davisty; July 7, 2014, 10:48 AM.

                    Comment


                    • #11
                      Re: java.lang.ArrayStoreException in java program

                      Originally posted by davisty

                      Code:
                      D new_AES         PR                  like(AES)          
                      D                                           ExtProc(*java:     
                      D                                          'pashaAES':        
                      D                                          *CONSTRUCTOR)      
                      D AESString                           like(jString)
                      
                      D decrypt         PR                  like(jString)     
                      D                                     extproc(*java:    
                      D                                     'pashaAES':       
                      D                                     'decrypt')        
                      D argBytes                            like(jString)          
                      
                      eval      xString = new_String(%trim(SCENCODED))
                      eval      AES = new_AES(xString)                    
                      eval      xString = new_String(%trim(SCENCODED))    
                      eval      yString = decrypt(AES:xString)    // returns    java.lang.ArrayStoreException in java program
                      Hi , How did you assign the value to SCENCODED variable?
                      are you passing value from command line?

                      Comment


                      • #12
                        Re: java.lang.ArrayStoreException in java program

                        The JNI program is a CGIDEV2 program. I am getting encrypted links that I need to decrypt, in order to pass the program the correct parms I am calling the decrypt method in my RPG program

                        The scencoded field is a link Im reading in from a webpage.

                        Comment


                        • #13
                          Re: java.lang.ArrayStoreException in java program

                          I found my problem,
                          But I dont know how to prototype this:

                          public static void main(String[] args) throws Exception {
                          pashaAES AES = new pashaAES();
                          AES.decrypt(args[0]);
                          }

                          Comment


                          • #14
                            Re: java.lang.ArrayStoreException in java program

                            What problem did you find?

                            The RPG code from your initial post already looks basically the same as the Java main.

                            Can you show the prototype for new_String?

                            Comment


                            • #15
                              Re: java.lang.ArrayStoreException in java program

                              Thank You Barbara for all your help

                              I needed to have a "driving" java class that would supply the values to my "getter" class.

                              My original program was not supplying the correct values to my getter method.

                              Took me forever to figure this out ...

                              Comment

                              Working...
                              X