· 7 years ago · Feb 15, 2018, 10:54 AM
1package za.co.globalwallet.globalwallet.util;
2
3import android.annotation.TargetApi;
4import android.os.Build;
5import android.security.keystore.KeyGenParameterSpec;
6import android.security.keystore.KeyPermanentlyInvalidatedException;
7import android.security.keystore.KeyProperties;
8
9import java.io.IOException;
10import java.security.InvalidAlgorithmParameterException;
11import java.security.InvalidKeyException;
12import java.security.KeyStore;
13import java.security.KeyStoreException;
14import java.security.NoSuchAlgorithmException;
15import java.security.UnrecoverableKeyException;
16import java.security.cert.CertificateException;
17
18import javax.crypto.Cipher;
19import javax.crypto.KeyGenerator;
20import javax.crypto.NoSuchPaddingException;
21import javax.crypto.SecretKey;
22import javax.crypto.spec.IvParameterSpec;
23
24/**
25 * Created by Dev on 2016-07-13.
26 */
27public class UtilC {
28
29 private static final String KEY_NAME = "GlobalWallet6bf1953c-4553-4d88-b341-014263b46825";
30
31 private static UtilC instance;
32 protected static KeyStore keyStore;
33 protected static KeyGenerator keyGenerator;
34 private static Cipher cipher;
35
36 public static UtilC getInstance(KeyGenerator keyGenerator, KeyStore keyStore, Cipher cipher) {
37 if (instance == null) {
38 instance = new UtilC();
39 }
40 setCipher(cipher);
41 setKeyGenerator(keyGenerator);
42 setKeyStore(keyStore);
43
44 return instance;
45 }
46
47 @TargetApi(Build.VERSION_CODES.M)
48 public void initCipher(String keyProperty) {
49 try {
50 cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
51 + KeyProperties.BLOCK_MODE_CBC + "/"
52 + keyProperty);
53 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
54 e.printStackTrace();
55 throw new RuntimeException(e);
56 }
57 }
58
59 @TargetApi(Build.VERSION_CODES.M)
60 public boolean initEncryptionOnCipher() {
61 try {
62 keyStore.load(null);
63 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
64 cipher.init(Cipher.ENCRYPT_MODE, key);
65 return true;
66 } catch (KeyPermanentlyInvalidatedException e) {
67 return false;
68 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
69 | NoSuchAlgorithmException | InvalidKeyException e) {
70 throw new RuntimeException("Failed to init Cipher", e);
71 }
72 }
73
74 @TargetApi(Build.VERSION_CODES.M)
75 public boolean initDecryptionOnCipher(byte[] iVSpec) {
76 try {
77 keyStore.load(null);
78 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
79 IvParameterSpec ivParameterSpec = new IvParameterSpec(iVSpec);
80 cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
81 return true;
82 } catch (KeyPermanentlyInvalidatedException e) {
83 e.printStackTrace();
84 return false;
85 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
86 | NoSuchAlgorithmException | InvalidKeyException e) {
87 throw new RuntimeException("Failed to init Cipher", e);
88 } catch (InvalidAlgorithmParameterException e) {
89 e.printStackTrace();
90 return false;
91 }
92 }
93
94 @TargetApi(Build.VERSION_CODES.M)
95 public void createKey() {
96 // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
97 // for your flow. Use of keys is necessary if you need to know if the set of
98 // enrolled fingerprints has changed.
99 try {
100 keyStore.load(null);
101 // Set the alias of the entry in Android KeyStore where the key will appear
102 // and the constrains (purposes) in the constructor of the Builder
103 keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
104 KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
105 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
106 // Require the user to authenticate with a fingerprint to authorize every use
107 // of the key
108 .setUserAuthenticationRequired(true)
109 .setEncryptionPaddings(new String[]{"PKCS5PADDING"})
110 .build());
111 keyGenerator.generateKey();
112 } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException
113 | CertificateException | IOException e) {
114 throw new RuntimeException(e);
115 }
116 }
117
118 public Cipher getCipher() {
119 return cipher;
120 }
121
122 public static KeyStore getKeyStore() {
123 return keyStore;
124 }
125
126 public static void setKeyStore(KeyStore keyStore) {
127 UtilC.keyStore = keyStore;
128 }
129
130 public static KeyGenerator getKeyGenerator() {
131 return keyGenerator;
132 }
133
134 public static void setKeyGenerator(KeyGenerator keyGenerator) {
135 UtilC.keyGenerator = keyGenerator;
136 }
137
138 public static void setCipher(Cipher cipher) {
139 UtilC.cipher = cipher;
140 }
141}