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.lang.IllegalStateException: Cipher not initialized

843811Sep 24 2007 — edited Oct 2 2007
We get this error intermittently.

2007-07-27 22:32:05,710 INFO [STDOUT] java.lang.IllegalStateException: Cipher not initialized
2007-07-27 22:32:05,710 INFO [STDOUT] at javax.crypto.Cipher.c(DashoA12275)
2007-07-27 22:32:05,710 DEBUG 2007-07-27 22:32:05,710 INFO [STDOUT] at javax.crypto.Cipher.doFinal(DashoA12275)

We have a browser based application where we encrypt and decrypt certain sensitive data. We use the following two functions to do the same

public static String encryptData(String inputData) {
String output = null;
try {
byte[] data = inputData.getBytes();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(data);
output = Base64.encodeBytes(result, Base64.GZIP | Base64.DONT_BREAK_LINES);
} catch (InvalidKeyException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalBlockSizeException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (BadPaddingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch(Exception e) {
e.printStackTrace();
}
return output;
}

public static String decryptData(String inputData) {
String output = null;
try {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encdata = (byte[])Base64.decode(inputData);
byte[] original = cipher.doFinal(encdata);
output = new String(original);
} catch (InvalidKeyException e) {
log.error("decryptData-->InvalidKeyException Caught");
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalBlockSizeException e) {
log.error("decryptData-->IllegalBlockSizeException Caught");
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (BadPaddingException e) {
log.error("decryptData-->BadPaddingException Caught");
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (Exception e) {
log.error("decryptData-->Exception Caught");
e.printStackTrace();
}
return output;
}

Please let me know, if you have any suggestions

Thanks in Advance,

Devendra
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 26 2007
Added on Sep 24 2007
11 comments
6,461 views