无效密钥异常:无法使用Sun JRE解密OpenJDK/JRE加密字符串

3

我使用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在这里做了些什么不同的事情吗? 如果是,我该如何解决这个问题? 如果不是,那么问题是什么?

1个回答

4
我认为您需要安装Java Unlimited JCE扩展。
请下载并安装安全策略文件(取代已安装的文件),并将它们复制到/lib/security目录下的JDK和JRE中。
下载链接在Java Downloads网站上。

非常感谢!在Ubuntu中,将jar文件复制到/usr/lib/jvm/java-6-sun/jre/lib/security目录下就可以了! - Timo Ernst

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接