13

I use the EncryptedSharedPreferences from the androidx.security:security-crypto:1.0.0-alpha02 lib to store some Tokens. If i use prefs.edit().remove("token")).apply() or prefs.edit().remove("token")).commit() the token is removed. But if i try to clear the prefs at once with the clear() method nothing happens.

This call: prefs.edit().clear().commit() even returns false.

I get the EncryptedSharedPreferences by using this method:

    private fun getPrefs(): SharedPreferences {

    val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
    return EncryptedSharedPreferences.create(
            "myPrefs",
            masterKeyAlias,
            context,
            EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
            EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM

    )
}

Do i have to clear the EncryptedSharedPreferences in some other way?

The documentation says (https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences) :

// use the shared preferences and editor as you normally would SharedPreferences.Editor editor = sharedPreferences.edit();

Update 07.02.2020 I created a bug ticket in the google bugtracker (https://issuetracker.google.com/issues/138314232) but they wont fix it...

elpatricko
  • 488
  • 5
  • 17
  • 1
    The `EncrypedSharedPreferences` is a new api, it might be a bug. Check out the issue tracker: https://issuetracker.google.com/issues?q=EncryptedSharedPreferences if there is no bug, then create a ticket for it. – Giorgos Neokleous Jul 25 '19 at 13:45
  • @GiorgosNeokleous i found nothing and created a new Ticket. Thanks for the Link. – elpatricko Jul 25 '19 at 14:14
  • 1
    Any updates on this? I'm facing the same issue. Getting the exception, java.lang.SecurityException: Could not decrypt key. decryption failed – Tony Nov 06 '19 at 17:20
  • @Tony updated the post... – elpatricko Feb 07 '20 at 10:34

5 Answers5

1

This issue fixed from version 1.0.0-rc03. Clear is working fine now. Enjoy!

Refer release note: https://developer.android.com/jetpack/androidx/releases/security#security-crypto-1.0.0-rc03

0

Update: as Jeevanandham indicated, this issue has been fixed. There is now a stable version. https://developer.android.com/jetpack/androidx/releases/security#version_100_3


Original answer:

One solution is to clear prefs using the standard, non secure preference manager.

PreferenceManager.getDefaultSharedPreferences(context).edit().clear().apply();

This will clear all the preferences.

Markymark
  • 2,804
  • 1
  • 32
  • 37
0

Works in different activity as well!

SharedPreference preference

...

preference.getSharedPreferences("myPrefs",0).edit().clear().apply();
0

It turns out this is bug from the library. It can not clear all the keys at once. https://developer.android.com/jetpack/androidx/releases/security#security-crypto-1.0.0-rc03

So simple solution would be just to call the shared preference without encrypted instance.

private fun clearPref() {
       context.getSharedPreferences("myPrefs",
           Context.MODE_PRIVATE).edit().clear().apply()
}
Azamat Mahkamov
  • 912
  • 14
  • 18
-3

Use Remove method for now :

public void clear(){
SharedPreferences.Editor editor = prefs.edit();
 Map<String, ?> allEntr = prefs.getAll();
        for (String entry : allEntr.keySet()) {
            editor.remove(entry);
        }
        prefs.editor.clear();
        prefs.editor.apply();
 }