xaldarof / encrypted-shared-preferences

Perfect for developers prioritizing security, Encrypted Shared Preferences integrates effortlessly into your projects, offering peace of mind and enhancing data privacy. Safeguard your app's data with this essential encryption solution
Apache License 2.0
12 stars 2 forks source link

Failed assertion: line 8 pos 12: 'key.length == 16': is not true. #11

Closed fanjavaid closed 6 days ago

fanjavaid commented 1 month ago

Hi, thank you for the library

I try to implement it. However got the following error:

E/flutter (25535): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:encrypt_shared_preferences/src/crypto/aes.dart': Failed assertion: line 8 pos 12: 'key.length == 16': is not true.
E/flutter (25535): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:50:61)
E/flutter (25535): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
E/flutter (25535): #2      AESEncryptor.encrypt (package:encrypt_shared_preferences/src/crypto/aes.dart:8:12)
E/flutter (25535): #3      SharedPreferencesDecorator.save (package:encrypt_shared_preferences/src/shared_preferences_decorator.dart:176:30)
E/flutter (25535): #4      SharedPreferencesDecorator.setString (package:encrypt_shared_preferences/src/shared_preferences_decorator.dart:170:12)
E/flutter (25535): #5      EncryptedSharedPreferences.setString (package:encrypt_shared_preferences/src/enc_shared_pref.dart:74:23)
E/flutter (25535): #6      LoginRepositoryImpl.login (package:mentalku/data/repository/authentication/login_repository_impl.dart:39:29)
E/flutter (25535): <asynchronous suspension>
E/flutter (25535): #7      new LoginBloc.<anonymous closure> (package:mentalku/features/authentication/bloc/login_bloc.dart:24:42)
E/flutter (25535): <asynchronous suspension>
E/flutter (25535): #8      Bloc.on.<anonymous closure>.handleEvent (package:bloc/src/bloc.dart:229:13)
E/flutter (25535): <asynchronous suspension>
E/flutter (25535): 

Below is my implementation:

      String key = "very_secret_access_token";
      String? token = response.data?.token;
      if (token != null && token.isNotEmpty) {
        encryptedSharedPref.setString(key, token);
      }
      return ResourceSuccess(
        data: LoginEntity(token: token),
        message: response.message,
      );

And below is main implementation:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EncryptedSharedPreferences.initialize("mentalku_encrypted_sp");
  runApp(MainApp());
}
fanjavaid commented 1 month ago

When i debug my code, i believe it is > 16 chars length.

Screenshot 2024-10-12 at 10 13 31
xaldarof commented 1 month ago

Hi, you should use 16 length key for encryption, for example "my_secret_key123" it is 16 length