Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jordizspmobile opened this issue Oct 7, 2019 · 0 comments
Open

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

jordizspmobile opened this issue Oct 7, 2019 · 0 comments

Comments

@jordizspmobile
Copy link

jordizspmobile commented Oct 7, 2019

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)
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant