# 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)

*** ** * ** ***


```
class EncryptedSharedPreferences : 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:

```kotlin
 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 |
|---|
| `enum https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme` **This enum is deprecated.** Use `https://developer.android.com/reference/android/content/SharedPreferences.html` instead. <br /> |
| `enum https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme` **This enum is deprecated.** Use `https://developer.android.com/reference/android/content/SharedPreferences.html` instead. <br /> |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `[contains](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#contains(java.lang.String))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` **This function is deprecated.** Deprecated in Java |
| `java-static https://developer.android.com/reference/android/content/SharedPreferences.html` | `[create](https://developer.android.com/reference/kotlin/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))( context: https://developer.android.com/reference/android/content/Context.html, fileName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, masterKey: https://developer.android.com/reference/kotlin/androidx/security/crypto/MasterKey, prefKeyEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme, prefValueEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme )` **This function is deprecated.** Deprecated in Java |
| `java-static https://developer.android.com/reference/android/content/SharedPreferences.html` | `[create](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#create(java.lang.String,java.lang.String,android.content.Context,androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme,androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme))( fileName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, masterKeyAlias: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, context: https://developer.android.com/reference/android/content/Context.html, prefKeyEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme, prefValueEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme )` **This function is deprecated.** Use `https://developer.android.com/reference/kotlin/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://developer.android.com/reference/android/content/SharedPreferences.Editor.html` | `[edit](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#edit())()` **This function is deprecated.** Deprecated in Java |
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-map/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-map/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!, *>` | `[getAll](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getAll())()` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `[getBoolean](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getBoolean(java.lang.String,boolean))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, defValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | `[getFloat](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getFloat(java.lang.String,float))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, defValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `[getInt](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getInt(java.lang.String,int))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, defValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `[getLong](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getLong(java.lang.String,long))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, defValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?` | `[getString](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getString(java.lang.String,java.lang.String))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, defValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?)` **This function is deprecated.** Deprecated in Java |
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>?` | `[getStringSet](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getStringSet(java.lang.String,java.util.Set<java.lang.String>))(key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html?, defValues: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>?)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[registerOnSharedPreferenceChangeListener](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))( listener: https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html )` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[unregisterOnSharedPreferenceChangeListener](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))( listener: https://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html )` **This function is deprecated.** Deprecated in Java |

## Public functions

### 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)

```
fun [contains](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#contains(java.lang.String))(key: String?): Boolean
```

> [!CAUTION]
> **This function 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)

```
java-static fun [create](https://developer.android.com/reference/kotlin/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))(
    context: Context,
    fileName: String,
    masterKey: MasterKey,
    prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme,
    prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme
): SharedPreferences
```

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

Opens an instance of encrypted SharedPreferences

| Parameters |
|---|---|
| `fileName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the file to open; can not contain path separators. |
| `masterKey: https://developer.android.com/reference/kotlin/androidx/security/crypto/MasterKey` | The master key to use. |
| `prefKeyEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme` | The scheme to use for encrypting keys. |
| `prefValueEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme` | The scheme to use for encrypting values. |

| Returns |
|---|---|
| `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)

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

> [!CAUTION]
> **This function is deprecated.**   
>
> Use `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `fileName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the file to open; can not contain path separators. |
| `masterKeyAlias: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The alias of the master key to use. |
| `context: https://developer.android.com/reference/android/content/Context.html` | The context to use to open the preferences file. |
| `prefKeyEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefKeyEncryptionScheme` | The scheme to use for encrypting keys. |
| `prefValueEncryptionScheme: https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.PrefValueEncryptionScheme` | The scheme to use for encrypting values. |

| Returns |
|---|---|
| `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)

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

> [!CAUTION]
> **This function 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)

```
fun [getAll](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getAll())(): (Mutable)Map<String!, *>
```

> [!CAUTION]
> **This function 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)

```
fun [getBoolean](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getBoolean(java.lang.String,boolean))(key: String?, defValue: Boolean): Boolean
```

> [!CAUTION]
> **This function 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)

```
fun [getFloat](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getFloat(java.lang.String,float))(key: String?, defValue: Float): Float
```

> [!CAUTION]
> **This function 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)

```
fun [getInt](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getInt(java.lang.String,int))(key: String?, defValue: Int): Int
```

> [!CAUTION]
> **This function 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)

```
fun [getLong](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getLong(java.lang.String,long))(key: String?, defValue: Long): Long
```

> [!CAUTION]
> **This function 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)

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

> [!CAUTION]
> **This function 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)

```
fun [getStringSet](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#getStringSet(java.lang.String,java.util.Set<java.lang.String>))(key: String?, defValues: (Mutable)Set<String!>?): (Mutable)Set<String!>?
```

> [!CAUTION]
> **This function 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)

```
fun [registerOnSharedPreferenceChangeListener](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))(
    listener: SharedPreferences.OnSharedPreferenceChangeListener
): Unit
```

> [!CAUTION]
> **This function 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)

```
fun [unregisterOnSharedPreferenceChangeListener](https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences#unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener))(
    listener: SharedPreferences.OnSharedPreferenceChangeListener
): Unit
```

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