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.
You should encrypt your data and write to SharedPreferences. When you want get this data then you should decrypt from SharedPreferences. you need the following helper class for this
then you can write in SharedPreferences by encrypting your data as follows
you can finally read from SharedPreferences data in the following way. This way, sensitive information will be safer while kept on the hardware level in the phone
Base64 is NOT encryption! Don't use it! Yes 'root' users can access that data. One thing you can do is use AES to encrypt either that data or use a single NoSQL database file and encrypt that file. When the app opens, you decrypt the database and use that to store info or encrypt all files independently.
Look here: https://code.tutsplus.com/tutorials/storing-data-securely-on-android--cms-30558
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
UPDATED ANSWER:
Android has released a security library with EncryptedSharedPreferences in their Jitpack library.
Min API is 23 (6.0+)
https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences.html