2

When executing this code on a real device

String masterKeyAlias = masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
SharedPreferences settings = EncryptedSharedPreferences.create(
                    "encrypted_preferences",
                    masterKeyAlias,
                    myAppContext,
                    EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                    EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
            );

I get the following exception:

com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).

MasterKeyAlias has the value "androidx_security_master_key".

Is it some of the parameter values causing the problem or something else?android

Mathias Rönnlund
  • 4,078
  • 7
  • 43
  • 96
  • Does this answer your question? [Caused by: com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero)](https://stackoverflow.com/questions/73784634/caused-by-com-google-crypto-tink-shaded-protobuf-invalidprotocolbufferexception) – Mark Sep 28 '22 at 18:49
  • 2
    Uninstalling and reinstalling didn't work but going into app settings and clearing data (cache was already empty) actually solved it. Will this then happen to all users when upgrading? – Mathias Rönnlund Sep 29 '22 at 18:13
  • 3
    I believe I had as similar issue to do with this. Android backed up the some preference information pertaining to Enrypted Preferences library between installs. If android restores this data/key from backup it won't work with the newly installed app. Try adding `android:allowBackup="false"` to your manifest and `android:dataExtractionRules="@xml/data_extraction_rules"` for Android 12 excluding prefs from backup info on data extraction rules - https://developer.android.com/guide/topics/data/autobackup#include-exclude-android-12. I cannot say for sure whether updating an app will have issues – Mark Sep 29 '22 at 19:05

0 Answers0