Android Secure SharedPreferences

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Shared Preferences are key-value based XML files. It is located under /data/data/package_name/shared_prefs/<filename.xml>.

So a user with root privileges can navigate to this location and can change its values. If you want to protect values in your shared preferences, you can write a simple encryption and decryption mechanism.

You should know tough, that Shared Preferences were never built to be secure, it's just a simple way to persist data.

Syntax

  1. public static String encrypt(String input);
  2. public static String decrypt(String input);

Parameters

ParameterDefinition
inputString value to encrypt or decrypt.

Remarks

Shared Preferences were never built to be secure, it's 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.



Got any Android Question?