Skip to content

For some device the init Cipher doesn't work (API 23) #18

@jordizspmobile

Description

@jordizspmobile

In some devices the init Cipher doesn't work you need change your code to:

Kotlin code:

private fun initCipher() : Boolean {
        try {
            cipher = Cipher.getInstance(
                KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7
            )
        } catch (e:NoSuchAlgorithmException) {
            throw RuntimeException("Failed to get Cipher", e)
        } catch (e: NoSuchPaddingException) {
            throw RuntimeException("Failed to get Cipher", e)
        }

        return try {
            keyStore?.load(null)
            sharedPreferences.getString("biometric_param", null)?.let {
                val key = keyStore?.getKey(KEY_NAME, null) as SecretKey
                if (it.isNotEmpty()) {
                    val arraysBytes = Base64.decode(it, Base64.NO_WRAP)
                    val ivParams = IvParameterSpec(arraysBytes)
                    cipher?.init(mode, key, ivParams)
                }
            } ?: run {
                cipher?.init(Cipher.ENCRYPT_MODE, generateKey())
                val ivParams = cipher?.parameters?.getParameterSpec(IvParameterSpec::class.java)
                ivParams?.iv?.let { iv ->
                    sharedPreferences.edit().apply {
                        putString(BIOMETRIC_PARAM, Base64.encodeToString(iv, Base64.NO_WRAP))
                    }
                }
            }
            true
        } catch (e: KeyPermanentlyInvalidatedException) {
            false
        } catch (e: Exception) {
            throw java.lang.RuntimeException("Failed to init Cipher", e)
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions