juliansteenbakker / flutter_secure_storage

A Flutter plugin to store data in secure storage
https://pub.dartlang.org/packages/flutter_secure_storage
BSD 3-Clause "New" or "Revised" License
1.14k stars 391 forks source link

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

Closed RANSoftRA closed 3 years ago

RANSoftRA commented 3 years ago

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:

RANSoftRA commented 3 years ago

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 https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-471642126)

RANSoftRA commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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
        ....
    }
    ...
}
...
}
vanlooverenkoen commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

SanjiKir commented 3 years ago

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 commented 3 years ago

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

jasaw commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

romreed commented 3 years ago

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

rsmaximiliano commented 3 years ago

Same issue with flutter_secure_storage: ^4.2.0

comatory commented 3 years ago

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

justoke commented 2 years ago

Still an issue.

zigaspica commented 2 years ago

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

NamanShergill commented 2 years ago

I have opened a new issue to track this #354

neiljaywarner commented 1 year ago

If the change to the manifest works please add to readme

KyawLwinSoe27 commented 1 year ago

Thanks a lot, This is works now.

chrisDK1977 commented 10 months ago

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)
                                                                                                <asynchronous suspension>
                                                                                                #2      MethodChannelFlutterSecureStorage.readAll (package:flutter_secure_stora
bahman2000 commented 10 months ago

Still an issue.

flutter_secure_storage 9.0.0 in Android 9.

acheronian commented 9 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 7 months ago

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 commented 7 months ago

and this is related to targeting sdk level 34? i think i got my problem to go away by going back to 33 - but we have to go to 34 very soon.

On Tue, Apr 9, 2024 at 7:38 AM Rene Nucci @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/mogol/flutter_secure_storage/issues/210#issuecomment-2045087290, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAXBGYO2U4LB75Z5N36QT5DY4POKRAVCNFSM4YD6HLL2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBUGUYDQNZSHEYA . You are receiving this because you commented.Message ID: @.***>

renenucci commented 4 months ago

Any news? The issue happening

bruntzcreative commented 4 months ago

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

brandon-watkins-avcrm commented 4 months ago

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

bazl-E commented 3 months ago

I'm also facing the same issue. any updates

sajanIocod commented 3 months ago

Any updates on this issue?

dishankjindal1 commented 2 months ago

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.

bryan1anderson commented 1 month ago

This is a problem for us too

bryan1anderson commented 1 month ago

@mogol I think we might need to re-open this issue.

bryan1anderson commented 1 month ago

@dishankjindal1 How are you having them clear cache?

NamanShergill commented 1 month ago

@bryan1anderson There’s another (already open) issue for this actually, #354. I opened it because this one was closed, so it’s still being tracked fwiw.

NamanShergill commented 1 month ago

Based on the updates there, it might be fixed in an upcoming rewrite https://github.com/mogol/flutter_secure_storage/issues/769