Android EncryptedSharedPreference 系統升級後無法解密的錯誤與解決方案

一、問題描述 1 2 使用者的手機為 Sony 品牌,首次安裝 App 時,系統版本為 Android 14,能夠正常使用。 近期系統升級為 Android 15 後,開啟 App 即閃退。即使移除後重新安裝,仍然無法使用。 二、查看錯誤事件 查找 Crashlytics 上的錯誤事件,全部事件只有兩筆,其中只有 BasePreference.pref_delegate$lambda$0 這筆看起來最為相關。 查看錯誤訊息的內容,確認是在實例化 EncryptedSharedPreference 時出了問題。 程式裡實例的方式是參考 Android 開發 | 加密版的 SharedPreference - EncryptedSharedPreferences。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Fatal Exception: com.google.crypto.tink.shaded.protobuf.D: Protocol message contained an invalid tag (zero). at com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException.<init>(InvalidProtocolBufferException.java:100) at com.google.crypto.tink.shaded.protobuf.GeneratedMessageLite.parsePartialFrom(GeneratedMessageLite.java:100) at com.google.crypto.tink.shaded.protobuf.GeneratedMessageLite.parseFrom(GeneratedMessageLite.java:100) at com.google.crypto.tink.proto.Keyset.parseFrom(Keyset.java:3) at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.read(SharedPrefKeysetReader.java:67) at com.google.crypto.tink.CleartextKeysetHandle.read(CleartextKeysetHandle.java:67) at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.read(AndroidKeysetManager.java:67) at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewKeyset(AndroidKeysetManager.java:67) at com.google.firebase.messaging.GmsRpc.i(GmsRpc.java:17) at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:180) at // ... 錯誤訊息 Fatal Exception: com.google.crypto.tink.shaded.protobuf.D: Protocol message contained an invalid tag (zero). InvalidProtocalBufferException 代表 解密資料時解析 protobuf 格式失敗,通常是因為解密出的內容是無效的(可能是空的或亂碼)。 ...

February 11, 2025 · 4 min · 642 words · Daniel Huang

Android 開發 | 加密版的 SharedPreference - EncryptedSharedPreferences

介紹 根據官方文件, EncryptedSharedPreferences 使用 2-part system 來管理金鑰,並用金鑰加解密儲存的資料。 一組 Keyset(密鑰集合)包含一個或多個 Key(密鑰),用來加密解密資料。這組 Keyset 會儲存在 SharedPreferences 裡。 一把 Primary Key (主鑰) 負責加密所有的 Keysets,這把是存在 Android 的 KeyStore 裡面。 運作流程 graph TD; A[應用程式存取 EncryptedSharedPreferences] --> B{檢查 MasterKey 是否存在於 KeyStore} B -- 存在 --> C[讀取 MasterKey] B -- 不存在 --> D[建立新的 MasterKey] C --> E{檢查 KeySet 是否存在於 SharedPreferences} D --> E E -- 存在 --> F[讀取 KeySet] E -- 不存在 --> G[產生新的 KeySet 並加密存入 SharedPreferences] F --> H[使用 KeySet 加密 Key & Value] G --> H H --> I[存入 shared_prefs/*.xml 已加密的數據] 資源引用 1 2 3 4 5 6 7 8 9 10 11 // Encrypted SharedPreference implementation "androidx.security:security-crypto:1.0.0" // For Identity Credential APIs implementation "androidx.security:security-identity-credential:1.0.0-alpha03" // For App Authentication APIs implementation "androidx.security:security-app-authenticator:1.0.0-alpha02" // For App Authentication API testing androidTestImplementation "androidx.security:security-app-authenticator:1.0.0-alpha02" 這個套件基本上也可以對檔案加密,但本篇以實作 SharedPerefrences 為主,詳情可以參考 Work with data more securely。 ...

October 8, 2021 · 3 min · 558 words · Daniel Huang