我得到了java.security.InvalidKeyException: Keystore operation failed
。有没有人有想法?这是我的代码:
initDecodeCipher的代码:
private static void initDecodeCipher(int mode) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException, InvalidKeyException, NoSuchPaddingException {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) sKeyStore.getEntry(FINGERPRINT_KEY_NAME, null);
Cipher output = Cipher.getInstance(TRANSFORMATION);
output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
}
initEncodeCipher的代码:
private static void initEncodeCipher(int mode) throws KeyStoreException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
PublicKey key = sKeyStore.getCertificate(FINGERPRINT_KEY_NAME).getPublicKey();
PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm()).generatePublic(new X509EncodedKeySpec(key.getEncoded()));
OAEPParameterSpec spec = new OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);
sCipher.init(mode, unrestricted, spec);
}
生成新密钥的代码:
@TargetApi(Build.VERSION_CODES.M)
private static boolean generateNewKey() {
if (getKeyPairGenerator()) {
try {
sKeyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(FINGERPRINT_KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
.setUserAuthenticationRequired(true)
.build());
sKeyPairGenerator.generateKeyPair();
return true;
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
}
return false;
}
错误:
java.security.InvalidKeyException: Keystore operation failed
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:760)
at android.security.KeyStore.getInvalidKeyException(KeyStore.java:781)
at android.security.keystore.KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(KeyStoreCryptoOperationUtils.java:
at android.security.keystore.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:89)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:109)
at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2668)
at javax.crypto.Cipher.tryCombinations(Cipher.java:2575)
at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2480)
at javax.crypto.Cipher.chooseProvider(Cipher.java:567)
at javax.crypto.Cipher.init(Cipher.java:831)
at javax.crypto.Cipher.init(Cipher.java:772)
at com.mayzusfs.android.moneypolo.app.utils.CryptoUtils.initDecodeCipher(CryptoUtils.java:229)
at com.mayzusfs.android.moneypolo.app.utils.CryptoUtils.initCipher(CryptoUtils.java:189)
at com.mayzusfs.android.moneypolo.app.utils.CryptoUtils.getCryptoObject(CryptoUtils.java:256)
at com.mayzusfs.android.moneypolo.app.fragments.FingerprintScanFragment.prepareSensor(FingerprintScanFragment.java:74)
at com.mayzusfs.android.moneypolo.app.fragments.FingerprintScanFragment.setUpForm(FingerprintScanFragment.java:61)
at com.mayzusfs.android.moneypolo.app.fragments.FingerprintScanFragment.onViewCreated(FingerprintScanFragment.java:26)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
使用的算法:
private static final String ALGORITHM= KeyProperties.KEY_ALGORITHM_RSA;
private static final String TRANSFORMATION = ALGORITHM+"/ECB/OAEPWithSHA-256AndMGF1Padding";
请不要强烈评价我,我是加密新手。我已经从Stackoverflow中尝试了几个答案但没有结果。
答案 0 :(得分:1)
如果其他人是通过Google到达这里的,
在引发异常的情况下,检查“ cause”变量。这将为您提供所需的详细信息。在上面的示例中,问题很可能是“填充模式不兼容”
答案 1 :(得分:0)
LINK-Android KeyStore operation failed: Unsupported Digest
我现在已经在多种设备上进行了测试,发现此问题仅发生在安装了自定义操作系统的手机上,例如Android 9和10版的沿袭,复活混音 上面的链接具有相同的详细信息
希望这对某人有帮助!
时间:1583130945557 msg:android.security.KeyStoreException:需要用户身份验证 stacktrace:java.security.ProviderException:密钥库操作失败 在android.security.keystore.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:324) 在javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:612) 在com.firstdata.mpl.utils.g.a(SourceFile:199) 在com.firstdata.mpl.utils.g.b(SourceFile:95) 在com.firstdata.mpl.utils.g.d(SourceFile:132) 在com.firstdata.mpl.utils.g。(SourceFile:62) 在com.firstdata.mpl.utils.g.a(SourceFile:69) 在com.firstdata.mpl.utils.aa.f(SourceFile:2769) 在com.firstdata.mpl.FirstFuelApplication $ 11.run(SourceFile:1161) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6724) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:495) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 原因:android.security.KeyStoreException:需要用户认证 在android.security.KeyStore.getKeyStoreException(KeyStore.java:813) ...还有16个