【问题标题】:java.security.InvalidKeyException: Keystore operation failedjava.security.InvalidKeyException:密钥库操作失败
【发布时间】:2018-04-25 23:18:10
【问题描述】:

我收到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 的几个答案,但没有结果。

【问题讨论】:

  • 你在哪里得到它?请在您的问题中进行堆栈跟踪。
  • 在 output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
  • 刚刚添加
  • 常量的值是多少?您使用的是什么密码转换,密钥对类型是什么?
  • 以防万一:private static final String ALGORITHM= KeyProperties.KEY_ALGORITHM_RSA;私有静态最终字符串转换 = ALGORITHM+"/ECB/OAEPWithSHA-256AndMGF1Padding";

标签: java android encryption cryptography keystore


【解决方案1】:

如果其他人通过 Google 到达这里:

在抛出的异常中,检查“原因”变量。这将为您提供所需的详细信息。在上面的示例中,问题很可能是“不兼容的填充模式”

【讨论】:

    【解决方案2】:

    LINK -Android KeyStore operation failed: Unsupported Digest

    我现在已经在多个设备上进行了测试,发现这个问题只发生在安装了自定义操作系统的手机上,如血统,复活混音,在 android 版本 9 和 10 上 上面的链接有相同的细节

    希望这对某人有所帮助!

    准确的日志

    时间:1583130945557 味精:android.security.KeyStoreException:需要用户身份验证 堆栈跟踪:java.security.ProviderException:密钥库操作失败 在 android.security.keystore.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:324) 在 javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:612) 在 com.firstdata.mpl.utils.g.a(源文件: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 更多

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2011-10-17
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多