# EncryptedSharedPreferences

Artifact: [androidx.security:security-crypto](https://developer.android.com/jetpack/androidx/releases/security) [View Source](https://cs.android.com/search?q=file:androidx/security/crypto/EncryptedSharedPreferences.java+class:androidx.security.crypto.EncryptedSharedPreferences) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

*** ** * ** ***


```
public final class EncryptedSharedPreferences implements SharedPreferences
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> Use `https://developer.android.com/reference/android/content/SharedPreferences.html` instead.

An implementation of `https://developer.android.com/reference/android/content/SharedPreferences.html` that encrypts keys and values. **WARNING** : The preference file should not be backed up with Auto Backup. When restoring the file it is likely the key used to encrypt it will no longer be present. You should exclude all `EncryptedSharedPreference`s from backup using [backup rules](https://developer.android.com/guide/topics/data/autobackup#IncludingFiles). Basic use of the class:

```
 MasterKey masterKey = new MasterKey.Builder(context)
     .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
     .build();

 SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
     context,
     "secret_shared_prefs",
     masterKey,
     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 types |
|---|
| `public enum https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme` **This enum is deprecated.** Use `https://developer.android.com/reference/android/content/SharedPreferences.html` instead. <br /> |
| `public enum https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme` **This enum is deprecated.** Use `https://developer.android.com/reference/android/content/SharedPreferences.html` instead. <br /> |

| ### Public methods |
|---|---|
| `boolean` | `[contains](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#contains(java.lang.String))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key)` **This method is deprecated.** Deprecated in Java |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.html` | `[create](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#create(android.content.Context,java.lang.String,androidx.security.crypto.MasterKey,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html fileName, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey masterKey, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme )` **This method is deprecated.** Deprecated in Java |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.html` | `[create](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#create(java.lang.String,java.lang.String,android.content.Context,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html fileName, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html masterKeyAlias, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme )` **This method is deprecated.** Use `https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#create(android.content.Context,java.lang.String,androidx.security.crypto.MasterKey,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme)` instead. <br /> |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.Editor.html` | `[edit](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#edit())()` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/String.html, @https://developer.android.com/reference/androidx/annotation/NonNull ?>` | `[getAll](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getAll())()` **This method is deprecated.** Deprecated in Java |
| `boolean` | `[getBoolean](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getBoolean(java.lang.String,boolean))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key, boolean defValue)` **This method is deprecated.** Deprecated in Java |
| `float` | `[getFloat](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getFloat(java.lang.String,float))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key, float defValue)` **This method is deprecated.** Deprecated in Java |
| `int` | `[getInt](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getInt(java.lang.String,int))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key, int defValue)` **This method is deprecated.** Deprecated in Java |
| `long` | `[getLong](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getLong(java.lang.String,long))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key, long defValue)` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `[getString](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getString(java.lang.String,java.lang.String))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html defValue)` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html>` | `[getStringSet](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getStringSet(java.lang.String,java.util.Set<java.lang.String>))(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html key, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html> defValues)` **This method is deprecated.** Deprecated in Java |
| `void` | `[registerOnSharedPreferenceChangeListener](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html listener )` **This method is deprecated.** Deprecated in Java |
| `void` | `[unregisterOnSharedPreferenceChangeListener](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html listener )` **This method is deprecated.** Deprecated in Java |

## Public methods

### contains

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public boolean [contains](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#contains(java.lang.String))(@Nullable String key)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### create

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public static @NonNull SharedPreferences [create](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#create(android.content.Context,java.lang.String,androidx.security.crypto.MasterKey,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme))(
    @NonNull Context context,
    @NonNull String fileName,
    @NonNull MasterKey masterKey,
    @NonNull EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme,
    @NonNull EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Opens an instance of encrypted SharedPreferences

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html fileName` | The name of the file to open; can not contain path separators. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/MasterKey masterKey` | The master key to use. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme` | The scheme to use for encrypting keys. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme` | The scheme to use for encrypting values. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.html` | The SharedPreferences instance that encrypts all data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/security/GeneralSecurityException.html` | when a bad master key or keyset has been attempted |
| `https://developer.android.com/reference/java/io/IOException.html` | when fileName can not be used |

### create

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public static @NonNull SharedPreferences [create](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#create(java.lang.String,java.lang.String,android.content.Context,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme))(
    @NonNull String fileName,
    @NonNull String masterKeyAlias,
    @NonNull Context context,
    @NonNull EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme,
    @NonNull EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use `https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#create(android.content.Context,java.lang.String,androidx.security.crypto.MasterKey,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme)` instead.

Opens an instance of encrypted SharedPreferences

If the `masterKeyAlias` used here is for a key that is not yet created, this method will not be thread safe. Use the alternate signature that is not deprecated for multi-threaded contexts.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html fileName` | The name of the file to open; can not contain path separators. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html masterKeyAlias` | The alias of the master key to use. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context to use to open the preferences file. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme` | The scheme to use for encrypting keys. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme` | The scheme to use for encrypting values. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/SharedPreferences.html` | The SharedPreferences instance that encrypts all data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/security/GeneralSecurityException.html` | when a bad master key or keyset has been attempted |
| `https://developer.android.com/reference/java/io/IOException.html` | when fileName can not be used |

### edit

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull SharedPreferences.Editor [edit](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#edit())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getAll

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull Map<String, @NonNull ?> [getAll](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getAll())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getBoolean

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public boolean [getBoolean](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getBoolean(java.lang.String,boolean))(@Nullable String key, boolean defValue)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getFloat

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public float [getFloat](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getFloat(java.lang.String,float))(@Nullable String key, float defValue)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getInt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public int [getInt](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getInt(java.lang.String,int))(@Nullable String key, int defValue)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getLong

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public long [getLong](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getLong(java.lang.String,long))(@Nullable String key, long defValue)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getString

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @Nullable String [getString](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getString(java.lang.String,java.lang.String))(@Nullable String key, @Nullable String defValue)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### getStringSet

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @Nullable Set<String> [getStringSet](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#getStringSet(java.lang.String,java.util.Set<java.lang.String>))(@Nullable String key, @Nullable Set<String> defValues)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### registerOnSharedPreferenceChangeListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public void [registerOnSharedPreferenceChangeListener](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))(
    @NonNull SharedPreferences.OnSharedPreferenceChangeListener listener
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

### unregisterOnSharedPreferenceChangeListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public void [unregisterOnSharedPreferenceChangeListener](https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))(
    @NonNull SharedPreferences.OnSharedPreferenceChangeListener listener
)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java