· 7 years ago · Oct 24, 2018, 10:20 PM
1javax.crypto.BadPaddingException
2 at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:482)
3 at javax.crypto.Cipher.doFinal(Cipher.java:1502)
4 (...)
5
6Caused by: android.security.KeyStoreException: Invalid argument
7 at android.security.KeyStore.getKeyStoreException(KeyStore.java:940)
8 at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
9 at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:473)
10 ... 12 more
11
12private void initializeKeystore() {
13
14 try {
15 mKeyStore = KeyStore.getInstance(KEY_STORE_NAME); //AndroidKeyStore
16 } catch (KeyStoreException e) {
17 mKeyStore = null;
18 }
19
20 try {
21 mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KEY_STORE_NAME);
22 } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
23 mKeyGenerator = null;
24 }
25}
26
27private void createKey() {
28 if (mKeyGenerator != null) {
29 try {
30 mKeyStore.load(null);
31
32 KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(KEY_NAME,
33 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
34 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
35 .setUserAuthenticationRequired(true)
36 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
37
38 mKeyGenerator.init(builder.build());
39 mKeyGenerator.generateKey();
40 } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException |
41 CertificateException | IOException e) {
42 mKeyGenerator = null;
43 }
44 }
45}
46
47private void createCipher() {
48 try {
49 mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
50 + KeyProperties.BLOCK_MODE_CBC + "/"
51 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
52 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
53 mCipher = null;
54 }
55}
56
57@Nullable Cipher getCipher(@NonNull final FingerprintStore ivStore) {
58 if (mKeyStore != null && mKeyGenerator != null && mCipher != null) {
59 try {
60 mKeyStore.load(null);
61 SecretKey key = (SecretKey)mKeyStore.getKey(KEY_NAME, null);
62
63 switch (mEncryptionMode) {
64 case MODE_ENCRYPT:
65 mCipher.init(Cipher.ENCRYPT_MODE, key);
66 ivStore.writeIv(mCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV());
67 break;
68 case MODE_DECRYPT:
69 byte[] iv = ivStore.readIv();
70 mCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
71 break;
72 }
73 return mCipher;
74 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
75 | NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException
76 | InvalidParameterSpecException | NullPointerException e) {
77 return null;
78 }
79 }
80
81 return null;
82}
83
84@Nullable byte[] encryptOrDecrypt(@NonNull Cipher cipher, @NonNull byte[] subject) {
85 try {
86 return cipher.doFinal(subject);
87 } catch (BadPaddingException | IllegalBlockSizeException e) {
88 e.printStackTrace();
89 return null;
90 }
91}