Android Shared Preferences Encryption using jetpack library or How to Secure Android Shared Preferences? or Real and Write Encrypted Shared Preferences in android.

 Example below:-

try {
    String mainKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
 SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
        "example",
          mainKey,
          this,
         EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
            );

            // use the shared preferences and editor as you normally would
            @SuppressLint("CommitPrefEdits")
 SharedPreferences.Editor editor =    sharedPreferences.edit();
          editor.putInt("id", 23);
          editor.apply();

         Log.e("tag", "id: " + sharedPreferences.getInt("id", 0));
  } catch (GeneralSecurityException e) {
            e.printStackTrace();
   } catch (IOException e) {
            e.printStackTrace();
   }

Comments

Popular posts from this blog

Lint found fatal errors while assembling a release target.

Android Studio 3.5 or above usb device not detected in Android Studio or Unable to detect adb version, exit value: 0xc0000135. ADB depends on the Windows Universal C Runtime, which is usually installed by default via Windows Update. You may need to manually fetch and install the runtime package here: https://support.microsoft.com/en-ca/help/2999226/update-for-universal-c-runtime-in-windows

ViewModelProviders.of(this) is deprecated what to use instead ViewModelProviders.of(this) OR if you don't want to use ViewmodelFactory OR ViewModelProviders error.