赞
踩
【5年Android从零复盘系列之二十六】Android存储(1):Sharedpreference
sp存储轻量数据,如果隐私度较高,建议简单加密处理一下,不推荐使用加密后含有特殊符号的加密方式。
/** * 加密 * * @param password 待加密String * @return 加密后的String */ public static String encrypt(String password) { long enMills = System.currentTimeMillis(); char[] array = password.toCharArray(); StringBuilder builder = new StringBuilder(); for (char c : array) { builder.append(c); builder.append(new Random().nextInt(9)); } //最后添加一个随机字符 long mill = System.currentTimeMillis() - enMills; if (mill % 3 == 0) { builder.append("g"); } if (mill % 3 == 1) { builder.append("a"); } if (mill % 3 == 2) { builder.append("k"); } return builder.toString(); } /** * 解密 * * @param password 加密过的 String * @return 解密后的String */ public static String decrypt(String password) { char[] array = password.toCharArray(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < array.length - 1; i++) {//舍弃最后一个随机字符 builder.append(array[i]); i++; } return builder.toString(); }
package com.cupster.spcache; import android.content.Context; import android.content.SharedPreferences; import android.util.Base64; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import java.util.Set; /** * code by Cupster 2019.02.06 */ public final class SPCache { private static final String SP_SETTING = "setting";//sp文件setting.xml private static final String SP_DATA = "data";//sp文件data.xml private static SharedPreferences getPreference(Context context, String spName) { return context.getSharedPreferences(spName, 0); } private static SharedPreferences.Editor getEditor(Context context, String spName) { return getPreference(context, spName).edit(); } /** * 设置 Setting.sp 的键值对 * * @param context context * @param key key * @param value value */ public static void putSetting(Context context, String key, String value) { getEditor(context, SP_SETTING).putString(key, value).apply(); } public static void putSetting(Context context, String key, int value) { getEditor(context, SP_SETTING).putInt(key, value).apply(); } public static void putSetting(Context context, String key, boolean value) { getEditor(context, SP_SETTING).putBoolean(key, value).apply(); } public static void putSetting(Context context, String key, float value) { getEditor(context, SP_SETTING).putFloat(key, value).apply(); } public static void putSetting(Context context, String key, long value) { getEditor(context, SP_SETTING).putLong(key, value).apply(); } public static void putSetting(Context context, String key, Set<String> value) { getEditor(context, SP_SETTING).putStringSet(key, value).apply(); } /** * 获取 Setting.sp 的数据 * * @param context context * @param key key * @param defValue 无数据时替代值 * @return 值 */ public static String getSetting(Context context, String key, String defValue) { return getPreference(context, SP_SETTING).getString(key, defValue); } public static int getSetting(Context context, String key, int defValue) { return getPreference(context, SP_SETTING).getInt(key, defValue); } public static boolean getSetting(Context context, String key, boolean defValue) { return getPreference(context, SP_SETTING).getBoolean(key, defValue); } public static float getSetting(Context context, String key, float defValue) { return getPreference(context, SP_SETTING).getFloat(key, defValue); } public static long getSetting(Context context, String key, long defValue) { return getPreference(context, SP_SETTING).getLong(key, defValue); } public static Set<String> getSetting(Context context, String key) { return getPreference(context, SP_SETTING).getStringSet(key, null); } /** * 设置 Setting.sp 的键值对 * * @param context context * @param key key * @param value value */ public static void putData(Context context, String key, String value) { getEditor(context, SP_DATA).putString(key, value).apply(); } /** * 获取 Setting.sp 的数据 * * @param context context * @param key key * @param defValue 无数据时替代值 * @return 值 */ public static String getData(Context context, String key, String defValue) { return getPreference(context, SP_DATA).getString(key, defValue); } /** * 加密 * * @param password 待加密String * @return 加密后的String */ public static String encrypt(String password) { long enMills = System.currentTimeMillis(); char[] array = password.toCharArray(); StringBuilder builder = new StringBuilder(); for (char c : array) { builder.append(c); builder.append(new Random().nextInt(9)); } //最后添加一个随机字符 long mill = System.currentTimeMillis() - enMills; if (mill % 3 == 0) { builder.append("g"); } if (mill % 3 == 1) { builder.append("a"); } if (mill % 3 == 2) { builder.append("k"); } return builder.toString(); } /** * 解密 * * @param password 加密过的 String * @return 解密后的String */ public static String decrypt(String password) { char[] array = password.toCharArray(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < array.length - 1; i++) {//舍弃最后一个随机字符 builder.append(array[i]); i++; } return builder.toString(); } /** * 将对象转换成byte数组,并将其进行base64编码 * * @param object base64编码前String * @return 编码后String */ public static String base64Encrypt(Object object) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(baos); oos.writeObject(object); } catch (IOException e) { e.printStackTrace(); } // 将对象放到OutputStream中 // 将对象转换成byte数组,并将其进行base64编码 String objectStr = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT)); try { baos.close(); if (oos != null) { oos.close(); } } catch (Exception e) { e.printStackTrace(); } return objectStr; } /** * 将base64格式字符串还原成byte数组 * * @param string base64编码前String * @return base64解码后的对象 */ public static Object base64Decrypt(String string) { try { if (string == null || string.equals("")) { // 不可少,否则在下面会报java.io.StreamCorruptedException return ""; } byte[] objBytes = Base64.decode(string.getBytes(), Base64.DEFAULT); ByteArrayInputStream bais = new ByteArrayInputStream(objBytes); ObjectInputStream ois = new ObjectInputStream(bais); // 将byte数组转换成product对象 Object obj = ois.readObject(); bais.close(); ois.close(); return obj; } catch (Exception e) { e.printStackTrace(); } return ""; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。