Skip to content

Encrypted SharedPreferences uses strong encryption algorithms (AES-256) and is integrated with the Android Keystore system, which securely manages the encryption keys.

License

Notifications You must be signed in to change notification settings

AndroidWithRossyn/Encrypted-SharedPreferences

Repository files navigation

Securing Android App with Encrypted SharedPreferences

Generally data is saved in this way in Android devices.

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <boolean name="googleMapShow" value="true" />
    <int name="dateFormat" value="7" />
    <boolean name="isShowTemperature" value="true" />
    <boolean name="isShowLevelIndicator" value="false" />
    <string name="language">en</string>
    <int name="latLongFormat" value="2" />
    <boolean name="isShowDate" value="true" />
    <int name="selectedTemplate" value="0" />
    <boolean name="isShowShortAddress" value="true" />
    <boolean name="firstTime" value="false" />
    <boolean name="isShowLongAddress" value="true" />
    <boolean name="isShowLatLong" value="true" />
    <boolean name="isShowTime" value="true" />
    <int name="temperatureFormat" value="1" />
    <int name="timeFormat" value="8" />
    <int name="googleMapType" value="1" />
</map>

But when we use Encrypted SharedPreferences.

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="__androidx_security_crypto_encrypted_prefs_key_keyset__">12a901bc682e67316d97810d5c4ed741daaeddd8877e30137e1b60d4094b04a6ff0bd27c351334ece9568e0b6a3d4b0de494c5577cd71a614e4198c02b8ce62626579ff52740fb41336fe4b012861f5b0533668d2642db6c45025f4428cf6fcd1aba151e991a4e1785dbd0848c8ded29a7540ad8a31236fd40b8c4ec9fa08b5ddf07ed49d6ffb42dbfa98c1a9a06c929f46120bcc6c563e82714e2f4134915b989ed0e75036428bcec24d37d1a4408d2a9e1c304123c0a30747970652e676f6f676c65617069732e636f6d2f676f6f676c652e63727970746f2e74696e6b2e4165735369764b6579100118d2a9e1c3042001</string>
    <string name="AUh4VNJxfVMtKEShUAH66Y/Wd6tacepdmB51suathcQ+YCrsu4hZ0Xg=">ARpRaxiHZmU9NmpOYhXGsKFWh0Q5gfG1YtjOZ+/ET0vY/zU4Pe4=</string>
    <string name="AUh4VNJW7atm/QULrLCmY/d2aNzirJKQGNOA3WOPUb0=">ARpRaxipyG/6dldVpNFeXh4JEO+NjL6JxmH24TTQVBj1xDVt4YKpEK8=</string>
    <string name="AUh4VNJyHr2mLKGjNgD1KKyjYWNeUMCaUEwd9BTrIk/ycU9l">ARpRaxhaVtgbMw1OzeIB/+3Nywr2+DM4pzP8S9M3Jd0Qd3bvKtQXBN4=</string>
    <string name="__androidx_security_crypto_encrypted_prefs_value_keyset__">128801cb3c76b722706eeed2f367d681c440c160a4cad6d6279d87eb1ae86494ac213f596c289d9b677f1aff5d6152fbf777fe5e57319363213e1ad6dea868e104cc6c3772924516f3e6026e863dcf3a29547876e83c833e6d7a711c86fec53a9445c09bfbe47fb3bf3d3b5339ef9c80da7a7198a6c9a2736e9a55f2039a455ddaf7fce792200904df97921a440898d6c5d201123c0a30747970652e676f6f676c65617069732e636f6d2f676f6f676c652e63727970746f2e74696e6b2e41657347636d4b657910011898d6c5d2012001</string>
    <string name="AUh4VNLEMzO0ZEzywCdpX5nBzKTXdomqrA86p2Qz1hNg3DniJQw=">ARpRaxi7ULeVm2XWXKuSkU/0EzQKvdlou87SmUcBW4iaZMRvBgk=</string>
    <string name="AUh4VNLBzx1AEDT0DKye1uJvGOy0pDUu/+5L4EVNp6VeMFd7Ua8=">ARpRaxg8hP0mELpEOwh0nfVD6wd1K8FEt78EjzdWESkBk+nY0hE=</string>
    <string name="AUh4VNKRI5oaAyh3ukO1nrUvZOyznA8Wq1PUU0kZ1g==">ARpRaxi3ROeOi0fEMQ4i4Px+1ryY+adkx7aG5zBBWwD+9hPfv20=</string>
    <string name="AUh4VNJG2G6tiib4AgyC1Zyp+JB0fwinTJRyTkvooQ==">ARpRaxgClZXUx9+dAt8DPN/pWap8QTWm5cGiIMwB0OHDNPZJs2gMu60=</string>
    <string name="AUh4VNL4/Rv9MNzDJTM5VPM9seA71pP1sIqt13XGC5oSj60ToE/g">ARpRaxgk4Eg5Ssh8jV4w8BDdKjFDZ1pD3V6P6PJRtiJMpTXckmw=</string>
    <string name="AUh4VNLOvgbmqRDmi7BBk8+/1F/FJ7H6rsoNl2B3MBl/7g==">ARpRaxipnE2qdflE/X7dQYbNBtu7NxptAfmdz8+J/lBODO0kqSU=</string>
    <string name="AUh4VNK3nOPZuU3qgLFBAX1rs08LgLF3DKeCCbNK">ARpRaxghDuvQTvR+sH3qge7h9t7SZH4WITftmnA9jpZuizdDTzE=</string>
    <string name="AUh4VNJ6aLMPosbF9bIT1f8h3KkBDKsNjH3TNa/9">ARpRaxgTIDlUlcqBurlhjMIBiWdYd/Iv/XafyPYAX+4FbElm+b8yEys=</string>
</map>

Add the required dependencies to your build.gradle file:

dependencies {
    implementation("androidx.security:security-crypto:1.1.0-alpha06")
}
  fun init(context : Context) {
   
        val masterKeyAlias = MasterKey.Builder(context)
            .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
            .build()

        this.preferences = EncryptedSharedPreferences.create(
            context,
            SHARED_PREFERENCE_FILE_NAME,
            masterKeyAlias,
            EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
            EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
        )

    }

Elevate your Android app to the next level with our state-of-the-art solutions! Contact us today and let’s create something extraordinary!

Instagram LinkedIn Medium


Note: Please review our Code of Conduct before using this project.

Find this Repository useful? ❤️

Support it by joining stargazers for this repository. ⭐

Also, follow me on GitHub for my next creations! 🤩

All Repositories

☕ Fuel My Code

PayPal UPI

Your support transforms caffeine into code! ✨

🚀 Faster Updates 🔍 Better Documentation 🛠️ More Features
Why Support?

Every contribution helps me dedicate more time to creating high-quality open source Code. Your support directly translates to better software for everyone!

License

Copyright: 
~ Rossyn
~ Rohitraj Khorwal

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.