· 7 years ago · Nov 18, 2018, 06:48 AM
1private fun createAndInitCipher() {
2 val keyName = KeyStoreUtil.getDefaultKeyName()
3
4 try {
5 createKey(keyName, false)
6 } catch (e: Exception){
7 CaughtExceptionHandler.reportException(e)
8 onSkipped()
9 }
10
11 val defaultCipher: Cipher = try {
12 Cipher.getInstance(cipherTransformation)
13 } catch (e: NoSuchAlgorithmException) {
14 throw RuntimeException("Failed to get an instance of Cipher", e)
15 } catch (e: NoSuchPaddingException) {
16 throw RuntimeException("Failed to get an instance of Cipher", e)
17 }
18
19
20
21 if (initCipher(defaultCipher, cipherMode, keyName)) {
22 cryptoObject = FingerprintManagerCompat.CryptoObject(defaultCipher)
23 }
24}
25
26@SuppressLint("NewApi")
27private fun initCipher(cipher: Cipher, cipherMode: Int, keyName: String): Boolean {
28 try {
29 keyStore?.load(null)
30
31 val key: Key = keyStore?.getKey(keyName, null) as SecretKey
32 try {
33
34
35 if (cipherMode == Cipher.DECRYPT_MODE) {
36 val ivStr = DbPrefDao.getString("fingeriv", null)
37 val decodedIv = Base64.decode(ivStr, Base64.NO_WRAP)
38 val iv = IvParameterSpec(decodedIv)
39
40 cipher.init(cipherMode, key, iv)
41 } else {
42 cipher.init(cipherMode, key)
43
44 val iv = cipher.iv
45 val encodedIv = Base64.encodeToString(iv, Base64.NO_WRAP)
46 DbPrefDao.putString("fingeriv", encodedIv)
47
48 }
49 } catch (e: KeyPermanentlyInvalidatedException) {
50
51 if (activity is TransactionPinCodeActivity) {
52
53 (activity as TransactionPinCodeActivity).onFingerprintInvalidated()
54
55 }
56 this.dismissAllowingStateLoss()
57 }
58
59
60 if (cipherMode == Cipher.ENCRYPT_MODE) {
61
62 }
63
64 return true
65 } catch (e: KeyStoreException) {
66 throw RuntimeException("Failed to init Cipher", e)
67 } catch (e: CertificateException) {
68 throw RuntimeException("Failed to init Cipher", e)
69 } catch (e: UnrecoverableKeyException) {
70 throw RuntimeException("Failed to init Cipher", e)
71 } catch (e: IOException) {
72 throw RuntimeException("Failed to init Cipher", e)
73 } catch (e: NoSuchAlgorithmException) {
74 throw RuntimeException("Failed to init Cipher", e)
75 } catch (e: InvalidKeyException) {
76 throw RuntimeException("Failed to init Cipher", e)
77 } catch (e: KeyPermanentlyInvalidatedException) {
78 return false
79 }
80}
81
82Fatal Exception: java.lang.RuntimeException
83
84Unable to start activity ComponentInfo: java.lang.RuntimeException: Failed to init Cipher
85android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2792)
86com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
87Caused by java.lang.RuntimeException
88Failed to init Cipher
89com.core.ui.authentication.pincode.fingerprint.FingerprintDialog.initCipher (SourceFile:371)
90com.core.ui.authentication.pincode.fingerprint.FingerprintDialog.createAndInitCipher (SourceFile:140)
91com.core.ui.authentication.pincode.fingerprint.FingerprintDialog.onCreate (SourceFile:115)
92android.support.v4.app.Fragment.performCreate (SourceFile:2331)
93android.support.v7.app.AppCompatActivity.onStart (SourceFile:177)
94com.core.ui.base.BaseActivity.onStart (SourceFile:348)
95com.core.ui.base.BaseLocationAwareActivity.onStart (SourceFile:28)
96android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1335)
97com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
98Caused by java.security.UnrecoverableKeyException
99Failed to obtain information about key
100android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreSecretKeyFromKeystore (AndroidKeyStoreProvider.java:282)
101java.security.KeyStore.getKey (KeyStore.java:1062)
102com.core.ui.authentication.pincode.fingerprint.FingerprintDialog.initCipher (SourceFile:320)
103com.core.ui.authentication.pincode.fingerprint.FingerprintDialog.createAndInitCipher (SourceFile:140)
104com.core.ui.authentication.pincode.fingerprint.FingerprintDialog.onCreate (SourceFile:115)
105android.support.v4.app.Fragment.performCreate (SourceFile:2331)
106android.support.v7.app.AppCompatActivity.onStart (SourceFile:177)
107com.core.ui.base.BaseActivity.onStart (SourceFile:348)
108com.core.ui.base.BaseLocationAwareActivity.onStart (SourceFile:28)
109android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1335)
110com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807