EncryptedSharedPreferences

public final class EncryptedSharedPreferences
extends Object implements SharedPreferences

java.lang.Object
   ↳ androidx.security.crypto.EncryptedSharedPreferences


An implementation of SharedPreferences that encrypts keys and values.

  String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);

  SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
      "secret_shared_prefs",
      masterKeyAlias,
      context,
      EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
      EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
  );

  // use the shared preferences and editor as you normally would
  SharedPreferences.Editor editor = sharedPreferences.edit();
 

Summary

Nested classes

enum EncryptedSharedPreferences.PrefKeyEncryptionScheme

The encryption scheme to encrypt keys. 

enum EncryptedSharedPreferences.PrefValueEncryptionScheme

The encryption scheme to encrypt values. 

Public methods

boolean contains(String key)
static SharedPreferences create(String fileName, String masterKeyAlias, Context context, EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)

This method is deprecated. Use create(Context, String, MasterKey, PrefKeyEncryptionScheme, PrefValueEncryptionScheme) instead.

static SharedPreferences create(Context context, String fileName, MasterKey masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)

Opens an instance of encrypted SharedPreferences

SharedPreferences.Editor edit()
Map<String, ?> getAll()
boolean getBoolean(String key, boolean defValue)
float getFloat(String key, float defValue)
int getInt(String key, int defValue)
long getLong(String key, long defValue)
String getString(String key, String defValue)
Set<String> getStringSet(String key, Set<String> defValues)
void registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)
void unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener)

Inherited methods

Public methods

contains

public boolean contains (String key)

Parameters
key String

Returns
boolean

create

public static SharedPreferences create (String fileName, 
                String masterKeyAlias, 
                Context context, 
                EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, 
                EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)

This method is deprecated.
Use create(Context, String, MasterKey, PrefKeyEncryptionScheme, PrefValueEncryptionScheme) instead.

Opens an instance of enc