Android SharedPreferences加密存储方法
程序员文章站
2022-06-27 17:45:00
这一篇是整理 Android本地数据SP加密存储的方法,也是属于APP安全范畴。主要是通过AES加密方法进行加密,对Key和Value全部加密,进一步保障数据安全方法整理如下:import android.content.Context;import android.content.SharedPreferences;import android.preference.PreferenceManager;import android.text.TextUtils;import an...
这一篇是整理 Android本地数据SP加密存储的方法,也是属于APP安全范畴。主要是通过AES加密方法进行加密,对Key和Value全部加密,进一步保障数据安全
方法整理如下:
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.jlpay.connect.utils.AESUtils;
import com.jlpay.partner.application.PartnerApp;
/**
* Created by on 2020/12/04.
*/
public class SpUtils {
public static Context context = PartnerApp.getContext();
/**
* SP加密存储,Key 和 Value 均加密
* 将所有类型数据都转为 String 后加密
*/
private static void encryptPut(@NonNull String key, @NonNull Object value) {
String valueStr = null;
if (value instanceof String) {
valueStr = (String) value;
} else if (value instanceof Boolean) {
valueStr = Boolean.toString((Boolean) value);
} else if (value instanceof Integer) {
valueStr = Integer.toString((Integer) value);
} else if (value instanceof Float) {
valueStr = Float.toString((Float) value);
} else if (value instanceof Long) {
valueStr = Long.toString((Long) value);
}
if (valueStr != null && !TextUtils.isEmpty(valueStr)) {
byte[] encryptValue = AESUtils.encrypt(AESUtils.AES_KEY.getBytes(), valueStr.getBytes());
String base64ValueStr = RSAUtils.byteToBase64Str(encryptValue);
byte[] encryptKey = AESUtils.encrypt(AESUtils.AES_KEY.getBytes(), key.getBytes());
String base64KeyStr = RSAUtils.byteToBase64Str(encryptKey);
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
settings.edit().putString(base64KeyStr, base64ValueStr).apply();
}
}
/**
* SP解密
*/
private static Object decryptGet(@NonNull String key, @NonNull Object defaultvalue) {
String valueStr = null;
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
byte[] encryptKey = AESUtils.encrypt(AESUtils.AES_KEY.getBytes(), key.getBytes());
String base64KeyStr = RSAUtils.byteToBase64Str(encryptKey);
if (defaultvalue instanceof String) {
valueStr = (String) defaultvalue;
String settingsString = settings.getString(base64KeyStr, valueStr);
if (settingsString != null && !settingsString.equals(valueStr)) {
byte[] bytes = RSAUtils.base64StrToByte(settingsString);
byte[] decryptByte = AESUtils.decrypt(AESUtils.AES_KEY.getBytes(), bytes);
String decryptStr = new String(decryptByte).trim();
return decryptStr;
}
return valueStr;
} else if (defaultvalue instanceof Boolean) {
valueStr = Boolean.toString((Boolean) defaultvalue);
String settingsString = settings.getString(base64KeyStr, valueStr);
if (settingsString != null && !settingsString.equals(valueStr)) {
byte[] bytes = RSAUtils.base64StrToByte(settingsString);
byte[] decryptByte = AESUtils.decrypt(AESUtils.AES_KEY.getBytes(), bytes);
String decryptStr = new String(decryptByte).trim();
return Boolean.valueOf(decryptStr);
}
return (boolean) defaultvalue;
} else if (defaultvalue instanceof Integer) {
valueStr = Integer.toString((Integer) defaultvalue);
String settingsString = settings.getString(base64KeyStr, valueStr);
if (settingsString != null && !settingsString.equals(valueStr)) {
byte[] bytes = RSAUtils.base64StrToByte(settingsString);
byte[] decryptByte = AESUtils.decrypt(AESUtils.AES_KEY.getBytes(), bytes);
String decryptStr = new String(decryptByte).trim();
return Integer.valueOf(decryptStr);
}
return (int) defaultvalue;
} else if (defaultvalue instanceof Float) {
valueStr = Float.toString((Float) defaultvalue);
String settingsString = settings.getString(base64KeyStr, valueStr);
if (settingsString != null && !settingsString.equals(valueStr)) {
byte[] bytes = RSAUtils.base64StrToByte(settingsString);
byte[] decryptByte = AESUtils.decrypt(AESUtils.AES_KEY.getBytes(), bytes);
String decryptStr = new String(decryptByte).trim();
return Float.valueOf(decryptStr);
}
return (float) defaultvalue;
} else if (defaultvalue instanceof Long) {
valueStr = Long.toString((Long) defaultvalue);
String settingsString = settings.getString(base64KeyStr, valueStr);
if (settingsString != null && !settingsString.equals(valueStr)) {
byte[] bytes = RSAUtils.base64StrToByte(settingsString);
byte[] decryptByte = AESUtils.decrypt(AESUtils.AES_KEY.getBytes(), bytes);
String decryptStr = new String(decryptByte).trim();
return Long.valueOf(decryptStr);
}
return (long) defaultvalue;
}
return null;
}
public static String getPrefString(String key, final String defaultValue) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// return settings.getString(key, defaultValue);
return (String) decryptGet(key, defaultValue);
}
public static void setPrefString(final String key, final String value) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// settings.edit().putString(key, value).apply();
encryptPut(key, value);
}
public static boolean getPrefBoolean(final String key, final boolean defaultValue) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// return settings.getBoolean(key, defaultValue);
return (boolean) decryptGet(key, defaultValue);
}
public static boolean hasKey(final String key) {
return PreferenceManager.getDefaultSharedPreferences(context).contains(key);
}
public static void setPrefBoolean(final String key, final boolean value) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// settings.edit().putBoolean(key, value).apply();
encryptPut(key, value);
}
public static void setPrefInt(final String key, final int value) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// settings.edit().putInt(key, value).apply();
encryptPut(key, value);
}
public static int getPrefInt(final String key, final int defaultValue) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// return settings.getInt(key, defaultValue);
return (int) decryptGet(key, defaultValue);
}
public static void setPrefFloat(final String key, final float value) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// settings.edit().putFloat(key, value).apply();
encryptPut(key, value);
}
public static float getPrefFloat(final String key, final float defaultValue) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// return settings.getFloat(key, defaultValue);
return (float) decryptGet(key, defaultValue);
}
public static void setSettingLong(final String key, final long value) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// settings.edit().putLong(key, value).apply();
encryptPut(key, value);
}
public static long getPrefLong(final String key, final long defaultValue) {
// final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// return settings.getLong(key, defaultValue);
return (long) decryptGet(key, defaultValue);
}
public static void clearPreference(final SharedPreferences p) {
final SharedPreferences.Editor editor = p.edit();
editor.clear();
editor.apply();
}
}
下面是导出来的 SP文件截图:这样也无法凭Key去猜测了
本文地址:https://blog.csdn.net/fitaotao/article/details/110649420
上一篇: 【奇技淫巧】-- 走地图的不同路径
下一篇: 电商五大趋势分析 电商创业赚钱需注意
推荐阅读
-
Android 图片存储到指定路径和相册的方法
-
Android获取内外置存储卡的方法
-
Android使用文件进行数据存储的方法
-
android中SharedPreferences实现存储用户名功能
-
Android 存储SharedPreferences出现无法获取到数据
-
Android开发之使用SQLite存储数据的方法分析
-
Android存储:sharedpreferences存储一个Map
-
优酷视频下载以及破解优酷超清视频加密存储文件方法教程详解
-
Android数据存储之SharedPreferences
-
Android 入门第七讲01-数据存储(数据存储概述,文件存储(raw和asserts目录读写,data/data/包名目录读写,sdcard目录读写),SharedPreferences读写)