Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Exception: PlatformException(Exception encountered, read, javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT #210

Closed
RANSoftRA opened this issue Feb 24, 2021 · 34 comments

Comments

@RANSoftRA
Copy link

Problem described in #161 still occurrs in 3.3.5.

E/flutter (29195): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(Exception encountered, read, javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
E/flutter (29195): 	at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
E/flutter (29195): 	at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:570)
E/flutter (29195): 	at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:351)
E/flutter (29195): 	at javax.crypto.Cipher.doFinal(Cipher.java:1741)
E/flutter (29195): 	at com.it_nomads.fluttersecurestorage.ciphers.StorageCipher18Implementation.decrypt(StorageCipher18Implementation.java:91)
E/flutter (29195): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin.decodeRawValue(FlutterSecureStoragePlugin.java:163)
E/flutter (29195): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin.read(FlutterSecureStoragePlugin.java:144)
E/flutter (29195): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin.access$300(FlutterSecureStoragePlugin.java:29)
E/flutter (29195): 	at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$MethodRunner.run(FlutterSecureStoragePlugin.java:197)
E/flutter (29195): 	at java.lang.Thread.run(Thread.java:764)
E/flutter (29195): , null)

In my case for:
Samsung S8 (Android 8)

Some others mention this behavior for:

  • Pixel 4a
  • Pixel 5 (Android 11)
@RANSoftRA
Copy link
Author

I was able to reproduce this issue with my Galaxy A40 (Android 10):

  1. Installing production App from the PlayStore
  2. Login ( storing some data with FlutterSecureStorage, e.g. token)
  3. Close the App
  4. Uninstalling the App
  5. Install the production app from the PlayStore again
  6. Login -> Error occurs

The error did not occur anymore after I deleted the App-Data (Settings>App>Storage>"Clear App Data").

Seems like this is an restoring issue from some devices. I'll try to overcome this issue by adding the:

android:allowBackup="false"
android:fullBackupContent="false"

