2017-06-02 21 views
0

我有问题在SharedPreference中加密我的数据,这是我当前的SharedPreference,保存数据未加密。我不知道什么加密数据。如何将列表数据加密到SharedPreference

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(); 
    } 

}

回答

0

如果你知道加密,您可以通过urself写它,它只是通过多种双向加密算法/散列如SHA,AES或别的东西处理加密数据。如果不是,您可以搜索“安全的SharedPreference”许多库或支持类在那里。

0

以下将帮助您

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(); 

}