Однако Cipher.doFinal при расшифровке выдает следующее исключение:
Код: Выделить всё
javax.crypto.BadPaddingException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:482)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
(...)
Caused by: android.security.KeyStoreException: Invalid argument
at android.security.KeyStore.getKeyStoreException(KeyStore.java:940)
at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:473)
... 12 more
Инициализируйте хранилище ключей:
Код: Выделить всё
private void initializeKeystore() {
try {
mKeyStore = KeyStore.getInstance(KEY_STORE_NAME); //AndroidKeyStore
} catch (KeyStoreException e) {
mKeyStore = null;
}
try {
mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KEY_STORE_NAME);
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
mKeyGenerator = null;
}
}
Код: Выделить всё
private void createKey() {
if (mKeyGenerator != null) {
try {
mKeyStore.load(null);
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
mKeyGenerator.init(builder.build());
mKeyGenerator.generateKey();
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException |
CertificateException | IOException e) {
mKeyGenerator = null;
}
}
}
Код: Выделить всё
private void createCipher() {
try {
mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
mCipher = null;
}
}
Код: Выделить всё
@Nullable Cipher getCipher(@NonNull final FingerprintStore ivStore) {
if (mKeyStore != null && mKeyGenerator != null && mCipher != null) {
try {
mKeyStore.load(null);
SecretKey key = (SecretKey)mKeyStore.getKey(KEY_NAME, null);
switch (mEncryptionMode) {
case MODE_ENCRYPT:
mCipher.init(Cipher.ENCRYPT_MODE, key);
ivStore.writeIv(mCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV());
break;
case MODE_DECRYPT:
byte[] iv = ivStore.readIv();
mCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
break;
}
return mCipher;
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
| NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException
| InvalidParameterSpecException | NullPointerException e) {
return null;
}
}
return null;
}
Код: Выделить всё
@Nullable byte[] encryptOrDecrypt(@NonNull Cipher cipher, @NonNull byte[] subject) {
try {
return cipher.doFinal(subject);
} catch (BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
return null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/394 ... api-cypher