如何加密列表数据到共享preference



我有问题加密我的数据,这是我当前的共享preference,其中包括保存数据而不是加密。我不知道什么加密数据。

 public class MySharedPreference {
//this is name PREFS_NAME
        public static final String PREFS_NAME = "LIST_CARD";
//this is CARD where are save data Card 
        public static final String CARD = "CARD";
        public MySharedPreference() {
            super();
        }
//this is functon witch save array list to Sharepreference 
    public void saveCardToSharedPreference(Context context, ArrayList<Card> cardList) {
        SharedPreferences settings;
        SharedPreferences.Editor editor;
        settings = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = settings.edit();
        Gson gson = new Gson();
        String jsonString = gson.toJson(cardList);
        editor.putString(CARD, jsonString);
        editor.commit();
    }

}

如果您知道加密,则可以通过自己编写,它只是通过多种双向加密算法/哈希(例如SHA,AES或其他东西(来处理加密数据。如果否,您可以在那里搜索"有安全的共享"或"支持类别"。

以下将帮助您

static SharedPreferences.Editor editor;
public static ArrayList<Card> getListSharedPref(Context context) {
    SharedPreferences sharedPref= PreferenceManager.getDefaultSharedPreferences(context);
    String json=sharedPref.getString("LIST_PREF",null);
    Gson gson=new Gson();
    Type type=new TypeToken<ArrayList<Card>>(){}.getType();
    ArrayList<Card> beans = null ;
    try {
        beans = gson.fromJson(json, type);
    } catch(Exception e) {
        return null ;
    }
    return beans;
}
public static void setListSharedPref(Context context, ArrayList<Card> cardList) {
    SharedPreferences sharedPref= PreferenceManager.getDefaultSharedPreferences(context);
    editor = sharedPref.edit();
    Gson gson=new Gson();
    String json=gson.toJson(cardList);
    editor.putString("LIST_PREF",json);
    editor.commit();
}

最新更新