java.lang.RuntimeException:无法使用FingerPrint初始化密码



我使用指纹sdk,它总是崩溃。

java.lang.RuntimeException: Failed to init Cipher                                                                                               
at com.example.ammar.fingerbyitself.MainActivity.initCipher(MainActivity.java:160)
at com.example.ammar.fingerbyitself.MainActivity.access$000(MainActivity.java:55)
at com.example.ammar.fingerbyitself.MainActivity$1.onClick(MainActivity.java:109)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.security.InvalidKeyException: Only SecretKey is supported
at com.android.org.conscrypt.OpenSSLCipher.checkAndSetEncodedKey(OpenSSLCipher.java:435)
at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCipher.java:260)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:612)
at javax.crypto.Cipher.tryCombinations(Cipher.java:532)
at javax.crypto.Cipher.getSpi(Cipher.java:437)
at javax.crypto.Cipher.init(Cipher.java:815)
at javax.crypto.Cipher.init(Cipher.java:774)
at com.example.ammar.fingerbyitself.MainActivity.initCipher(MainActivity.java:153)
at com.example.ammar.fingerbyitself.MainActivity.access$000(MainActivity.java:55) 
at com.example.ammar.fingerbyitself.MainActivity$1.onClick(MainActivity.java:109) 
at android.view.View.performClick(View.java:5697) 
at android.widget.TextView.performClick(TextView.java:10814) 
at android.view.View$PerformClick.run(View.java:22526) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7229) 
at java.lang.reflect.Method.invoke(Native Method) 

当我调用CIPHERinit()时

private boolean initCipher() {
        try {
//            KeyStore mKeyStore = KeyStore.getInstance("AndroidKeyStore");
            mKeyStore.load(null);
            SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
            mCipher.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);
        }
    }

它崩溃的原因尚不清楚,甚至从GitHub下载的代码也是一样的。

FingerPrintDialog示例中有一个生成密钥的createKey方法。调用createKey后,只能调用initCipher

当我传递给init函数的SecretKey实际上是null时,我得到了这个错误。

也就是说,我做了一个指纹认证库。你可能想试试:

FingerprintDialog.initialize(this)
    .title(R.string.title)
    .message(R.string.message)
    .callback(new FingerprintDialogCallback() {
        @Override public void onAuthenticationSucceeded() {}
        @Override public void onAuthenticationCancel() {}
    })
    .show();

指纹库

您可以选择是否使用CryptoObject。

由KEY_NAME引起的不是密钥。您可以使用尝试另一种方法初始化密钥

SecretKey key = keyGenerator.generateKey();

With keyGenerator在源代码中是肯定的。它适用于我

在FingerPrintDialog实现的函数中示例:

private boolean initCipher() {
        try {
//            KeyStore mKeyStore = KeyStore.getInstance("AndroidKeyStore");
            mKeyStore.load(null);
            SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
            mCipher.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);
        }
    }

当发生屏幕锁定重置但未登记指纹时,响应为throw new RuntimeException("Failed to init Cipher", e);。。。因此,代替throwingRuntimeException,将所有异常分组为一个catch,并为组中的每个异常分组为return false

private boolean initCipher(Cipher cipher, String keyName) {
        try {
            mKeyStore.load(null);
            SecretKey key = (SecretKey) mKeyStore.getKey(keyName, null);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return true;
        } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            return false;
        }
    }

或者

private int initCipher(Cipher cipher, String keyName) {
        try {
            mKeyStore.load(null);
            SecretKey key = (SecretKey) mKeyStore.getKey(keyName, null);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return 1;
        } catch (KeyPermanentlyInvalidatedException e) {
            return 0;
        } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
                | NoSuchAlgorithmException | InvalidKeyException e) {
            return 2;
        }
    }
private boolean initCipher(Cipher cipher, String keyName) {
        try {
            mKeyStore.load(null);
            SecretKey key = (SecretKey) mKeyStore.getKey(keyName, null);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return true;
        } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
            return false;
        }
    }

"return false"这对我来说很好

相关内容

  • 没有找到相关文章

最新更新