android.security.KeyStoreException:升级到android 8后需要用户身份验证

时间:2018-04-26 19:02:30

标签: java android security

在Android 8上更新后,当我尝试创建新密钥以便在指纹身份验证后使用它时,我得到 android.security.KeyStoreException:需要用户身份验证。 在更新到8之前,它按预期工作。 这并非在每台设备上都会发生。

  mKeyStore.load(null);

 KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(
                    alias,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
                    .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                    .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                    .setBlockModes(KeyProperties.BLOCK_MODE_ECB)
                    .setUserAuthenticationRequired(userAuthenticationRequired) 
                    .setKeyValidityEnd(calendar.getTime())
                    .setKeySize(keysize)
                    .build();
mKeyPairGenerator.initialize(spec);
 KeyPair keyPair = mKeyPairGenerator.generateKeyPair();//this is the line I'm getting the keystore exception

1 个答案:

答案 0 :(得分:0)

我们已将此问题缩小为S9 Android 8设备未使用PIN,图案或密码进行保护。

一种解决方案是将ProviderException添加到围绕generateKeyPair的try catch块中并在那里进行处理。

或者,您可以使用KeyguardManager.isKeyguardSecure()防止生成密钥。

相关问题