Register now for Android Dev Summit 2019!

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)

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)

Opens an instance of encrypted SharedPreferences

Parameters
fileName String: The name of the file to open; can not contain path separators.

masterKeyAlias String

context Context

prefKeyEncryptionScheme EncryptedSharedPreferences.PrefKeyEncryptionScheme

prefValueEncryptionScheme EncryptedSharedPreferences.PrefValueEncryptionScheme

Returns
SharedPreferences The SharedPreferences instance that encrypts all data.

Throws
GeneralSecurityException when a bad master key or keyset has been attempted
IOException when fileName can not be used

getAll

public Map<String, ?> getAll ()

Returns
Map<String, ?>

getBoolean

public boolean getBoolean (String key, 
                boolean defValue)

Parameters
key String

defValue boolean

Returns
boolean

getFloat

public float getFloat (String key, 
                float defValue)

Parameters
key String

defValue float

Returns
float

getInt

public int getInt (String key, 
                int defValue)

Parameters
key String

defValue int

Returns
int

getLong

public long getLong (String key, 
                long defValue)

Parameters
key String

defValue long

Returns
long

getString

public String getString (String key, 
                String defValue)

Parameters
key String

defValue String

Returns
String

getStringSet

public Set<String> getStringSet (String key, 
                Set<String> defValues)

Parameters
key String

defValues Set

Returns
Set<String>

registerOnSharedPreferenceChangeListener

public void registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)

Parameters
listener SharedPreferences.OnSharedPreferenceChangeListener

unregisterOnSharedPreferenceChangeListener

public void unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)

Parameters
listener SharedPreferences.OnSharedPreferenceChangeListener