Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Secure Android Shared Preferences?

The common location where SharedPreferences are stored in Android apps is:

/data/data/<package name>/shared_prefs/<filename.xml> 

User with root privileges can navigate to this location and can change its values.Need of protecting it is of much importance.

In how many ways we can encrypt whole shared_pref's xml file?

We all know that we can encrypt and save data in shared_pref's xml file, but that's not only 100% safe, so need to encrypt whole file with a key. Need help in knowing various ways to encrypt whole xml file. This is generic question, various encryption methods discussed as answers here can be helpful to all developers in securing apps.

like image 709
Harsh Dattani Avatar asked May 10 '15 07:05

Harsh Dattani


People also ask

How secure are Android shared preferences?

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.

How secure is EncryptedSharedPreferences?

Conclusion. EncryptedSharedPreferences is reliable and dead simple for Android 6.0 and higher. It comes with two big big pluses: it doesn't require us to hardcode anything on our code.

Where shared preferences are stored in Android device?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

What is jetpack security in Android?

Jetpack Security (JetSec) is a part of Android Jetpack. It provides abstractions for encrypting and decrypting SharedPreferences and Files. It also provides us with easy key management for the Android Keystore system. To use JetSec in our application you need to include it in your project first.


2 Answers

UPDATED ANSWER:

Android has released a security library with EncryptedSharedPreferences in their Jetpack library.

Edit: With version v1.1.0 you can support Lollipop (API level 21) and above

String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);  SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(     "secret_shared_prefs",     masterKeyAlias,     context,     EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,     EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM );  // use the shared preferences and editor as you normally would SharedPreferences.Editor editor = sharedPreferences.edit(); 
like image 118
Bojan Kseneman Avatar answered Sep 21 '22 16:09

Bojan Kseneman


Google has released EncryptedSharedPreferences as part of it's androidx, I believe this should be the preferable way of encrypting the preferences.

See https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences

like image 29
Ch3D Avatar answered Sep 18 '22 16:09

Ch3D