Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Crypto Exception

842544Dec 16 2012
Hello All -

When I run the following program, i get the below exception. Any help would be appreciated..


import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class AESDecryption {

/*Algorithm*/
private static final String ALGO = "AES";
/*Padding scheme - CBC with PKCS5Padding - Initialization Vector is used with AES*/
private static final String ALGOParams = "/CBC/PKCS5Padding";
/*Key byte stream*/
private static byte[] keyValue;
/*Initialization Vector*/
private static byte[] iv;


/*
* decrypt method will take a encrypted string, key and IV in string format and
* return the decrypted string.
*/
public static String decrypt(String encryptedData, String keyString, String ivString) throws Exception {

/*convert key and IV in String format to byte array*/
stringKeystoByteArray(keyString, ivString);

/*Generate the key using the keyvalue specified*/
Key key = generateKey();
/*Get Cipher with AES/CBC/PKCS5Padding*/
Cipher c = Cipher.getInstance(ALGO+ALGOParams);
/*Initialize the Cipher*/
c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

/*Decode the encrypted string using the BASE64 Decoder*/
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
/*Decrypt the decoded encrypted string*/
byte[] decValue = c.doFinal(decordedValue);

return new String (decValue);
}

/*
* The stringKeystoByteArray method will take the comma separated, hexadecimal, String representation
* of the key and the IV and convert each of them to byte arrays.
*/
private static void stringKeystoByteArray(String keyString, String ivString){

/*Split the comma separated key and iv*/
String keyArray [] = keyString.split(",");
String ivArray [] = ivString.split(",");

/*instantiate key and iv*/
keyValue = new byte[keyArray.length];
iv = new byte[ivArray.length];

/*convert each hexadecimal string value to a byte and assign to key and iv*/
for (int i=0; i< keyArray.length; i++)
keyValue[i] = (byte)Integer.parseInt(keyArray, 16);
for (int i=0; i< ivArray.length; i++)
iv[i] = (byte)Integer.parseInt(ivArray[i], 16);
}

/*
* Generates the Key for the Cipher
*/
private static Key generateKey() throws Exception {

Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}



Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:45)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:599)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa der.java:58)
Caused by: java.lang.ExceptionInInitializerError
at java.lang.J9VMInternals.initialize(J9VMInternals.java:222)
at javax.crypto.Cipher.getInstance(Unknown Source)
at AESDecryption.decrypt(AESDecryption.java:44)
at AESDecryption.main(AESDecryption.java:96)
... 5 more
Caused by: java.lang.SecurityException: Cannot set up certs for trusted CAs
at javax.crypto.b.<clinit>(Unknown Source)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
... 8 more
Caused by: java.lang.SecurityException: Jurisdiction policy files are not signed by trusted signers!
at javax.crypto.b.a(Unknown Source)
at javax.crypto.b.a(Unknown Source)
at javax.crypto.b.access$600(Unknown Source)
at javax.crypto.b$0.run(Unknown Source)
at java.security.AccessController.doPrivileged(AccessController.java:251 )
... 11 more
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2013
Added on Dec 16 2012
0 comments
1,513 views