Reputation: 51
How to implement encrypted sharepreferences in my android java apps using https://developer.android.google.cn/reference/androidx/security/crypto/EncryptedSharedPreferences? I dont know how to implement it, anyone can help?
Upvotes: 4
Views: 6184
Reputation: 71
MasterKeys.getOrCreate is deprecated, you have to use this:
public SharedPreferences getEncryptedSharedPreferences() throws GeneralSecurityException, IOException {
MasterKey masterKeyAlias = new MasterKey.Builder(getApplicationContext())
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
getApplicationContext(),
"secret_shared_prefs_file",
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
return sharedPreferences;
}
Upvotes: 0
Reputation: 317
I was using a similar code to what @Chirag wrote but after I applied new updates to my Android Studio 4.0 project, I was getting a warning that MasterKeys
class is deprecated.
So I found this answer and it did the trick. Here is the code in a fragment. If you want to use it in your MainActivity, change getContext()
to this
MasterKey getMasterKey() {
try {
KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(
"_androidx_security_master_key_",
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.build();
return new MasterKey.Builder(getContext())
.setKeyGenParameterSpec(spec)
.build();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error on getting master key", e);
}
return null;
}
private SharedPreferences getEncryptedSharedPreferences() {
try {
return EncryptedSharedPreferences.create(
Objects.requireNonNull(getContext()),
"Your preference file name",
getMasterKey(), // calling the method above for creating MasterKey
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error on getting encrypted shared preferences", e);
}
return null;
}
Then you can use the above like this:
public void someFunction(){
SharedPreferences sharedPreferences = getEncryptedSharedPreferences();
//Used to add new entries and save changes
SharedPreferences.Editor editor = sharedPreferences.edit();
//To add entry to your shared preferences file
editor.putString("Name", "Value");
editor.putBoolean("Name", false);
//Apply changes and commit
editor.apply();
editor.commit();
//To clear all keys from your shared preferences file
editor.clear().apply();
//To get a value from your shared preferences file
String returnedValue = sharedPreferences.getString("Name", "Default value if null is returned or the key doesn't exist");
}
Upvotes: 5
Reputation: 931
As per the documentation example, you can init EncryptedSharedPreferences
like this.
public SharedPreferences getEncryptedSharedPreferences(){
String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
"secret_shared_prefs_file",
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
return sharedPreferences;
}
Save Data
getEncryptedSharedPreferences().edit()
.putString("key", value)
.apply()
Get Data
getEncryptedSharedPreferences().getString("key", "defaultValue");
Make sure your app API version is 23+ and you need to add this dependency
implementation "androidx.security:security-crypto:1.0.0-alpha02" //Use latest version
Upvotes: 4