encryptedsharedpreferences-example
encryptedsharedpreferences-example copied to clipboard
Request: update repository
Seems MasterKeys.getOrCreate is deprecated. It says to use MasterKey.Builder instead. Can you please update it?
Also, I think the minimal API for androidx.security:security-crypto:1.1.0-alpha02, is now 21.
I think this can be done as such:
val masterKey = MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build()
I have updated Library to androidx.security:security-crypto:1.1.0-alpha03
and I update this code as below
// Step 1: Create or retrieve the Master Key for encryption/decryption
val masterkey= MasterKey.Builder(this, MasterKey.DEFAULT_MASTER_KEY_ALIAS).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build()
// Step 2: Initialize/open an instance of EncryptedSharedPreferences
sharedPreferences = EncryptedSharedPreferences.create(
applicationContext,
preferencesName,
masterkey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
But I am getting this error while initializing
2022-08-02 10:38:43.482 22620-22620/com.akaita.encryptedsharedpreferences W/AndroidKeysetManager: keyset not found, will generate a new one
java.io.FileNotFoundException: can't read keyset; the pref value __androidx_security_crypto_encrypted_prefs_key_keyset__ does not exist
at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readPref(SharedPrefKeysetReader.java:71)
at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readEncrypted(SharedPrefKeysetReader.java:89)
at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:105)
at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.read(AndroidKeysetManager.java:311)
at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewKeyset(AndroidKeysetManager.java:287)
at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:238)
at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:155)
at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
at com.akaita.encryptedsharedpreferences.MainActivity.initEncryptedSharedPreferences(MainActivity.kt:71)
at com.akaita.encryptedsharedpreferences.MainActivity.initSharedPreferences(MainActivity.kt:47)
at com.akaita.encryptedsharedpreferences.MainActivity.access$initSharedPreferences(MainActivity.kt:25)
at com.akaita.encryptedsharedpreferences.MainActivity$onCreate$1.onCheckedChanged(MainActivity.kt:36)
at android.widget.CompoundButton.setChecked(CompoundButton.java:222)
at androidx.appcompat.widget.SwitchCompat.setChecked(SwitchCompat.java:1064)
at com.akaita.encryptedsharedpreferences.MainActivity.onCreate(MainActivity.kt:40)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Activity.performCreate(Activity.java:8034)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3688)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3864)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7870)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
@AndroidDeveloperLB @akaita Please Help me if you have any solution.
@nikunjparadva It's just an annoying warning, which I requested to remove: https://issuetracker.google.com/issues/185219606
Please consider starring.
Okay Thanks for your comment
@AndroidDeveloperLB Please suggest one thing.
is it healthy to use with this warning in production?
Thanks
@nikunjparadva The warning can be ignored, as I wrote. Sadly though, there is this issue: https://issuetracker.google.com/issues/176215143
A bit rare, but exists, and you might want to have a workaround for it. Read comments 54,55 there. You can also add Crashlytics to see for yourself that this issue exists.