java.security.InvalidKeyException: сбой операции хранилища ключей - PullRequest
0 голосов
/ 26 апреля 2018

Я получаю 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, но безрезультатно.

1 Ответ

0 голосов
/ 03 мая 2019

Если кто-то еще прибывает сюда через Google:

В исключении, которое выдается, проверьте переменную "причина".Это даст вам дать вам детали, которые вам нужны.В приведенном выше примере проблема, скорее всего, будет «Несовместимый режим заполнения»

...