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

时间:2018-04-21 17:42:46

标签: android android-fingerprint-api

我正面临指纹认证的问题,实际上,我已经在我的应用程序中集成了指纹认证,并且工作正常,除了一个案例。

我已经在我的设备中设置了两个指纹,在用指纹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 keysAndroid Key Invalidation when Fingerprints removed),没有任何帮助我摆脱它。

1 个答案:

答案 0 :(得分:2)

您的代码并非严格依赖于指纹。所有它有一点相关的是:

setUserAuthenticationRequired(true)

这表示您在AndroidKeyStore中创建的密钥需要用户身份验证。它没有说明指纹。

如果用户已注册指纹,则用户可以使用指纹进行身份验证,以便使用此密钥。这将是用户注册的任何指纹,在您的情况下,您仍然有一个注册指纹(即使用户删除了第二个指纹)。此外,用户无需使用指纹进行身份验证 - 他们可以选择使用密码,PIN或模式。

  

这是预期的行为还是OS的任何错误?

这是预期的行为。