终结器引发的未捕获异常:绑定器已终结,Android密钥库

时间:2019-01-09 20:11:53

标签: java android authentication fingerprint

我在应用程序中实现了指纹解锁,现在我添加了密码解锁,但该应用程序根本无法启动。我试图更改KeyStore实例名称,并使用相同的KeyStore实例名称。

有时应用会引发此错误:

E/System: Uncaught exception thrown by finalizer
E/System: java.lang.IllegalStateException: Binder has been finalized!
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:751)
        at android.security.IKeystoreService$Stub$Proxy.abort(IKeystoreService.java:1373)
        at android.security.KeyStore.abort(KeyStore.java:541)
        at android.security.keystore.AndroidKeyStoreCipherSpiBase.finalize(AndroidKeyStoreCipherSpiBase.java:744)
        at android.security.keystore.AndroidKeyStoreUnauthenticatedAESCipherSpi$CBC$PKCS7Padding.finalize(Unknown Source:0)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:253)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:240)
        at java.lang.Daemons$Daemon.run(Daemons.java:103)

当我尝试启动该应用程序时,它会立即关闭而不会在调试日志或logcat日志中崩溃,对此问题有何建议?

代码:

@Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.auth);

    keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    msg = (TextView) findViewById(R.id.message);


        try {
            generateKey();
        } catch (FingerprintException e) {
            e.printStackTrace();
        }

        if (initCipher()) {
            cryptoObject = new FingerprintManager.CryptoObject(cipher);
            FingerprintHandler helper = new FingerprintHandler(this);
            helper.startAuth(fingerprintManager, cryptoObject);
        }

    if (!getIntent().hasExtra(EXTRA_OPER)) {
        sendErr("operation is not defined");
        return;
    }
    oper = getIntent().getStringExtra(EXTRA_OPER);
    if (OPER_CREATE.equals(oper)) {
        msg.setText(R.string.create_code);
        if (!getIntent().hasExtra(EXTRA_SESSION)) {
            sendErr("session not found");
            return;
        }
        session = getIntent().getStringExtra(EXTRA_SESSION);
    } else if (OPER_VERIFY.equals(oper)) {
        msg.setText(R.string.type_code);
    }
    pin = new StringBuilder();
}

private void generateKey() throws FingerprintException {
    try {
        // Obtain a reference to the Keystore using the standard Android keystore container identifier (“AndroidKeystore”)//
        keyStore = KeyStore.getInstance("AndroidKeyStore");

        //Generate the key//
        keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");

        //Initialize an empty KeyStore//
        keyStore.load(null);

        //Initialize the KeyGenerator//
        keyGenerator.init(new

                //Specify the operation(s) this key can be used for//
                KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)

                //Configure this key so that the user has to confirm their identity with a fingerprint each time they want to use it//
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(
                        KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());

        //Generate the key//
        keyGenerator.generateKey();

    } catch (KeyStoreException
            | NoSuchAlgorithmException
            | NoSuchProviderException
            | InvalidAlgorithmParameterException
            | CertificateException
            | IOException exc) {
        exc.printStackTrace();
        throw new FingerprintException(exc);
    }
}

//Create a new method that we’ll use to initialize our cipher//
public boolean initCipher() {
    try {
        //Obtain a cipher instance and configure it with the properties required for fingerprint authentication//
        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 if the cipher has been initialized successfully//
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {

        //Return false if cipher initialization failed//
        return false;
    } catch (KeyStoreException | CertificateException
            | UnrecoverableKeyException | IOException
            | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}

private class FingerprintException extends Exception {
    public FingerprintException(Exception e) {
        super(e);
    }
}

private void next() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    if (OPER_CREATE.equals(oper)) {
        if (pin0 == null) {
            pin0 = pin.toString();
            pin = new StringBuilder();
            showPin();
            msg.setText(R.string.retype_code);
            return;
        }
        if (!pin0.equals(pin.toString())) {
            Toast.makeText(this, R.string.not_equal, Toast.LENGTH_LONG).show();
            pin0 = null;
            pin = new StringBuilder();
            showPin();
            msg.setText(R.string.create_code);
            return;
        }
        boolean b = new PinFacade(this).setupPin(pin.toString(), session);
        if (b) sendOk();
        else sendErr("can't setup pin");
    } else if (OPER_VERIFY.equals(oper)){
        try {
            session = new PinFacade(this).verifyPin(pin.toString());
            sendOk();
        } catch (Exception e) {
            e.printStackTrace();
            sendErr(e.getMessage());
        }
    }
}

private void sendOk() {
    setResult(RESULT_OK, new Intent().putExtra(EXTRA_SESSION, session));
    finish();
}

private void sendErr(String err) {
    setResult(RESULT_CANCELED, new Intent().putExtra(EXTRA_ERROR, err));
    finish();
}

}

1 个答案:

答案 0 :(得分:0)

我通过检查电话是否具有指纹硬件,电话是否配置了指纹,应用程序是否具有指纹权限以及电话是否已锁定来解决问题。

    if (!fingerprintManager.isHardwareDetected()) {
        msg.setText("Your device doesn't support fingerprint authentication");
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
        msg.setText("Please enable the fingerprint permission");
    }
    if (!fingerprintManager.hasEnrolledFingerprints()) {
        msg.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
    }

    if (!keyguardManager.isKeyguardSecure()) {
        msg.setText("Please enable lockscreen security in your device's Settings");
    } else {
        try {
            generateKey();
        } catch (FingerprintException e) {
            e.printStackTrace();
        }

        if (initCipher()) {
            cryptoObject = new FingerprintManager.CryptoObject(cipher);
            helper = new FingerprintHandler(this);
            helper.startAuth(fingerprintManager, cryptoObject);
        }
    }