public static Object get(Context context, String fileName, String key, Cipher cipher) { try { String hex = get(context, fileName, key, (String) null); if (hex == null) return null; byte[] bytes = HexUtil.decodeHex(hex.toCharArray()); if (cipher != null) bytes = cipher.decrypt(bytes); Object obj = ByteUtil.byteToObject(bytes); Log.i(TAG, key + " get: " + obj); return obj; } catch (Exception e) { e.printStackTrace(); } return null; }
public static void put(Context context, String fileName, String key, Object ser, Cipher cipher) { SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE); try { Log.i(TAG, key + " put: " + ser); if (ser == null) { sp.edit().remove(key).commit(); } else { byte[] bytes = ByteUtil.objectToByte(ser); if (cipher != null) bytes = cipher.encrypt(bytes); put(context, fileName, key, HexUtil.encodeHexStr(bytes)); } } catch (Exception e) { e.printStackTrace(); } }