· 7 years ago · Jul 11, 2018, 03:32 PM
1private boolean initCipher() {
2 try {
3 Cipher cipher = Cipher.getInstance(
4 KeyProperties.KEY_ALGORITHM_AES + "/"
5 + KeyProperties.BLOCK_MODE_CBC + "/"
6 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
7
8 } catch (NoSuchAlgorithmException |
9 NoSuchPaddingException e) {
10 throw new RuntimeException("Failed to get Cipher", e);
11 }
12
13 try {
14 keyStore.load(null);
15 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
16 null);
17 cipher.init(Cipher.ENCRYPT_MODE, key);
18 return true;
19
20
21 } catch (KeyPermanentlyInvalidatedException e) {
22 return false;
23
24 } catch (KeyStoreException | CertificateException
25 | UnrecoverableKeyException | IOException
26 | NoSuchAlgorithmException | InvalidKeyException e) {
27
28 throw new RuntimeException("Failed to init Cipher", e);
29 }
30 }