In the Android Keystore, where should the key that encrypts a session token live now that EncryptedSharedPreferences is deprecated, and what does the app hold?
answer
- a provider name, not a file
- KeyGenerator plus a KeyGenParameterSpec
- key material never enters the process
- security-crypto 1.1.0 deprecated everything
- the SecretKey is a handle, not bytes
basics
~20 sGenerate the key inside the AndroidKeyStore provider, which security-crypto 1.1.0 points to after deprecating EncryptedSharedPreferences. The app holds only a SecretKey handle: the Keystore performs each encryption outside the app's process and never exports the key material.
solid answer
~40 sSince `androidx.security:security-crypto` 1.1.0, every API in the library, `EncryptedSharedPreferences` and `MasterKey` included, is deprecated in favour of platform APIs and direct use of the Android Keystore; `MasterKey`'s note says to use `KeyGenerator` with the `AndroidKeyStore` provider instead. So the app calls `KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")` with a `KeyGenParameterSpec`, and later reloads the key with `KeyStore.getInstance("AndroidKeyStore")`, `load(null)` and `getKey(alias, null)`. What comes back is a `SecretKey` handle, not key bytes: plaintext and ciphertext are fed to a system process, and when the device's TEE or StrongBox supports the key's settings, the material stays inside that hardware. The app keeps only the ciphertext in its own storage, and the purposes, modes and authentication rules it set at generation cannot be changed afterwards.
go deeper
Recall the provider name AndroidKeyStore, the KeyGenerator call that creates the key, and that security-crypto 1.1.0 deprecated EncryptedSharedPreferences in favour of using the Keystore directly.
Explain that getKey returns a non-exportable handle, that operations run in a system process, and that the ciphertext and IV live in ordinary app storage.
Show you plan for device-bound keys: restored ciphertext that cannot be decrypted, migration off EncryptedSharedPreferences, and Keystore work kept off the main thread.
Frame the Keystore as the team's single home for secrets and set the rule that no key ships in code or leaves the provider.
## What the AndroidKeyStore provider is The **Android Keystore system** is a container for cryptographic keys that apps can *use* but not *read*. An app reaches it through the standard Java cryptography classes with one special provider name, `"AndroidKeyStore"`: - `KeyGenerator` and `KeyPairGenerator` create new keys inside it. - `java.security.KeyStore` of type `AndroidKeyStore` lists, loads and deletes entries. - `Cipher`, `Signature` and `Mac` run operations with those keys. The Android documentation describes two protections. First, **key material never enters the application process**: when the app encrypts with a Keystore key, the plaintext and ciphertext are handed to a system process that does the work. If the app's process is compromised, an attacker may be able to *use* the key while in control, but cannot *extract* it. Second, key material can be **bound to secure hardware**, a Trusted Execution Environment (TEE) or a secure element such as StrongBox, so that even a compromised OS cannot copy it off the device. ## Generating and loading a token key A session token needs a symmetric key, so the app generates an AES key once and gives it an **alias**: 1. Build a `KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)` with the block mode and padding the key may be used with. 2. Call `KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")`, then `init(spec)` and `generateKey()`. 3. On later launches, call `KeyStore.getInstance("AndroidKeyStore")`, `load(null)` and `getKey(alias, null)`; no password is involved. The object returned is a `SecretKey` that works with `Cipher.init`, but its material cannot be read out. The Keystore also records the key's **authorizations** at generation: allowed purposes, block modes, paddings, and whether the user must authenticate. The documentation states that once a key is generated these cannot be changed, and the Keystore enforces them outside the app's process. | What | Where it lives | |---|---| | AES key material | Inside the Keystore (secure hardware when available) | | The key's alias | Known to the app, used to look the key up | | Ciphertext and IV | The app's own storage: a preferences file, a database, a file | | Plaintext token | In memory only, while it is being used | ## What replaced EncryptedSharedPreferences `EncryptedSharedPreferences` wrapped `SharedPreferences` and encrypted keys and values with keys protected by a `MasterKey`. The `security-crypto` 1.1.0 release notes say every API was **deprecated in favour of existing platform APIs and direct use of Android Keystore**. The class-level notes are specific: - `EncryptedSharedPreferences`: use `android.content.SharedPreferences` instead. - `MasterKey`: use `javax.crypto.KeyGenerator` with an `AndroidKeyStore` instance instead. - `EncryptedFile`: use `java.io.File` instead. Read together, the replacement is: the app generates its own key in `AndroidKeyStore`, encrypts the value itself, and writes the ciphertext to ordinary storage. Deprecated is not removed: the 1.1.0 classes still exist, which gives existing apps time to migrate stored values to their own key. A migration usually runs once per install: 1. Open the old `EncryptedSharedPreferences` file and read the token. 2. Generate an `AndroidKeyStore` AES key and encrypt the token with it. 3. Write the ciphertext and IV to plain preferences or another store. 4. Delete the old file, and drop the library once every supported release has migrated. ## What the Keystore does not do - **It does not hold the app's data.** Entries are keys and certificates; the encrypted token is the app's to store. - **It does not stop a compromised app from using a key.** It stops extraction. Limiting *use* is what authorizations such as required user authentication are for. - **It does not choose the algorithm settings for you.** The purposes, block modes and paddings come from the `KeyGenParameterSpec` the app writes. ## Practical rules - **Keep Keystore calls off the main thread.** The Keystore documentation warns that cryptographic operations may be time-consuming and suggests `StrictMode` to find main-thread use. - **Treat the key as device-bound.** The library's own warning about Auto Backup applies to any Keystore-encrypted data: a restored ciphertext usually arrives on a device where the key is absent, so the app must handle an undecryptable value by signing in again. - **Never ship a key in the APK.** A key in code or resources can be read by anyone who unpacks the app; a Keystore key never exists outside the Keystore. - **KeyChain is a different API.** `KeyChain` gives several apps access to system-wide credentials the user picks; the `AndroidKeyStore` provider holds keys only the generating app can use. ## Why interviewers ask it The answer shows whether a candidate knows where secrets actually sit on Android today. A strong answer names the provider, says the app holds a handle rather than bytes, and knows that `EncryptedSharedPreferences` is deprecated rather than recommended.
- Can an attacker who takes over the app's process steal a Keystore key?Not its material. The Android documentation says that with a compromised app process an attacker might be able to use the app's keys while in control, but cannot extract their material for use elsewhere. Keys bound to secure hardware go further: even a compromised OS can use them on the device but cannot copy them off it.
- Do existing EncryptedSharedPreferences files stop working once an app moves to security-crypto 1.1.0?No. Version 1.1.0 deprecates the classes but still ships them, so existing files can still be read. The migration is to read each value once, encrypt it with the app's own `AndroidKeyStore` key, write the ciphertext to plain storage, and then delete the old file.
- Should the app call the Keystore from the main thread?The Keystore documentation advises against it: cryptographic operations can take time because they run in a system process and possibly in secure hardware. Run key generation and bulk encryption on a background dispatcher, and use `StrictMode` to find main-thread calls.
A notary who stamps any document you bring but never lends out the seal: the Keystore performs encryption on request, while the key itself never leaves its hands.
- EncryptedSharedPreferences is still the recommended way to store a token
- KeyStore.getKey returns the raw AES bytes, which the app can copy elsewhere
- A key hardcoded in the app is safe once the code is obfuscated
- Keystore keys move to a new phone with the app's Auto Backup data
- The Keystore stores the encrypted token itself, so the app stores nothing