android 6.0 javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT

pkucamel picture pkucamel · Aug 19, 2016 · Viewed 14.4k times · Source

this code works well before android 6.0, but get an error on 6.0 if encrypted file size less than about 1k bytes.

public static byte[] decode(byte[] decrypteSrcBuffer) throws Exception {
    Key deskey = null;
    DESedeKeySpec spec = new DESedeKeySpec(mKeyBytes);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);
    Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
    IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
    byte[] decryptData = cipher.doFinal(decrypteSrcBuffer);

    return decryptData;
}

Error info:

javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:568)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:350)
at javax.crypto.Cipher.doFinal(Cipher.java:2056)

Answer