加密代码在android 4.2以后不起作用

时间:2013-12-21 23:57:54

标签: android encryption

我在我的应用中使用以下代码来加密和解密密码。它工作正常,直到我将操作系统从4.1升级到4.3。不知道为什么现在失败了。我将先前加密的密码(在4.1中)传递给下面的解密代码。

我的解密代码

String keyword  = "keyword";
    int iterationCount = 1000; 
    int keyLength = 256;
    String[] fields = encryptedPassword.split("]");
    byte[] salt = fromBase64(fields[0]);
    byte[] cipherBytes = fromBase64(fields[1]);
    KeySpec keySpec = new PBEKeySpec(keyword.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterationCount);
    cipher.init(Cipher.DECRYPT_MODE, key, pbeSpec);
    byte[] plainBytes = cipher.doFinal(cipherBytes);
    plainStr = new String(plainBytes, "UTF-8").trim();
    return plainStr;

例外情况如下

javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

0 个答案:

没有答案