Przeglądaj źródła

本地存储数据加解密

wanyao 7 lat temu
rodzic
commit
f8ce066724

+ 42 - 8
ipu-plugin-basic/src/main/java/com/ai/ipu/mobile/plugin/MobileStorage.java

@ -1,5 +1,6 @@
1 1
package com.ai.ipu.mobile.plugin;
2 2

3
import java.security.Key;
3 4
import java.util.HashMap;
4 5
import java.util.Map;
5 6

@ -7,14 +8,18 @@ import org.json.JSONArray;
7 8
import org.json.JSONException;
8 9
import org.json.JSONObject;
9 10

11
import com.ai.ipu.basic.cipher.DES;
10 12
import com.ai.ipu.basic.string.StringUtil;
11 13
import com.ai.ipu.mobile.common.MobileCache;
12 14
import com.ai.ipu.mobile.data.SharedPrefUtil;
13 15
import com.ai.ipu.mobile.frame.IIpuMobile;
14 16
import com.ai.ipu.mobile.frame.plugin.Plugin;
17
import com.ai.ipu.mobile.frame.template.TemplateManager;
15 18
import com.ai.ipu.mobile.util.Constant;
16 19
import com.ailk.common.data.IData;
17 20
import com.ailk.common.data.impl.DataMap;
21
import com.litesuits.android.log.Log;
22
import com.ryg.utils.LOG;
18 23

19 24
public class MobileStorage extends Plugin{
20 25
	
@ -137,41 +142,70 @@ public class MobileStorage extends Plugin{
137 142
	}
138 143
	
139 144
	public void setOfflineCache(JSONArray param) throws Exception {
145
		Log.d("wanyao", param.toString());
140 146
		String key = param.getString(0);
141 147
		String value = param.getString(1);
142
		setOfflineCache(key, value);
148
		Boolean isEncrypt = param.getBoolean(2);
149
		setOfflineCache(key,value,isEncrypt);
143 150
		callback(value);
144 151
	}
145 152
	
146 153
	/**写入Android本地存储键值对*/
147
	public void setOfflineCache(String key,String value){
154
	public void setOfflineCache(String key,String value,Boolean isEncrypt)throws Exception {
155
		Key deskey = null;
156
		if(isEncrypt){
157
			deskey = TemplateManager.getResKey();
158
		}
148 159
		if (key == null || "".equals(key))
149 160
			return;
150 161
		if (StringUtil.isDataMap(key)) {
151 162
			IData data = new DataMap(key);
152
			SharedPrefUtil.put(Constant.MobileCache.IPU_MOBILE_STORAGE, data);
163
			if(isEncrypt){
164
				SharedPrefUtil.put(Constant.MobileCache.IPU_MOBILE_STORAGE, data,deskey);
165
			}else{
166
				SharedPrefUtil.put(Constant.MobileCache.IPU_MOBILE_STORAGE, data);
167
			}
153 168
		} else {
154
			SharedPrefUtil.put(Constant.MobileCache.IPU_MOBILE_STORAGE, key, value);
169
			if(isEncrypt){
170
				SharedPrefUtil.put(Constant.MobileCache.IPU_MOBILE_STORAGE, key, value,deskey);
171
			}else{
172
				SharedPrefUtil.put(Constant.MobileCache.IPU_MOBILE_STORAGE, key, value);
173
			}
155 174
		}
156 175
	}
157 176
	
158 177
	public void getOfflineCache(JSONArray param) throws Exception {
159 178
		String key = param.getString(0);
160 179
		String defValue = param.getString(1);
161
		String value = getOfflineCache(key, defValue);
180
		Boolean isEncrypt = param.getBoolean(2);
181
		String value = getOfflineCache(key, defValue,isEncrypt);
162 182
		callback(value);
163 183
	}
164 184
	
165 185
	/**获取 Android 本地存储键值 */
166
	public String getOfflineCache(String key,String defValue) throws JSONException{
186
	public String getOfflineCache(String key,String defValue,Boolean isEncrypt) throws Exception{
187
		Key deskey = null;
188
		if(isEncrypt){
189
			deskey = TemplateManager.getResKey();
190
		}
167 191
		if (key == null || "".equals(key))
168 192
			return null;
169 193
		if (StringUtil.isJSONArray(key)) {
170 194
			JSONArray keys = new JSONArray(key);
171
			JSONObject json =new JSONObject(SharedPrefUtil.get(Constant.MobileCache.IPU_MOBILE_STORAGE, keys));
195
			JSONObject json = null;
196
			if(isEncrypt){
197
				 json =new JSONObject(SharedPrefUtil.get(Constant.MobileCache.IPU_MOBILE_STORAGE, keys,deskey));
198
			}else{
199
				 json =new JSONObject(SharedPrefUtil.get(Constant.MobileCache.IPU_MOBILE_STORAGE, keys));
200
			}
172 201
			return json.toString();
173 202
		} else {
174
			return SharedPrefUtil.get(Constant.MobileCache.IPU_MOBILE_STORAGE, key, defValue);
203
			if(isEncrypt){
204
				return SharedPrefUtil.get(Constant.MobileCache.IPU_MOBILE_STORAGE, key, defValue,deskey);
205
			}else{
206
				return SharedPrefUtil.get(Constant.MobileCache.IPU_MOBILE_STORAGE, key, defValue);
207
			}
208
			
175 209
		}
176 210
	}
177 211