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

5 java security android

在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
Run Code Online (Sandbox Code Playgroud)

小智 1

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

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

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