Secure SharedPreferences
suggest changeVersions
This topic was documented against Android 1.0 through 7.1.
Introduction
Shared Preferences are key-value based XML files. They are located under /data/data/package_name/shared_prefs/<filename.xml>.
So a user with root privileges can navigate to this location and can change the values. If you want to protect values in your shared preferences, you can write a simple encryption and decryption mechanism.
You should know, though, that Shared Preferences were never built to be secure. They are just a simple way to persist data.
Syntax
- public static String encrypt(String input);
- public static String decrypt(String input);
Parameters
| Parameter | Definition |
|---|---|
| input | String value to encrypt or decrypt. |
Remarks
Shared Preferences were never built to be secure. They are just a simple way to persist data.
It is not a good idea to use shared preferences for storing critical information such as user credentials. To save user credentials (such as passwords) you need to use other methods such as Android’s AccountManager.