Storing saved passwords, cards and tokens in an Android app
androidkeystoreaes-gcmpasswordsautofillcredit-cardsbiometricclipboardtokens
An app that saves logins, payment cards or a service token for the user (here the Tokay browser's password manager, card and address autofill, and Kagi session token) should keep them encrypted at rest with a key that cannot leave the phone, and should treat the screen lock as a UI gate rather than as the encryption itself. This is the design Tokay settled on in 2026-10, and why.
Encryption at rest
- Generate one AES-256-GCM key in the AndroidKeyStore (
KeyGenParameterSpecwithPURPOSE_ENCRYPT | PURPOSE_DECRYPT,BLOCK_MODE_GCM, no padding). The key material never leaves the keystore (hardware-backed where the phone has a TEE or StrongBox), so a copy of the database or app files is useless on another device. - Encrypt each secret value separately and store it as a versioned string:
"v1:" + base64(iv || ciphertext+tag), with the keystore choosing a fresh 12-byte IV for each encryption. The prefix leaves room to change the scheme later and makes "not encrypted yet" detectable. - Migrate plaintext on first read: when an older version stored a token in plain preferences, read it, write the encrypted form under a new key, delete the old one.
- Decrypt failures (the key is gone after a factory reset or a restored backup) return "empty", not a crash; the user re-enters the secret.
- Keep secrets in their own database file (logins, cards, addresses) and set
android:allowBackup="false"(or exclude those files) so they never ride along into a cloud backup where the key cannot follow. - The decrypted list is kept in memory while the app runs. That is a deliberate trade-off: the threat model is a lost phone or a copied file, not malware with root on the running phone.
The screen-lock gate
- Opening the password list, showing a saved card, or filling a card asks for BiometricPrompt with
BIOMETRIC_WEAK | DEVICE_CREDENTIAL, only when the phone actually has a screen lock (KeyguardManager.isDeviceSecure). Without a lock there is nothing to ask for, so hide that setting instead of pretending. - Declare
android.permission.USE_BIOMETRICin the manifest. It is a normal permission (no runtime prompt), and without itBiometricPrompt.authenticatethrowsSecurityException: Must have USE_BIOMETRIC permission. It only throws on a phone that has a screen lock, so an emulator or test phone without one never shows the crash, and every real user's phone does. Tokay shipped that way for weeks: filling a saved card or opening the password list crashed the browser. Test the gate with a lock set (adb shell locksettings set-pin 1234, see Driving the Android emulator headlessly for QA), and wrap theauthenticatecall in a try/catch that shows a message, so a refused prompt can never take the app down. - In this design the key is not created with
setUserAuthenticationRequired, so the gate protects against someone picking up an unlocked phone, not against code running inside the app. Auth-bound keys are stronger but complicate background autofill and break if the user removes the screen lock; decide consciously.
Offering a saved login without trapping the user
The browser engine asks the app for saved logins every time a login field gets focus. Answering with a modal dialog creates a loop: the dialog takes focus, closing it (by picking or canceling) hands focus back to the field, and the engine asks again. The user can neither use the saved login nor type one by hand. Card and address pickers had a "don't ask again on this page" guard; the login picker did not.
- Offer saved logins in a bar that does not take focus (above the keyboard, like a snackbar): tap a row to fill, X to leave the form alone.
- Remember the answer per tab and URL. After a pick or a dismissal, do not offer again until the address changes.
- A newer request replaces an older one (dismiss the old prompt object so the engine is not left waiting), and the bar hides when the tab or URL no longer matches.
What not to store or leak
- Never store a card's CVV/CVC. Save number, name and expiry only; let the user type the code. Validate numbers with the Luhn check.
- Nothing from private (incognito) tabs is saved.
- Copying a secret to the clipboard: mark it sensitive so Android 13+ hides it in the clipboard preview and well-behaved keyboards keep it out of clipboard suggestions: set
ClipDescription.EXTRA_IS_SENSITIVE("android.content.extra.IS_SENSITIVE") to true in the clip description's extras. - Keep tokens out of URLs. A session token in a query string (
?token=…) ends up in history, the address bar, copied links, server logs and referrers. Send it in a request header, scoped to the one host that needs it (Tokay adds the header from a tiny built-in extension; see Building an Android browser on GeckoView (Tokay)). If a token is ever pasted somewhere public, reset it at the provider. - Masked fields that show secrets in the UI (password, card number) get a show/hide toggle.
Importing from other password managers
CSV exports from Chrome, Firefox, Bitwarden and Safari differ in column names; map them and keep only http(s) origins (Chrome includes android:// app credentials, Firefox a Firefox-accounts row). A CSV export is a plaintext file of every password: tell the user to delete it after import.
Sync is a separate decision
Syncing saved passwords means either running a server that holds other people's (encrypted) credentials, or joining an existing end-to-end encrypted system such as Firefox Sync. For a personal app the recommendation was not to build sync at all; see the sync section of Building an Android browser on GeckoView (Tokay).
See also
- App lock, PINs and brute-force lockout on Android
- SQLCipher on Android (keys, KDF cost, rekeying safely)
- Passphrase-encrypted export files for moving app data between phones
- E2E key management that never loses a key (keyring, backup, re-wrap)