Securing User Data with Encrypted Shared Preferences in Android

Software Engineer | IoT lover | Romans 8:38
Search for a command to run...

Software Engineer | IoT lover | Romans 8:38
No comments yet. Be the first to comment.
For most of Android's history, building an app required a workstation. A capable laptop, a USB debugging cable, and a stable internet connection were the price of admission. If you're an Android devel

From Algorithms to Affection: Why I Love Her "This isnāt about code. Itās about connection." Dear reader, I know, this isnāt my usual post. No deep dives, no frameworks, no rants about bugs that disappear the moment someone looks over your shoulder...

WebView is an indispensable tool for many Android apps, enabling developers to seamlessly embed web content into their native interfaces. However, its convenience comes with significant security challengesāfrom crossāsite scripting (XSS) attacks to u...

In the ever-evolving world of Android development, developers often focus on the latest trends like Jetpack Compose, Kotlin Coroutines, and dependency injection with Hilt. However, thereās one critical yet under-discussed topic in Android development...

API access tokens are the key to unlocking the power of various services for your mobile and web applications. But with this power comes the need for security to prevent unauthorized access to sensitive information.
In this article, we will dive into the technique of securing your app's data using Encrypted Shared Preferences of the Jetpack security library.
By the end of this article, you will have a solid understanding of how to safeguard your data in your Android applications and keep your user's data protected using encrypted shared preferences.
Encrypted shared preference Wraps the SharedPreferences class and automatically encrypts keys and values using a two-scheme method: Keys are encrypted using a deterministic encryption algorithm such that the key can be encrypted and properly looked up. Values are encrypted using AES-256 GCM and are non-deterministic
To use the Security library, add the following dependencies, as appropriate, to your app module's build.gradle file:
dependencies {
// security
implementation "androidx.security:security-crypto:1.0.0-alpha02"
}
To demonstrate encrypted shared preferences, we'll create a helper class with two functions saveData and getData which as the name suggests, will be used to save and get data in a secure way.
private val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
fun saveData(context: Context, key: String, data: String) {
// Initialize/open an instance of EncryptedSharedPreferences
val sharedPreferences = EncryptedSharedPreferences.create(
// passing a file name to share a preferences
FILE_NAME,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
sharedPreferences.edit().putString(key, data).apply()
}
In the snippet above, We start by initializing the android Keystore class which gives us access to a master key alias we'll be using to initialize an instance of the EncryptedSharedPreferences object.
On initializing the EncryptedSharedPreferences, we can then save our data as we normally would using the sharedPreferences edit() and apply() method
fun getData(context: Context, key: String): String? {
// Initialize/open an instance of EncryptedSharedPreferences on below line.
val sharedPreferences = EncryptedSharedPreferences.create(
// passing a file name to share a preferences
FILE_NAME,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
//return the value or an empty string if it's null
return sharedPreferences.getString(key, "")
}
Again, we initialize an instance of the EncryptedSharedPreferences object using the master key alias we created earlier and passing in the name of the key value we want to retrieve.
We can use the functions from our mainActivity.kt file or anywhere in our application like this:
And our complete sharedPreferencesHelper.kt file should look like this:
After running the code we should see:
The username is Astrocoder and the access token is MY_ACCESS_TOKEN printed in the logcat.
You can also check the emulator's data storage to see the encrypted My_file_name.xml file and its content, just like in the image below

The Jetpack team has made it really easy to securely store and retrieve sensitive data in our apps with EncryptedSharedPreferences; Here are some of the advantages of using it:
It uses Android keystore to automatically remove the pain of having to achieve the same thing using other methods of encryption
Every data we feed it is encrypted, including the key names used to access the shared preference values, as we can see in the image above, making it a lot harder for any attacker to know what data to attack
That's a wrap!!! feel free to reach out to me on any of my social media accounts should you have any questions.
You can find the complete code to this example on GitHub, here
Thank you for reading! Cheers!!!