Biometric-Auth-Sample
Biometric-Auth-Sample copied to clipboard
For some device the init Cipher doesn't work (API 23)
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)
}
}