4

我正在使用指纹 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 下载的相同代码也能正常工作。

4

5 回答 5

1

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

SecretKey key = keyGenerator.generateKey();

用 keyGenerator 在源代码中是肯定的。这个对我有用

于 2018-12-19T06:46:30.140 回答
1

FingerPrintDialog示例中有一个createKey生成密钥的方法。只有打电话后createKey才能打电话initCipher

于 2017-08-14T10:15:06.880 回答
1

SecretKey我传递给init函数实际上是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。

于 2017-07-07T16:35:28.110 回答
0

在 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;
        }
    }
于 2021-01-23T18:23:53.790 回答
0
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;
        }
    }

“返回错误;” 这对我来说很好

于 2021-02-19T06:37:26.717 回答