我正在使用指纹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下载相同的代码。
答案 0 :(得分: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();
<强> Fingerprint Library 强>
您可以选择是否使用CryptoObject。
答案 1 :(得分:1)
在FingerPrintDialog示例中,有一个生成密钥的createKey
方法。致电createKey
后,您只能拨打initCipher
答案 2 :(得分:1)
由KEY_NAME引起的不是秘密密钥。您可以尝试使用另一种方法初始化私钥,方法是使用
SecretKey key = keyGenerator.generateKey();
使用keyGenerator可以肯定在源代码中。它对我有用
答案 3 :(得分: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);
... 因此,将所有异常归为一个,而不是 throwing
RuntimeException
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;
}
}
答案 4 :(得分: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;
}
}
"返回假;"这对我来说很好