flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

EncryptedSharedPreferences initialization failed

Open AhmadFikriAS opened this issue 9 months ago • 5 comments

I got this issue in version 9.2.4 and still same in version 10.0.0-beta.4

Android ver : 10 Device : imin POS

E/SecureStorageAndroid( 8098): EncryptedSharedPreferences initialization failed E/SecureStorageAndroid( 8098): java.security.GeneralSecurityException: Keystore operation failed E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.crypto.MasterKeys.generateKey(MasterKeys.java:102) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.crypto.MasterKeys.getOrCreate(MasterKeys.java:56) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.crypto.MasterKey$Builder$Api23Impl.build(MasterKey.java:317) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.crypto.MasterKey$Builder.build(MasterKey.java:268) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.FlutterSecureStorage.initializeEncryptedSharedPreferencesManager(FlutterSecureStorage.java:128) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.FlutterSecureStorage.getEncryptedSharedPreferences(FlutterSecureStorage.java:108) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.FlutterSecureStorage.write(FlutterSecureStorage.java:54) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.handleWrite(FlutterSecureStoragePlugin.java:118) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.handleMethodCall(FlutterSecureStoragePlugin.java:92) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.run(FlutterSecureStoragePlugin.java:80) E/SecureStorageAndroid( 8098): at android.os.Handler.handleCallback(Handler.java:938) E/SecureStorageAndroid( 8098): at android.os.Handler.dispatchMessage(Handler.java:99) E/SecureStorageAndroid( 8098): at android.os.Looper.loop(Looper.java:223) E/SecureStorageAndroid( 8098): at android.os.HandlerThread.run(HandlerThread.java:67) E/SecureStorageAndroid( 8098): Caused by: java.security.ProviderException: Keystore operation failed E/SecureStorageAndroid( 8098): at android.security.keystore.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:331) E/SecureStorageAndroid( 8098): at javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:612) E/SecureStorageAndroid( 8098): at com.it_nomads.fluttersecurestorage.crypto.MasterKeys.generateKey(MasterKeys.java:98) E/SecureStorageAndroid( 8098): ... 13 more E/SecureStorageAndroid( 8098): Caused by: android.security.KeyStoreException: Unknown error E/SecureStorageAndroid( 8098): at android.security.KeyStore.getKeyStoreException(KeyStore.java:1301) E/SecureStorageAndroid( 8098): ... 16 more

AhmadFikriAS avatar Mar 04 '25 03:03 AhmadFikriAS

yeah same here

Flutter info : Flutter 3.27.3 • channel stable • https://github.com/flutter/flutter.git Framework • revision c519ee916e (6 weeks ago) • 2025-01-21 10:32:23 -0800 Engine • revision e672b006cb Tools • Dart 3.6.1 • DevTools 2.40.2

I started getting this error all of a sudden, also applies to 9.2.2 and above

E/KeyStore(19136): generateKeyInternal failed on response -41
E/SecureStorageAndroid(19136): EncryptedSharedPreferences initialization failed
E/SecureStorageAndroid(19136): java.security.GeneralSecurityException: Keystore operation failed
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.crypto.MasterKeys.generateKey(MasterKeys.java:102)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.crypto.MasterKeys.getOrCreate(MasterKeys.java:56)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.crypto.MasterKey$Builder$Api23Impl.build(MasterKey.java:317)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.crypto.MasterKey$Builder.build(MasterKey.java:268)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStorage.initializeEncryptedSharedPreferencesManager(FlutterSecureStorage.java:128)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStorage.getEncryptedSharedPreferences(FlutterSecureStorage.java:108)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStorage.containsKey(FlutterSecureStorage.java:44)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.handleRead(FlutterSecureStoragePlugin.java:127)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.handleMethodCall(FlutterSecureStoragePlugin.java:95)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.run(FlutterSecureStoragePlugin.java:80)
E/SecureStorageAndroid(19136): 	at android.os.Handler.handleCallback(Handler.java:938)
E/SecureStorageAndroid(19136): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/SecureStorageAndroid(19136): 	at android.os.Looper.loop(Looper.java:223)
E/SecureStorageAndroid(19136): 	at android.os.HandlerThread.run(HandlerThread.java:67)
E/SecureStorageAndroid(19136): Caused by: java.security.ProviderException: Keystore operation failed
E/SecureStorageAndroid(19136): 	at android.security.keystore.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:331)
E/SecureStorageAndroid(19136): 	at javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:612)
E/SecureStorageAndroid(19136): 	at com.it_nomads.fluttersecurestorage.crypto.MasterKeys.generateKey(MasterKeys.java:98)
E/SecureStorageAndroid(19136): 	... 13 more
E/SecureStorageAndroid(19136): Caused by: android.security.KeyStoreException: Memory allocation failed
E/SecureStorageAndroid(19136): 	at android.security.KeyStore.getKeyStoreException(KeyStore.java:1301)
E/SecureStorageAndroid(19136): 	... 16 more

mavzerbay avatar Mar 04 '25 21:03 mavzerbay

⚠️ This issue has been marked as stale because it has been open for 60 days with no activity.

If this issue is still relevant, please comment to keep it active. Otherwise, it will be closed in 60 days.

github-actions[bot] avatar May 04 '25 03:05 github-actions[bot]

What to do in this case?

sandrosimas avatar May 16 '25 13:05 sandrosimas

I've also been seeing this and have a guess that adding some resetOnError logic to this function might help resolve this (I'm thinking a try catch and delete the pref if it errors) https://github.com/juliansteenbakker/flutter_secure_storage/blob/71b75a36f35f2ce945998e20c6c6aa1820babfc6/flutter_secure_storage/android/src/main/java/com/it_nomads/fluttersecurestorage/FlutterSecureStorage.java#L132

I'm currently only able to reproduce this in a release signed (not with debug keys) version of the app deployed through PlayStore internal testing. This isn't ideal since I don't want to burn a bunch of version numbers testing the changes.

Would appreciate if anyone has a simpler repro path for me to follow! 🚀

ViscousPot avatar Jun 15 '25 19:06 ViscousPot

So I actually ended up burning a version number to test this and it works exactly as expected! Hope this resolves the issue for others too :)

https://github.com/juliansteenbakker/flutter_secure_storage/pull/946

Make sure to use resetOnError: true for my fix to work

ViscousPot avatar Jun 15 '25 19:06 ViscousPot