开发者

InvalidKeyException: Cannot decrypt OpenJDK/JRE-encrypted string with Sun JRE

I encrypted and stored some data to a db using the OpenJDK JRE and also successfully decrypted with OpenJDK when retrieving back from db.

Today I replaced the OpenJDK JRE with Sun's JRE and now I get the following exception when I try to decrypt the old (OpenJDK-encrypted) data:

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*..)

The exception occurs here in line 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;
    BASE64De开发者_如何学JAVAcoder       decoder         = new BASE64Decoder();
    try {
        res = decoder.decodeBuffer(text);
    } catch (IOException e) {
        throw new FailedCryptOperationException(e);
    }
    return res;
}

In this line:

cipher.init(Cipher.DECRYPT_MODE, secret);

Does the original Sun JRE do something differently here? If yes, how can I work around this? If no, what's the problem then?


I think you need the Java Unlimited JCE extensions.
Download and install the security policy files (replacing the the ones installed) and copy them in your JDK and JRE under /lib/security.
The link is in the download site Java Downloads

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