1

I am creating an App and i want to store username and password in app itself. The username password is not entered by user. The credentials is common for everyone. I want to save the username and password in app code itself. What is the secure method to save in android?

Rizwan
  • 61
  • 5

2 Answers2

1

You can use SharedPreference to store data permanently in the Android app.

In your Activity,

    //get the sharedPrefs
    val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return

    //store the data in sharedPrefs file
    with (sharedPref.edit()) {
        putString("Some_key_user_name",userNameText)
        commit()
    }

   //read the stored data form sharedPrefs
   val storedUserName = sharedPref.getString("Some_key_user_name", defaultValue)

For the password, you can use the same approach. However, if the password is so precious(which it is in most cases) you can encrypt the data stored in sharedPreference file. Look ahead with this

As an example in my Activity,


  val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return

   with (sharedPref.edit()) {
       putString("MY_HELLO_WORLD_KEY","helloWorld")
       commit()
   }

iCantC
  • 2,852
  • 1
  • 19
  • 34
  • Can you show a simple example code for the above? Suppose i want to hardcode password 'helloWorld' in the app. how can i do that? – Rizwan May 11 '20 at 15:28
0
  • You can use key store to save your private keys securely

check this Securely Storing Keys in Android Keystore

check How to save secret key securely in android

Oussèma Hassine
  • 741
  • 1
  • 7
  • 18