· 9 years ago · Sep 13, 2016, 10:32 PM
1javax.crypto.BadPaddingException
2 at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:482)
3 at javax.crypto.Cipher.doFinal(Cipher.java:1502)
4 at nl.appsint.illuminati.component.authentication.FingerprintEncryption.encryptOrDecrypt(FingerprintEncryption.java:137)
5 at nl.appsint.illuminati.component.authentication.FingerprintDialogFragment.onAuthenticated(FingerprintDialogFragment.java:215)
6 at nl.appsint.illuminati.component.authentication.FingerprintScanner.onAuthenticationSucceeded(FingerprintScanner.java:74)
7 at android.hardware.fingerprint.FingerprintManager$MyHandler.sendAuthenticatedSucceeded(FingerprintManager.java:1108)
8 at android.hardware.fingerprint.FingerprintManager$MyHandler.handleMessage(FingerprintManager.java:1052)
9 at android.os.Handler.dispatchMessage(Handler.java:102)
10 at android.os.Looper.loop(Looper.java:158)
11 at android.app.ActivityThread.main(ActivityThread.java:7229)
12 at java.lang.reflect.Method.invoke(Native Method)
13 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
14 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
15
16Caused by: android.security.KeyStoreException: Invalid argument
17 at android.security.KeyStore.getKeyStoreException(KeyStore.java:940)
18 at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
19 at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:473)
20 ... 12 more
21
22private void initializeKeystore() {
23
24 try {
25 mKeyStore = KeyStore.getInstance("AndroidKeyStore");
26 } catch (KeyStoreException e) {
27 mKeyStore = null;
28 }
29
30 try {
31 mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KEY_STORE_NAME);
32 } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
33 mKeyGenerator = null;
34 }
35}
36
37private void createKey() {
38 if (mKeyGenerator != null) {
39 try {
40 mKeyStore.load(null);
41
42 KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(KEY_NAME,
43 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
44 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
45 .setUserAuthenticationRequired(true)
46 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
47
48 mKeyGenerator.init(builder.build());
49 mKeyGenerator.generateKey();
50 } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException |
51 CertificateException | IOException e) {
52 mKeyGenerator = null;
53 }
54 }
55}
56
57private void createCipher() {
58 try {
59 mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
60 + KeyProperties.BLOCK_MODE_CBC + "/"
61 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
62 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
63 mCipher = null;
64 }
65}
66
67@Nullable Cipher getCipher(@NonNull final FingerprintStore ivStore) {
68 if (mKeyStore != null && mKeyGenerator != null && mCipher != null) {
69 try {
70 mKeyStore.load(null);
71 SecretKey key = (SecretKey)mKeyStore.getKey(KEY_NAME, null);
72
73 switch (mEncryptionMode) {
74 case MODE_ENCRYPT:
75 mCipher.init(Cipher.ENCRYPT_MODE, key);
76 ivStore.writeIv(mCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV());
77 break;
78 case MODE_DECRYPT:
79 byte[] iv = ivStore.readIv();
80 mCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
81 break;
82 }
83 return mCipher;
84 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
85 | NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException
86 | InvalidParameterSpecException | NullPointerException e) {
87 return null;
88 }
89 }
90
91 return null;
92}
93
94@Nullable byte[] encryptOrDecrypt(@NonNull Cipher cipher, @NonNull byte[] subject) {
95 try {
96 return cipher.doFinal(subject);
97 } catch (BadPaddingException | IllegalBlockSizeException e) {
98 e.printStackTrace();
99 return null;
100 }
101}