在Android L下使用KeyGuardManager时,应用程序崩溃

在Android L下使用KeyGuardManager时,应用程序崩溃,android,android-studio,android-5.0-lollipop,android-6.0-marshmallow,keyguard,Android,Android Studio,Android 5.0 Lollipop,Android 6.0 Marshmallow,Keyguard,KeyGuardManager在AndroidL上运行良好,但在AndroidL及以下版本中抛出异常 我正在使用以下功能: @RequiresApi(api = Build.VERSION_CODES.M) private void createKey() { try { KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(n

KeyGuardManager在AndroidL上运行良好,但在AndroidL及以下版本中抛出异常

我正在使用以下功能:

 @RequiresApi(api = Build.VERSION_CODES.M)
    private void createKey() {
        try {
            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            KeyGenerator keyGenerator = KeyGenerator.getInstance(
                    KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
            keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                    .setUserAuthenticationRequired(true)
                    // Require that the user has unlocked in the last 30 seconds
                    .setUserAuthenticationValidityDurationSeconds(AUTHENTICATION_DURATION_SECONDS)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                    .build());
            keyGenerator.generateKey();
        } catch (NoSuchAlgorithmException | NoSuchProviderException
                | InvalidAlgorithmParameterException | KeyStoreException
                | CertificateException | IOException e) {
            throw new RuntimeException("Failed to create a symmetric key", e);
        }
    }
但它在Android L&below中抛出以下异常:

dalvikvm: Could not find class 'android.security.keystore.KeyGenParameterSpec$Builder', referenced from method com.example.myapp.MainActivity.createKey
java.lang.VerifyError: com/example/myapp/MainActivity
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我还添加了一个条件,即该函数只能在Android L上调用。但我还是得到了以上的例外


任何帮助都将不胜感激。

问题在于加密方法中,您试图从密钥库初始化密码。Catch块试图捕获UserNotAuthenticatedException,它是在Android 23+中实现的,所以它会给您类notfound异常。与其用这种方式实现catch块,不如尝试下面的代码,它在我的代码中非常有用

private boolean tryEncrypt() {
        try {
            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_NAME, null);
            Cipher cipher = Cipher.getInstance(
                    KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/"
                            + KeyProperties.ENCRYPTION_PADDING_PKCS7);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            cipher.doFinal(SECRET_BYTE_ARRAY);
            return true;
        } catch (Exception e) {
            if (e instanceof UserNotAuthenticatedException) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    showAuthenticationScreen();
                }
                return false;
            }
            if (e instanceof KeyPermanentlyInvalidatedException) {
                return false;
            }
            return false;
        }
    }