Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.security.InvalidKeyException:密钥库操作失败_Java_Android_Encryption_Cryptography_Keystore - Fatal编程技术网

java.security.InvalidKeyException:密钥库操作失败

java.security.InvalidKeyException:密钥库操作失败,java,android,encryption,cryptography,keystore,Java,Android,Encryption,Cryptography,Keystore,我正在获取java.security.InvalidKeyException:密钥库操作失败。有人有主意吗?这是我的密码: 初始化密码的代码: private static void initDecodeCipher(int mode) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException, InvalidKeyException, NoSuchPaddingException {

我正在获取
java.security.InvalidKeyException:密钥库操作失败
。有人有主意吗?这是我的密码:

初始化密码的代码:

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());
}
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);
}
InitEncodeChipher的代码:

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());
}
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的几个答案,但没有结果。

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

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

链接-

我现在已经在多个设备上进行了测试,发现这个问题只发生在android版本9和10上安装了定制操作系统的手机上,比如天堂、复活混音 以上链接共享相同的详细信息

希望这对别人有帮助

精确日志 时间:158313094557 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(源文件:199) 位于com.firstdata.mpl.utils.g.b(源文件:95) 位于com.firstdata.mpl.utils.g.d(源文件:132) 位于com.firstdata.mpl.utils.g.(源文件:62) 位于com.firstdata.mpl.utils.g.a(源文件:69) 位于com.firstdata.mpl.utils.aa.f(源文件:2769) 位于com.firstdata.mpl.FirstFuelApplication$11.run(源文件: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个

你从哪里得到的?在您的问题中,请在output.init(Cipher.DECRYPT_模式,privateKeyEntry.getPrivateKey())上进行堆栈跟踪;刚加上的常数值是多少?您使用的密码转换是什么,密钥对类型是什么?以防万一:private static final String ALGORITHM=KeyProperties.key\u ALGORITHM\u RSA;私有静态最终字符串转换=算法+“/ECB/OAEPWITHHA-256和MGF1PADDING”;