Android Shared Preferences Encryption using jetpack library or How to Secure Android Shared Preferences? or Real and Write Encrypted Shared Preferences in android.
Example below:-
try {
String mainKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
"example",
mainKey,
this,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
// use the shared preferences and editor as you normally would
@SuppressLint("CommitPrefEdits")
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("id", 23);
editor.apply();
Log.e("tag", "id: " + sharedPreferences.getInt("id", 0));
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Comments
Post a Comment