Search code examples
androidstoragemaui

Secure Storage in Maui - Android FileNotFoundException


When I am calling my Set method, I get FileNotFound Exception. I checked the documentation but did not see any specific implementations (beside two extra values in the manifest file)

Here is my code

public async Task SetAsync(string key, string value)
{
  await SecureStorage.SetAsync(key, value);
}

here is the androidmanifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
    <application android:allowBackup="false" android:icon="@mipmap/appicon" android:exported="false" android:fullBackupContent="false" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

and here here is the exception

    [monodroid-assembly] open_from_bundles: failed to load assembly Xamarin.AndroidX.Security.SecurityCrypto.dll
Loaded assembly: /data/data/com.abc.mypp.test/files/.__override__/Xamarin.AndroidX.Security.SecurityCrypto.dll [External]
[AndroidKeysetManager] keyset not found, will generate a new one
[AndroidKeysetManager] java.io.FileNotFoundException: can't read keyset; the pref value __androidx_security_crypto_encrypted_prefs_key_keyset__ does not exist
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readPref(SharedPrefKeysetReader.java:71)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readEncrypted(SharedPrefKeysetReader.java:89)
[AndroidKeysetManager]  at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:116)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.read(AndroidKeysetManager.java:311)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewKeyset(AndroidKeysetManager.java:287)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:238)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:155)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
[AndroidKeysetManager] keyset not found, will generate a new one
[AndroidKeysetManager] java.io.FileNotFoundException: can't read keyset; the pref value __androidx_security_crypto_encrypted_prefs_value_keyset__ does not exist
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readPref(SharedPrefKeysetReader.java:71)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readEncrypted(SharedPrefKeysetReader.java:89)
[AndroidKeysetManager]  at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:116)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.read(AndroidKeysetManager.java:311)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewKeyset(AndroidKeysetManager.java:287)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:238)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:160)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
[EngineFactory] Provider GmsCore_OpenSSL not available

Solution

  • Actually, I have created a sample to test the Secure Storage in the maui. I called the await SecureStorage.SetAsync(key, value); in the button's clicked event and run it on the simulator android 11, android 12 and physical device XiaoMi 6X android 9.

    All of them worked correct, the key and value have been written into the device. So there may be two causes.

    1. Special device: the issue appears on special devices, you can try to run the project on the other device or the simulators.
    2. Some references' versions: I created the project on the visual studio 17.4 preview 2.1, you can upgrade the visual studio and create a new project to test it.