I need to perform simple encryption in SharedPreferences
.
So I found these 2 simple methods, one to encrypt the other, on the contrary, to get data.
private static String encrypt(String input) { return Base64.encodeToString(input.getBytes(), Base64.DEFAULT); } private static String decrypt(String input) { return new String(Base64.decode(input, Base64.DEFAULT)); }
And you need to use them like this
// Write SharedPreferences preferences = getSharedPreferences("some_prefs_name", MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString(encrypt("password"), encrypt("dummypass")); editor.apply(); // Or commit if targeting old devices // Read SharedPreferences preferences = getSharedPreferences("some_prefs_name", MODE_PRIVATE); String pass = preferences.getString(decrypt("password"), "default");
And now I have a question, will this method work correctly when saving and retrieving boolean and int values? As this example shows how to work with String
...