相关文章推荐
眼睛小的青蛙  ·  vue print.js ...·  6 月前    · 
孤独的脆皮肠  ·  python ...·  8 月前    · 
个性的小虾米  ·  How to check whether ...·  1 年前    · 
不拘小节的咖啡豆  ·  MySQL JSON - 简书·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

I'm storing AES decryption key in AndroidKeyStore to decrypt data from SQLiteDB. Unfortunately, sometimes I get this error (Any android device and any API) rarely.

I would like to know what exactly this error means. javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT .

Where I can find list of these error:* ?

this error is caused by a mismatch between the keys used for encryption and decryption. make sure the keys are the same. I experienced this error after the encryption device was using the default key instead of pulling from server. Manny265 Jan 30, 2019 at 8:16 @Manny265 I have used DefaultDataSourceFactory class provided by exoplayer. Inside Mainfirst below properties, I'm getting for EXT-X-KEY. I think Exoplayer will automatically take the description key and decrypt the content. #EXT-X-KEY: METHOD=AES-128, URI=" d.akamaized.net/th/encrypted/none/d9/p4/serve.key ? aka_me_session_id=A3l1dtjW" #EXTINF:10.000000, Please let me know if i'm missing anything. Thank you Gurmeet Singh Mar 14, 2022 at 15:08

Decryption seems to work differently in Android from default Java Code in backend. In my case I was getting this error cause. Re-encoding the value of IV and SecretKey as UTF-8.

I was converting the KEY and IV like below and I was causing string to re-encoded.

val ivParameterSpec = IvParameterSpec(iv.toByteArray(charset("utf-8")))
val secretKeySpec = SecretKeySpec(key.toByteArray(charset("utf-8")), "AES")

I removed the "UTF-8" part cause it's in default converted as "UTF-8" no need to pass it again.

val ivParameterSpec = IvParameterSpec(iv.toByteArray())
val secretKeySpec = SecretKeySpec(key.toByteArray(), "AES")

(Note: Length of encryptedData is 128. IV and Key lengths are 16. Which are all divisible by 16 as algorithm needed.)

Full-code is below.

 fun decrypt(encryptedData: String, iv: String, key: String): String? {
        try {
            //IV & Key Generation
            val ivParameterSpec = IvParameterSpec(iv.toByteArray())
            val secretKeySpec = SecretKeySpec(key.toByteArray(), "AES")
            val c = Cipher.getInstance("AES/CBC/PKCS5PADDING")
            c.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec)
            val decodedValue = Base64.decode(encryptedData, Base64.URL_SAFE)
            val decValue = c.doFinal(decodedValue)
            return String(decValue)
        } catch (ex: IllegalBlockSizeException) {
            println(ex.message)
        } catch (ex: BadPaddingException) {
            println(ex.message)
        } catch (ex: InvalidKeyException) {
            println(ex.message)
        } catch (ex: NoSuchAlgorithmException) {
            println(ex.message)
        } catch (ex: NoSuchPaddingException) {
            println(ex.message)
        } catch (ex: Exception) {
            println(ex.message)
        return null
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.