EncryptedSharedPreferences (Security library, part of Android Jetpack)

build.gradle

setup :
- minSdkVersion 23
- implementation "androidx.security:security-crypto:1.0.0-alpha02"


MainActivity.kt

onCreate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
    // Create MasterKey for encryption / decryption
    val masterKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)

    // Create EncryptedSharedPreferences file name : account_ESP and put password.
    val encryptedSharedPreferences = EncryptedSharedPreferences.create(
        "account_ESP",
        masterKey,
        applicationContext,
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM)
    val editor = encryptedSharedPreferences.edit()
    editor.putString("password", "123456789")
    editor.apply()

    // Get password (result == plaintext)
    val password = encryptedSharedPreferences.getString("password", null)
    Log.e("TEST:password_ESP:", password.toString())

}else{

    // Create Original SharedPreference File and put password.
    val sharePreference = this.getSharedPreferences("account_SP", Context.MODE_PRIVATE)
    val editor = sharePreference.edit()
    editor.putString("password", "123456789")
    editor.apply()

    val password = sharePreference.getString("password", null)
    Log.e("TEST:password_SP:", password.toString())
}

_______________________________________________________

output : /data/data/package_name/shared_prefs/account_ESP.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="AX88cKNiR6uVSVZdHxuE8pClSn+eRImmQrYUdDo=">AVQvIUN1bCzJsADFgb01UHlN8R7Ce23e+odonhBGXBWKLjRq7oDgSh6X/DT1J1yyo1g=</string>
    <string name="__androidx_security_crypto_encrypted_prefs_value_keyset__">12880177601ca5032985229fea7d3bdb61a0345d66db7393e61e29fb92d84bec09f1c1403d3d2b2b63952ab135aeea56ae2525d9916ca18f4717d88ee28e2c8190320e82d82435ba19c03817be63b1413d683d24c03a9c0fc02e6b2354d07f28c9e908709e5648482754130a57c5fcf9093bad099255cb330436c444c494da65d91acd7e15848e3b69932d1a4408c3c2bca105123c0a30747970652e676f6f676c65617069732e636f6d2f676f6f676c652e63727970746f2e74696e6b2e41657347636d4b6579100118c3c2bca1052001</string>
    <string name="__androidx_security_crypto_encrypted_prefs_key_keyset__">12a901e0af0b350f762ac68f7c0676d52922e861735966b9d2667ba964b859a3c0ad3d1c45127a4870c19aafa66423e2a4ee34f136ad21e6c8b6fbbe2cf6923675b996e1302f616d58f8c54829c90acc3455fada06c3380991571a5eadc3d865d90403c73bf7f7c53acebbad725d572d2ab8b02f0723238406b961f573f31abe555db4cdab66e7c1b28fd76d002d80aa8070cdd3ad36c409adb8335047578f75bd8749e87752e30c3cb5a0d81a4408a3e1f1f907123c0a30747970652e676f6f676c65617069732e636f6d2f676f6f676c652e63727970746f2e74696e6b2e4165735369764b6579100118a3e1f1f9072001</string>
</map>


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