InvalidKeyException:无法使用Sun JRE解密OpenJDK / JRE加密的字符串

时间:2011-09-27 14:51:07

标签: java encryption

我使用OpenJDK JRE加密并将一些数据存储到数据库,并在从数据库检索时使用OpenJDK成功解密。

今天我用Sun的JRE替换了OpenJDK 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在这里有不同的做法吗? 如果是,我该如何解决这个问题? 如果不是,那么问题是什么?

1 个答案:

答案 0 :(得分:4)

我认为你需要Java Unlimited JCE扩展。
下载并安装安全策略文件(替换已安装的文件)并将其复制到/lib/security下的JDK和JRE中。
该链接位于下载网站Java Downloads