How to Secure Android Shared Preferences?

前端 未结 9 2168
我在风中等你
我在风中等你 2020-11-28 22:11

The common location where SharedPreferences are stored in Android apps is:

/data/data//shared_prefs/
         


        
9条回答
  •  星月不相逢
    2020-11-28 22:33

    You need to handle Verison under API 23

     private static SharedPreferences getSharedPreferences(Context mContext) throws NullPointerException {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                String masterKeyAlias = null;
                try {
                    masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
                    sharedPreferences = EncryptedSharedPreferences.create(
                            "secret_shared_prefs",
                            masterKeyAlias,
                            mContext,
                            EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                            EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
                    );
                } catch (GeneralSecurityException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                sharedPreferences = mContext.getSharedPreferences(tag, Context.MODE_PRIVATE);
            }
            return sharedPreferences;
        }
    

提交回复
热议问题