4

First Activity

try {
    masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
    sharedPreferences = EncryptedSharedPreferences.create(
            "secret_shared_prefs",
            masterKeyAlias,
            getApplicationContext(),
            EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
            EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    );
} catch (GeneralSecurityException | IOException e) {
    e.printStackTrace();
}

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("memberID", response.body().get(0).getMemberID().toString()).commit();

Second Activity

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("secret_shared_prefs", MODE_PRIVATE);
sharedPreferences.getString("memberID", "unknown");

Within the same activity, getSharedPreferences() works as expected. When trying to access the preferences in another activity using this code, it always returns the default value. It seems there is a problem with decryption.

bliss
  • 336
  • 2
  • 8
  • Can you post the code where you store a value and the code where you read a value from sharedPreferences? – lwilber May 07 '20 at 08:38
  • From what I understand EncryptedSharedPreferences are read and written the same way as normal SharedPreferences, the difference is that ESP keys and values are encrypted in storage. So, the decryption should be handled for you and you should be able to get the string normally with `sharedPreferences.getString("memberID", "unknown");` source: https://proandroiddev.com/encrypted-preferences-in-android-af57a89af7c8 Please update if this doesn't work. – lwilber May 07 '20 at 23:16
  • Yes, that's what I understood from documentation, but it still seems to fail to decrypt using sharedPreferences.getString() from another activity. I may just try to implement an alternative at this point. – bliss May 08 '20 at 22:54

1 Answers1

3

Update for anyone running into this issue. I had the same problem, and fixed it by initializing the sharedPreferences the same way as in the first activity. So (in the example given above) the line in the second activity:

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("secret_shared_prefs", MODE_PRIVATE);

is replaced by:

try {
masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
sharedPreferences = EncryptedSharedPreferences.create(
        "secret_shared_prefs",
        masterKeyAlias,
        getApplicationContext(),
        EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
} catch (GeneralSecurityException | IOException e) {
    e.printStackTrace();
}

and can now be accessed by the regular getString(key, deafult) method:

sharedPreferences.getString("memberID","unknown");

For newbies such as myself I would also recommend checking the .xml file where the preferences are saved to check if they are actually encrypted (can be found in data/data/"application_name"/shared_prefs/"preference_name".xml).

MLRAL
  • 51
  • 7