私は、認証にバインドされた復号化キーを使用して BiometricPrompt を実装しようとしてかなり失敗しています(ピン/パスワード/パターンの代替を許可せずに)。ユーザー認証なしで文字列を暗号化し、ユーザー認証が必要な文字列を復号化する必要があるため、非対称キーを使用しています。ただし、BiometricPrompt によって提供される cryptoObject を onAuthenticationSucceeded コールバックに使用しようとすると、IllegalBlockSizeException nullcode = 100104 が発生します。 setUserAuthenticationRequired を false に設定するだけで、例外なくすべて正常に動作します。認証に何か問題がある場合、UserNotAuthenticatedException は発生しませんか? また、暗号化に問題がある場合、setUserAuthenticationRequired に関係なく、IllegalBlockSizeException が発生することはありません。この IllegalBlockSizeException の原因は何ですか? どうすれば解決できますか?
エンコーディング:
fun encode(
keyAlias: String,
decodedString: String,
isAuthorizationRequired: Boolean
): String {
val cipher: Cipher = getEncodeCipher(keyAlias, isAuthorizationRequired)
val bytes: ByteArray = cipher.doFinal(decodedString.toByteArray())
return Base64.encodeToString(bytes, Base64.NO_WRAP)
}
//allow encoding without user authentication
private fun getEncodeCipher(
keyAlias: String,
isAuthenticationRequired: Boolean
): Cipher {
val cipher: Cipher = getCipherInstance()
val keyStore: KeyStore = loadKeyStore()
if (!keyStore.containsAlias(keyAlias))
generateKey(keyAlias, isAuthenticationRequired
)
//from https://developer.android.com/reference/android/security/keystore/KeyGenParameterSpec.html
val key: PublicKey = keyStore.getCertificate(keyAlias).publicKey
val unrestricted: PublicKey = KeyFactory.getInstance(key.algorithm).generatePublic(
X509EncodedKeySpec(key.encoded)
)
val spec = OAEPParameterSpec(
"SHA-256", "MGF1",
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT
)
cipher.init(Cipher.ENCRYPT_MODE, unrestricted, spec)
return cipher
}
鍵の生成:
private fun generateKey(keyAlias: String, isAuthenticationRequired: Boolean) {
val keyGenerator: KeyPairGenerator = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore"
)
keyGenerator.initialize(
KeyGenParameterSpec.Builder(
keyAlias,
KeyProperties.PURPOSE_DECRYPT or KeyProperties.PURPOSE_ENCRYPT
).run {
setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
setUserAuthenticationRequired(isAuthenticationRequired) //only if isAuthenticationRequired is false -> No IllegalBlockSizeException during decryption
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setInvalidatedByBiometricEnrollment(true)
}
build()
}
)
keyGenerator.generateKeyPair()
}
}
デコード:
//this Cipher is passed to the BiometricPrompt
override fun getDecodeCipher(keyAlias: String): Cipher {
val keyStore: KeyStore = loadKeyStore()
val key: PrivateKey = keyStore.getKey(keyAlias, null) as PrivateKey
cipher.init(Cipher.DECRYPT_MODE, key)
return cipher
}
//this is called from inside onAuthenticationSucceeded BiometricPrompt.AuthenticationCallback
fun decodeWithDecoder(encodedStrings: List<String>, cryptoObject: BiometricPrompt.CryptoObject): List<String> {
return try {
encodedStrings.map {
val bytes: ByteArray = Base64.decode(it, Base64.NO_WRAP)
//here i get IllegalBlockSizeException after the first iteration if isAuthenticationRequired is set to true
String(cryptoObject.cipher!!.doFinal(bytes))
}
}
生体認証プロンプト:
private fun setUpBiometricPrompt() {
executor = ContextCompat.getMainExecutor(requireContext())
val callback = object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d("${this.javaClass.canonicalName}", "onAuthenticationError $errString")
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d("${this.javaClass.canonicalName}", "Authentication failed")
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
//passing the received crypto object on for decryption operation (which then fails)
decodeWithDecoder(encodedString: String, result.cryptoObject)
super.onAuthenticationSucceeded(result)
}
}
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Use account password")
.build()
biometricPrompt = BiometricPrompt(this, executor, callback)
}
//show the prompt
fun authenticate() {
biometricPrompt.authenticate(
promptInfo,
BiometricPrompt.CryptoObject(getDecodeCipher()))
}