-1

The below code snippet runs fine if included inside the JSP, but when I try to refer the same code from a class file I get "Bad version number in .class file" exception.

JARS used :

  • UnlimitedJCEPolicyJDK6:local_policy.jar
  • US_export_policy.jar

Java Code to decrypt

 import java.security.Key;
        
        import javax.crypto.Cipher;
        import javax.crypto.spec.SecretKeySpec;
        
        import sun.misc.BASE64Decoder;
        import sun.misc.BASE64Encoder;
        
        public class Decrypt256bit {
        
        
        private static Key key;
        
        private static Cipher cipher;
        
        static {
            key = new SecretKeySpec("P@ssw0Rd!@#**&&&P@ssw0Rd!@#**&&&".getBytes(), "AES");
            try {
                cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING","SunJCE");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public static String encryptData(String plainText) {
            try {
                cipher.init(Cipher.ENCRYPT_MODE, key);
                byte[] encrypted = cipher.doFinal(plainText.getBytes());
                return new BASE64Encoder().encode(encrypted);
            } catch (Exception e) {
        
                throw new IllegalArgumentException(e);
            }
        }
        
        
        //For testing purpose - to be deleted
        public static String decryptData(String encryptedValue) {
            try {
                cipher.init(Cipher.DECRYPT_MODE, key);
                byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
                int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
                System.out.println("Length==="+maxKeyLen);
                return new String(cipher.doFinal(decordedValue));
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }
        
        public static void main(String... a) {
            //String enc = encryptData("TPASU~TPAGU");
            //System.out.println("Encrypted text==="+enc);      
        }
        
        }
        
Community
  • 1
  • 1
Poornahas
  • 41
  • 1
  • 5
  • 1
    clean the project. delete already generated classes. It seems classes compiled in different Java version and running in different Java version – Braj May 26 '14 at 05:25
  • 1
    make sure the version of java you may have provided in JAVA_HOME and the version of java with which you are compiling your servlet are same. – ppuskar May 26 '14 at 05:29

1 Answers1

0

You have compiled the source file with higher version of Java and executing them in the JRE supporting only the lower version. Please ensure that you have compiled with supported version of Java in runtime.

This error comes when you run an incompatible class file in the JRE. say You execute a .class file compiled in Java 7 and executing in JRE 6. But, the reverse is perfectly valid. You can definitely run a .class file compiled in Java 6 and execute in JRE 7 environment.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53