@@ -67,7 +67,7 @@ object KeyStoreManagerUtils {
6767 * Method to encrypt data with key
6868 */
6969 fun encryptData (key : String , data : String ) {
70- sharedPreferences? .edit()? .putString(key, data)?.apply ()
70+ sharedPreferences.edit().putString(key, data)?.apply ()
7171 encryptedPairData = getEncryptedDataPair(data)
7272 encryptedPairData.second.toString(UTF_8 )
7373 }
@@ -88,44 +88,49 @@ object KeyStoreManagerUtils {
8888 * Method to decrypt data with key
8989 */
9090 fun decryptData (key : String ): String? {
91- val sharedPreferenceIds = sharedPreferences?.all
9291 var result: String? = null
93- sharedPreferenceIds?.forEach {
94- if (it.key.contains(key)) {
95- result = sharedPreferences?.getString(it.key, " " )
92+ try {
93+ val sharedPreferenceIds = sharedPreferences.all
94+ sharedPreferenceIds.forEach {
95+ if (it.key.contains(key)) {
96+ result = sharedPreferences.getString(it.key, " " )
97+ }
9698 }
99+ if (result == null ) return null
100+ val encryptedPairData = result?.let { getEncryptedDataPair(it) }
101+ val cipher = Cipher .getInstance(TRANSFORMATION )
102+ val keySpec = IvParameterSpec (encryptedPairData?.first)
103+ cipher.init (Cipher .DECRYPT_MODE , getKey(), keySpec)
104+ return cipher.doFinal(encryptedPairData?.second).toString(UTF_8 )
105+ } catch (ex: Exception ) {
106+ ex.printStackTrace()
97107 }
98- if (result == null ) return null
99- val encryptedPairData = result?.let { getEncryptedDataPair(it) }
100- val cipher = Cipher .getInstance(TRANSFORMATION )
101- val keySpec = IvParameterSpec (encryptedPairData?.first)
102- cipher.init (Cipher .DECRYPT_MODE , getKey(), keySpec)
103- return cipher.doFinal(encryptedPairData?.second).toString(UTF_8 )
108+ return result
104109 }
105110
106111 /* *
107112 * Store encrypted data into preferences
108113 */
109114 fun savePreferenceData (key : String , data : String ) {
110- sharedPreferences? .edit()? .putString(key, data)?.apply ()
115+ sharedPreferences.edit().putString(key, data)?.apply ()
111116 }
112117
113118 /* *
114119 * Retrieve decrypted data from preferences
115120 */
116121 fun getPreferencesData (key : String ): String? {
117- return sharedPreferences? .getString(key, " " )
122+ return sharedPreferences.getString(key, " " )
118123 }
119124
120125 /* *
121126 * Delete All local storage
122127 */
123128 fun deletePreferencesData (key : String ) {
124- sharedPreferences? .edit()? .remove(key)?.apply ()
125- val sharedPreferenceIds = sharedPreferences? .all
126- sharedPreferenceIds? .forEach {
129+ sharedPreferences.edit().remove(key)?.apply ()
130+ val sharedPreferenceIds = sharedPreferences.all
131+ sharedPreferenceIds.forEach {
127132 if (it.key.contains(key)) {
128- sharedPreferences? .edit()? .remove(key)?.apply ()
133+ sharedPreferences.edit().remove(key)?.apply ()
129134 }
130135 }
131136 }
0 commit comments