flags in the AndroidManifest.xml (see #43 (comment))

@RANSoftRA
Copy link
Author

Issue was resolved the following way:

Created a new version for the app:

  1. Exception handling and prompting for an option to delete the App-Data.
  2. Adding these flags
android:allowBackup="false"
android:fullBackupContent="false"

IMHO this is not an flutter_secure_storage issue. The backup content features should not include sensitive information that should only be stored on the device / used in the same application instance.

@vanlooverenkoen
Copy link

vanlooverenkoen commented Mar 16, 2021

@RANSoftRA one of our users has the same problem with a Huawei mate 20 lite.
I added the fullBackupContent=false,
(allowBackup="false" was already added.)

What error should I catch? And when should I catch the error?

@RANSoftRA
Copy link
Author

In my case the error occurred during the login process - it threw a PlattformException.

Example - Code In the Widget (so I can display an appropriate Error):

class LoginForm extends StatefulWidget {
...
// login - form submit
Future<void> _submit(BuildContext context) async {
    
    final authProvider = Provider.of<AuthProvider>(context, listen: false);

    try {
        // Performs a FlutterSecureStorage().write('...', '...') at some point
        await authProvider.login(_loginData['email'], _loginData['password']);
        ....
    } on PlatformException catch (e) {
       _setErrorState(message: 'Please clear App Data');
    } catch (e) {
        // Handling other errors
        ....
    }
    ...
}
...
}
  • Added try/catch block in the Widget
  • Handling the error properly (e.g. display that the user has to do it manually, or try to clear the App-Data programatically)

@vanlooverenkoen
Copy link

Do you have an idea why it is required to remove the app data for all users who are experiencing this?

For me the issue is happening on a FlutterSecureStorage.read.

Not sure why this is happening? Is it because when the app is reinstalled the secure store is used. but the keys for opening the secure store are different?

@RANSoftRA
Copy link
Author

RANSoftRA commented Mar 16, 2021

Yes, I believe this happens, because the used keys change when the user re-installs the app or uses a different device.

I have not investigated it further though, but it would certainly make sense.

All I know is that after clearing the App data the error does not ocurr anymore.

The authors mentioned that the Android backup feature could cause problems (see https://github.com/mogol/flutter_secure_storage#configure-android-version), but not for this issue. Maybe it would be good to mention this issue as well.

@vanlooverenkoen
Copy link

Alright. probably a delete & reinstall will fix the issue as well.

@SanjiKir
Copy link

Hey guys I am not sure this issue should remain closed, it just happened to us in the production environment and it has never happened before. This is a very random issue that might randomly affect real users.

@josefrvaldes
Copy link

Hi guys, it's happening here with 3.3.5 too. Maybe this issue shouldn't be closed ¿?

@jasaw
Copy link

jasaw commented Mar 26, 2021

It's failing on my Pixel 3, but works fine on my Pixel 4a. I'm running flutter_secure_storage version 3.3.5, clean install of app on both phones, production build of app (not debug).

I do not have these flags:

android:allowBackup="false"
android:fullBackupContent="false"

Update: It makes no difference whether the above flags are true or false.

@Xellly
Copy link

Xellly commented Apr 1, 2021

Happening to me too, 4.1
Only when I reinstall the application outside of debugging (APKs or Play store)
I have the backup flags set to false but they're not helpnig, only fix is clearing cache.

------Edit
For anyone having this issue, a work-around is installing SharedPreferences and emptying storage in the first bit of code that runs in your project
`final prefs = await SharedPreferences.getInstance();

if (prefs.getBool('first_run') ?? true) {
FlutterSecureStorage storage = FlutterSecureStorage();

await storage.deleteAll();

prefs.setBool('first_run', false);
}`

Thanks to this guy.

@komritza
Copy link

komritza commented Jul 6, 2021

I am using version 3.3.5 in the production and since few days ago I started getting sentry reports with following error log:

PlatformException
PlatformException(Exception encountered, read, javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
	at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
	at com.android.org.conscrypt.OpenSSLEvpCipher.doFinalInternal(OpenSSLEvpCipher.java:152)
	at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:374)
	at javax.crypto.Cipher.doFinal(Cipher.java:2055)
	at c.c.a.b.b.b(Unknown Source:32)
	at c.c.a.a.b(Unknown Source:11)
	at c.c.a.a.d(Unknown Source:7)
	at c.c.a.a.a(Unknown Source:0)
	at c.c.a.a$b.run(Unknown Source:134)
	at android.os.Handler.handleCallback(Handler.java:938)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loop(Looper.java:246)
	at android.os.HandlerThread.run(HandlerThread.java:67)
, null)

I tried to reproduce the bug, and I manage to do it. Devices that I have tested it Samsung s9 and Samsung s10e => error, Huawei P20 Pro is working and not crashing.
As mentioned above

  1. Installing production App from the PlayStore
  2. Login ( storing some data with FlutterSecureStorage, e.g. token)
  3. Close the App
  4. Uninstalling the App
  5. Install the production app from the PlayStore again
  6. Login -> Error occurs
    app got frozen on the splash screen and I got an instant sentry report. App stopped completely working...
    Then I have installed the app from VS code and it started working again, but I got this log in my terminal:
D/FlutterSecureStoragePl( 7250): Initializing StorageCipher
I/fluttersecurestorage( 7250): Creating keys!
I/fluttersecurestorage( 7250): Initializing
I/fluttersecurestorage( 7250): Generating key pair
E/KeyStore( 7250): generateKeyInternal failed on request -68
D/CertificatePolicyCache( 7250): Creating new instance of CertificatePolicyCache myUid: 10444 callingUid: 10444
D/CertificatePolicyCache( 7250): readVariables type: null userId: 0
D/FlutterSecureStoragePl( 7250): StorageCipher initialization complete

I don't know if this log is helpful, but this is actually a really huge bug.

@142hitesh
Copy link

Facing same issue with flutter_secure_storage: ^4.2.0 in Android 11.

@romreed
Copy link

romreed commented Oct 15, 2021

I have same issue with flutter_secure_storage: ^4.2.0 in Android 11 on samsung s20.

@rsmaximiliano
Copy link

Same issue with flutter_secure_storage: ^4.2.0

@comatory
Copy link

I'm having the same problem. It happens randomly and it never occured before. Please re-open the issue.

@justoke
Copy link

justoke commented Dec 6, 2021

Still an issue.

@zigaspica
Copy link

zigaspica commented Dec 14, 2021

Still an issue with the latest version. Issue can be reproduced with re-installing the app but only on specific devices @mogol

@NamanShergill
Copy link

I have opened a new issue to track this #354

@neiljaywarner
Copy link

If the change to the manifest works please add to readme

@KyawLwinSoe27
Copy link

Thanks a lot, This is works now.

@chrisDK1977
Copy link

chrisDK1977 commented Jan 18, 2024

Additional to Mentioned issue by @darshankawar

After changing

final FlutterSecureStorage _secure = const FlutterSecureStorage()

to

final FlutterSecureStorage _secure = const FlutterSecureStorage(aOptions: AndroidOptions(
    encryptedSharedPreferences: true,
  ))

the problem still exisits but changes to

PlatformException(Exception encountered, readAll, java.lang.SecurityException: Could not decrypt key. decryption failed
at androidx.security.crypto.a.c(Unknown Source:58)
at androidx.security.crypto.a.getAll(Unknown Source:49)
at x3.a.m(Unknown Source:5)
at x3.e$b.run(Unknown Source:204)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.security.GeneralSecurityException: decryption failed
at t2.c$a.b(Unknown Source:138)
at androidx.security.crypto.a.c(Unknown Source:13)
at androidx.security.crypto.a.getAll(Unknown Source:49) 
at x3.a.m(Unknown Source:5) 
at x3.e$b.run(Unknown Source:204) 
at android.os.Handler.handleCallback(Handler.java:958) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loopOnce(Looper.java:205) 
at android.os.Looper.loop(Looper.java:294) 
at android.os.HandlerThread.run(HandlerThread.java:67) 
, null)
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:322)

#2 MethodChannelFlutterSecureStorage.readAll (package:flutter_secure_stora

@bahman2000
Copy link

