【问题标题】:java.lang.RuntimeException: Failed to init Cipher using FingerPrintjava.lang.RuntimeException:无法使用指纹初始化密码
【发布时间】:2016-08-03 12:13:24
【问题描述】:

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

【问题讨论】:

  • 查看 roo 原因:Caused by: java.security.InvalidKeyException: Only SecretKey is supported
  • mKeyStore.getKey(KEY_NAME, null) 是否返回 null?那可能是你的问题。 AndroidKeyStore 中是否已经存储了具有该别名的密钥?如果没有,您必须创建一个。
  • 你有解决办法吗?
  • 是的,我找到了解决方案
  • 那么答案是什么?我在互联网上找不到针对“仅支持 SecretKey”异常的任何解决方案。

标签: java android fingerprinting android-fingerprint-api


【解决方案1】:

FingerPrintDialog 示例中,有一个createKey 方法可以生成密钥。拨打createKey后只有你可以拨打initCipher

【讨论】:

    【解决方案2】:

    当我传递给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();
    

    Fingerprint Library

    您可以选择是否使用 CryptoObject。

    【讨论】:

      【解决方案3】:

      KEY_NAME 不是秘钥造成的。您可以尝试使用另一种方式来初始化密钥

      SecretKey key = keyGenerator.generateKey();
      

      用keyGenerator 肯定在源代码中。它对我有用

      【讨论】:

        【解决方案4】:

        在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); ...因此,RuntimeException 不是throwing,而是将所有异常归为一个catch 和@987654326 @为组中的每个异常:

        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;
                }
            }
        

        【讨论】:

          【解决方案5】:
          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;
                  }
              }
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-08-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多