删除指纹不会触发 Fingerprnt 身份验证的 InvalidKeyException

Removal of Fingerprint not triggering InvalidKeyException for Fingerprnt Authentication

我遇到指纹身份验证问题,实际上,我在我的应用程序中集成了指纹身份验证并且工作正常,除了一种情况。

我在我的设备中设置了两个指纹,在使用 Fingerprint KEY 初始化 KeyGenerator 后,我从设备中删除了一个指纹并返回到我的应用程序并执行指纹验证,它工作正常。我不知道为什么它不触发 InvalidKeyException 就像添加指纹一样有效。 这是预期的行为还是 OS 的任何错误?

设备详情如下,

Device : Pixel

OS: Android 8.0

我的实现代码如下,

protected void generateKey() {
       try {
           keyStore = KeyStore.getInstance("AndroidKeyStore");
       } catch (Exception e) {
           e.printStackTrace();
       }


       KeyGenerator keyGenerator;
       try {
           keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
       } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
           throw new RuntimeException("Failed to get KeyGenerator instance", e);
       }


       try {
           keyStore.load(null);
           keyGenerator.init(new
                   KeyGenParameterSpec.Builder(KEY_NAME,
                   KeyProperties.PURPOSE_ENCRYPT |
                           KeyProperties.PURPOSE_DECRYPT)
                   .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                   .setUserAuthenticationRequired(true)
                   .setEncryptionPaddings(
                           KeyProperties.ENCRYPTION_PADDING_PKCS7)
                   .build());
           keyGenerator.generateKey();
       } catch (NoSuchAlgorithmException |
               InvalidAlgorithmParameterException
               | CertificateException | IOException e) {
           throw new RuntimeException(e);
       }
   }

 public boolean cipherInit() {
       try {
           cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
       } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
           throw new RuntimeException("Failed to get Cipher", e);
       }


       try {
           keyStore.load(null);
           SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
                   null);
           cipher.init(Cipher.ENCRYPT_MODE, key);
           return true;
       } catch (KeyPermanentlyInvalidatedException e) {
           return false;
       } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
           throw new RuntimeException("Failed to init Cipher", e);
       }
   }
}

我已经尝试了几个帖子(How to detect the removal fingerprint in Android?, Android Fingerprint API and Private/Public keys, Android Key Invalidation when Fingerprints removed),没有任何帮助我摆脱它。

您的密码与指纹没有严格关联。它所具有的有点相关的是:

setUserAuthenticationRequired(true)

这表示您在 AndroidKeyStore 中创建的密钥需要用户身份验证。它没有说任何关于指纹的事情。

如果用户注册了指纹,则用户可以使用指纹进行身份验证以使用此密钥。这将适用于用户注册的任何指纹,在您的情况下,您仍然拥有注册的指纹(即使在用户删除第二个指纹之后)。此外,用户不必使用指纹进行身份验证——他们可以选择使用密码、PIN 或图案。

Is this expected behavior or any bug with OS?

这是预期的行为。