bahman2000 commented Feb 5, 2024

Still an issue.

flutter_secure_storage 9.0.0 in Android 9.

@acheronian
Copy link

acheronian commented Feb 28, 2024

Got the issue randomly on Android 14 devices, LogCat shows following error in production app:

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(Exception encountered, read, javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
	at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
	at com.android.org.conscrypt.OpenSSLEvpCipher.doFinalInternal(OpenSSLEvpCipher.java:152)
	at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:374)
	at javax.crypto.Cipher.doFinal(Cipher.java:2056)
	at w6.h.b(Unknown Source:35)
	at v6.a.c(Unknown Source:11)
	at v6.a.k(Unknown Source:17)
	at v6.e$b.run(Unknown Source:233)
	at android.os.Handler.handleCallback(Handler.java:958)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:230)
	at android.os.Looper.loop(Looper.java:319)
	at android.os.HandlerThread.run(HandlerThread.java:67)
, null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167)

@renenucci
Copy link

renenucci commented Mar 13, 2024

Happens in latest version too.

Delete cache/all data not works.
Flags allowBackup and fullBackupContent "works."

Only in release mode build, debug works fine.

Android 9

03-13 12:11:51.415 29225 29245 E flutter : [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Exception encountered, read, javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT 03-13 12:11:51.415 29225 29245 E flutter : at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method) 03-13 12:11:51.415 29225 29245 E flutter : at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:596) 03-13 12:11:51.415 29225 29245 E flutter : at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:363) 03-13 12:11:51.415 29225 29245 E flutter : at javax.crypto.Cipher.doFinal(Cipher.java:2055) 03-13 12:11:51.415 29225 29245 E flutter : at q4.h.b(Unknown Source:35) 03-13 12:11:51.415 29225 29245 E flutter : at p4.a.c(Unknown Source:11) 03-13 12:11:51.415 29225 29245 E flutter : at p4.a.l(Unknown Source:17) 03-13 12:11:51.415 29225 29245 E flutter : at p4.e$b.run(Unknown Source:235) 03-13 12:11:51.415 29225 29245 E flutter : at android.os.Handler.handleCallback(Handler.java:873) 03-13 12:11:51.415 29225 29245 E flutter : at android.os.Handler.dispatchMessage(Handler.java:99) 03-13 12:11:51.415 29225 29245 E flutter : at android.os.Looper.loop(Looper.java:207) 03-13 12:11:51.415 29225 29245 E flutter : at android.os.HandlerThread.run(HandlerThread.java:65) 03-13 12:11:51.415 29225 29245 E flutter : , null) 03-13 12:11:51.415 29225 29245 E flutter : #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651) 03-13 12:11:51.415 29225 29245 E flutter : #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334) 03-13 12:11:51.415 29225 29245 E flutter : <asynchronous suspension>

@Lilimester
Copy link

Lilimester commented Mar 19, 2024

Hi guys,
I encountered the same issue recently,
I am using FlutterSecureStorage.

while using it, please make sure you are passing the AndroidOptions while initializing the instance.

Instead of :

var secureStorage = const FlutterSecureStorage();

try adding as :

AndroidOptions androidOptions = const AndroidOptions( encryptedSharedPreferences: true, ); var secureStorage = FlutterSecureStorage( aOptions: androidOptions, );

Pass AndroidOptions while initializing the instance of FlutterSercureStorage. and mark encryptedSharedPreference to true.

This change actually resolved my issue.

Hope this helps someone.

Thanks.

@renenucci
Copy link

Hi guys, I encountered the same issue recently, I am using FlutterSecureStorage.

while using it, please make sure you are passing the AndroidOptions while initializing the instance.

Instead of :

var secureStorage = const FlutterSecureStorage();

try adding as :

AndroidOptions androidOptions = const AndroidOptions( encryptedSharedPreferences: true, ); var secureStorage = FlutterSecureStorage( aOptions: androidOptions, );

Pass AndroidOptions while initializing the instance of FlutterSercureStorage. and mark encryptedSharedPreference to true.

This change actually resolved my issue.

Hope this helps someone.

Thanks.

That's half true, besides it works on update on store, sometimes on uninstall and install run into problem again.

Not fixed at all, tested on Android 14 and 9

@neiljaywarner
Copy link

neiljaywarner commented Apr 9, 2024 via email

@renenucci
Copy link

Any news? The issue happening

@bruntzcreative
Copy link

Having this issue as well. Have to upgrade and target sdk 34 in 45 days. Is there any news on this?

@brandon-watkins-avcrm
Copy link

brandon-watkins-avcrm commented Aug 2, 2024

Any news on this?? I am still seeing this issue

@bazl-E
Copy link

bazl-E commented Aug 8, 2024

I'm also facing the same issue. any updates

@sajanIocod
Copy link

Any updates on this issue?

@dishankjindal1
Copy link

dishankjindal1 commented Sep 16, 2024

Android 14 users of my production app is facing problems. App is stuck on startup as i am using this package to fetch secure values. After clearing the cache manually it start working.

Ps:- We cant tell all the users to delete cache and run app. This is very bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests