Encrypted Shared Preferences in Android
Many times we want to save the data within the android application. This data is in the form of text and we generally prefer to store this data in the shared preferences. Shared preferences are not secure as we can simply view the data stored within the shared preferences and can easily access data within that file. To make the data stored in shared preferences secure we use encrypted share preferences which are more secure and the data stored in them is encrypted. Anyone cannot read the data within the file easily. In this article, we will take a look at How to implement encrypted shared preferences in android.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Adding dependency in build.gradle file
Navigate to app>build.gradle and add below dependency in the dependencies section.
After adding the dependency sync your project to install it.
Step 3: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
Step 4: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
Now run your application to see the output of it.
Now check out the below article to check the value stored in the shared preferences How to View Data Stored in Shared Preferences in Android Studio. As we are using encrypted shared preferences we will get to see the data in the encrypted form. In the preferences.xml file, we will get to see the below data in the encrypted format. Encrypted shared preferences make it secure to store the data in shared preferences so anyone cannot access the data within our shared preferences.
Please Login to comment...