
Member-only story
Encrypted Preferences in Android
Storing data in SharedPreferences is so quick and comfortable. It’s also easy for malicious people to have a look at the data stored in SharedPreferences… so go have to be careful about what you put in there, and might need to start thinking about way to store data in an encrypted format.
For small amounts of data which don’t justify the usage of a db engine such a SqlCipher, our options were limited to:
- Custom encryption methods (if you know what you are doing)
- Pre-built solutions like Secure-preferences, another Secure-preferences, Armadillo, …
- Fighting with the most weird lifecycle issues of the Android Keystore System on each Android version
It has been in the works for a while, but now we have a proper and official solution. Even if it’s still alpha2 it already has been working pretty well for some time in one of my projects, all welcome EncryptedSharedPreferences to your min-sdk 23+ projects.
Let’s jump to an example of how to use it:
EncryptedSharedPreferences example
Min-sdk
As of today, 23 (Android 6.0)
Add the dependencies
Initialize / Open
Just create or fetch a Master Key from the Android Keystore for you, and use it to initialize/open a EncryptedSharedPreferences instance: