jeroentrappers / flutter_keychain

A flutter plugin for secure storage on Android via KeyStore and iOS via Keychain
Other
56 stars 41 forks source link

Cipher functions:OPENSSL_internal:BAD_DECRYPT #4

Closed jeprojects closed 10 months ago

jeprojects commented 5 years ago

Sometimes getting this error:

E/flutter_keychain(29852): error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852): Failed to handle method call
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852): java.lang.IllegalArgumentException: Unsupported value: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:293)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at io.flutter.plugin.common.StandardMethodCodec.encodeErrorEnvelope(StandardMethodCodec.java:70)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.error(MethodChannel.java:208)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at be.appmire.flutterkeychain.FlutterKeychainPlugin.onMethodCall(FlutterKeychainPlugin.kt:323)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at android.os.MessageQueue.next(MessageQueue.java:325)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at android.os.Looper.loop(Looper.java:142)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at android.app.ActivityThread.main(ActivityThread.java:6541)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
E/MethodChannel#plugin.appmire.be/flutter_keychain(29852):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
D/        (29852): HostConnection::get() New Host Connection established 0x724a7ce36680, tid 29895
D/EGL_emulation(29852): eglMakeCurrent: 0x724a95fa1c80: ver 3 0 (tinfo 0x724a8c40d540)
I/flutter (29852): Caught error: PlatformException(error, Unsupported value: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT, null)

It happens in the emulator normally after an app crash. I have to then clear the data from the app and login again to the application (which imports the data into the keychain again).

Any ideas?

jeroentrappers commented 5 years ago

Seems that the value that you are trying to decrypt is not properly padded. This could be the case when you are writing a value and while that is in progress your app crashes.

Noyal commented 5 years ago

Hi, I am also facing this issue, but I get it only for some phones. Situation: Published app via PlayStore (Test Environment). The issue occurs only when the app is installed for the first time. Incremental updates are not throwing this issue. After clearing the data, it will work.

Any ideas?

Minenash commented 5 years ago

I'm running into this problem also, but the app is neither crashing or being closed when it's being written too.

Diaglyonok commented 4 years ago

This happens after clearing data on android. I plunged into Kotlin code and found an interesting error there.

The author stores the AES key in preferences, and when the keychain.clear() method is called from Dart, preferences.clear().commit() is called in the Kotlin code. So the key is erased from the store and after a person tries to read any value from the store, the AES key is unavailable and therefore this error is thrown.

To fix this problem you need to do one of the following: 1) Save the AES key before clearing the preferences and then write the key back. To do it write this in FlutterKeychainPlugin.kt in 319 line:

val savedValue: String? = preferences.getString(AesStringEncryptor.WRAPPED_AES_KEY_ITEM, null)
preferences.edit().clear().commit()
preferences.edit().putString(AesStringEncryptor.WRAPPED_AES_KEY_ITEM, savedValue).commit()
result.success(null)

2) in the Dart code, after deleting the Keychain, also do the cleaning of shared preferences (don't know why it helps) 3) In the Dart code, create an array of keys that you use in the application and, instead of clearing keychain, go through these keys and set them to null.