Delete EncryptedSharedPreferences with clear() not working
Asked Answered
T

6

14

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...

Tracietracing answered 25/7, 2019 at 13:20 Comment(4)
The EncrypedSharedPreferences is a new api, it might be a bug. Check out the issue tracker: issuetracker.google.com/issues?q=EncryptedSharedPreferences if there is no bug, then create a ticket for it.Scientist
@GiorgosNeokleous i found nothing and created a new Ticket. Thanks for the Link.Tracietracing
Any updates on this? I'm facing the same issue. Getting the exception, java.lang.SecurityException: Could not decrypt key. decryption failedXylina
@Xylina updated the post...Tracietracing
B
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

Bulley answered 20/8, 2020 at 12:16 Comment(0)
A
2

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()
}
Audry answered 1/8, 2023 at 9:10 Comment(0)
B
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

Bulley answered 20/8, 2020 at 12:16 Comment(0)
O
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.

Overmatch answered 7/2, 2020 at 20:44 Comment(0)
B
0

Works in different activity as well!

SharedPreference preference

...

preference.getSharedPreferences("myPrefs",0).edit().clear().apply();
Blade answered 26/3, 2020 at 10:7 Comment(0)
L
0

I am using the androidx.security:security-crypto:1.1.0-alpha01 library.

I was getting the following exception when i call sharedPref.edit().clear().apply()

Exception java.lang.SecurityException: Could not decrypt key. decryption failed
  at androidx.security.crypto.EncryptedSharedPreferences.decryptKey (EncryptedSharedPreferences.java:609).

I was not able to find any particular issue because this error is not coming on every device. So, to handle this, i had to write custom logic to set the each key to a default value whenever i want to clear the data.

Lummox answered 25/1, 2024 at 10:33 Comment(0)
M
-4

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();
 }
Maniac answered 13/1, 2020 at 11:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.