我使用OpenJDK JRE加密并储存在数据库中的一些数据,并在从数据库中检索时成功地使用OpenJDK解密。
今天,我将OpenJDK JRE替换为Sun的JRE,现在尝试解密旧的(OpenJDK加密的)数据时出现以下异常:
java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
异常出现在第14行:
// Decrypts the given ciphertext with the given password
public String decrypt(String ciphertext, String password)
throws FailedCryptOperationException {
String plaintext = "";
byte[] ciphertext_bytes = decode(ciphertext);
try {
byte[] salt = decode(SALT_BASE64);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); //$NON-NLS-1$
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 1024, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secret);
plaintext = new String(cipher.doFinal(ciphertext_bytes), TEXT_FORMAT);
} catch (Exception e) {
throw new FailedCryptOperationException(e);
}
return plaintext;
}
// Does Base64 decoding
public byte[] decode(String text) throws FailedCryptOperationException {
byte[] res;
BASE64Decoder decoder = new BASE64Decoder();
try {
res = decoder.decodeBuffer(text);
} catch (IOException e) {
throw new FailedCryptOperationException(e);
}
return res;
}
在这行代码中:
cipher.init(Cipher.DECRYPT_MODE, secret);
原始的Sun JRE在这里做了些什么不同的事情吗? 如果是,我该如何解决这个问题? 如果不是,那么问题是什么?