搜索插件、代码仓与 APP 分类

插件详情v1.0.1

encrypt_shared_preferences

此包为存储在您应用中的敏感信息(如用户凭据、API 密钥或其他机密数据)添加了额外的保护层。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.8万
喜欢数
49
Pub 分数
130

安装插件

flutter pub add encrypt_shared_preferences

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

encrypt_shared_preferences 包通过加密用户凭据和 API 密钥等敏感信息,增强了存储在 SharedPreferences 中的数据的安全性。它可无缝集成到跨多个平台的 Flutter 应用中,为防止未经授权的访问提供额外保护层。通过使用加密技术,即使设备被攻破,也能确保机密数据的安全。该包非常适合处理私有用户数据的应用程序,并且设置简单,易于实现。

适合场景

  • 保护用户登录令牌
  • 安全存储API密钥
  • 保护个人用户数据
  • 加密配置设置
  • 防止在root设备上发生数据泄露
  • 增强应用安全合规性

主要特点

  • 端到端加密
  • 与 SharedPreferences 的轻松集成
  • 跨平台支持
  • 安全密钥管理
  • 最小的样板代码
  • 支持所有主要的Flutter平台

适合对象

  • 处理敏感用户数据的应用程序
  • 金融或医疗应用
  • 需要数据隐私的应用程序
  • 开发人员优先考虑安全
  • 使用 SharedPreferences 的项目已存在
  • 遵循安全最佳实践的团队

使用建议

  • 需要适当的密钥存储策略
  • 可能略微影响性能
  • 需要仔细处理加密密钥
  • 不是服务器端安全的替代品
  • 依赖于底层 SharedPreferences 的可靠性
  • 调试加密数据的额外复杂性

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Shared preferences are a simple key-value storage mechanism that can be used to store small amounts of data. However, since shared preferences are stored in plain text, it is not suitable for storing sensitive information. To address this issue, you can use AES encryption to encrypt the data before saving it to shared preferences.

To use shared preferences with AES encryption support, you can add this package to your project as dependency.

Open pub.dev

Legacy EncryptedSharedPreferences

void main() async {
  final key = "";
  await EncryptedSharedPreferences.initialize(key: key);
  var sharedPref = EncryptedSharedPreferences.getInstance();

  await sharedPref.setString(
      'user_token', 'xxxxxxxxxxxx', notify: true); ////notify = true by default

  sharedPref.getString('user_token'); //xxxxxxxxxxxx

  await sharedPref.setInt('age', 99, notify: true); //notify = true by default

  sharedPref.getInt('age', defaultValue: 1001); //99

  await sharedPref.setDouble('pi', 3.14, notify: true); //notify = true by default

  sharedPref.getDouble('pi'); //3.14

  await sharedPref.setBool('isPremium', true, notify: true); //notify = true by default

  sharedPref.getBool('isPremium'); //true

  await sharedPref.setStringList("stringList", ["apple", "orange", "boom"]);

  sharedPref.getStringList("stringList");

  await sharedPref.remove('user_token', notify: true); //notify = true by default

  await sharedPref.clear(notify: true); //notify = true by default

  await sharedPref.reload();

  final badKeys = {
    "key1"
        "key2"
  };
  await sharedPref.removeWhere((key, value) => badKeys.contains(key));

  sharedPref.observe(key: 'token').listen((event) {
    // event = key
    print(event);
  });

  sharedPref.observeSet(keys: {'key1', 'key2', 'keyN'}).listen((event) {
    // event = key
    print(event);
  });
}

EncryptedSharedPreferencesAsync

void main() async {
  final key = "";
  await EncryptedSharedPreferencesAsync.initialize(key: key);
  var sharedPref = EncryptedSharedPreferencesAsync.getInstance();

  await sharedPref.setString(
      'user_token', 'xxxxxxxxxxxx', notify: true); ////notify = true by default

  await sharedPref.getString('user_token'); //xxxxxxxxxxxx

  await sharedPref.setInt('age', 99, notify: true); //notify = true by default

  await sharedPref.getInt('age', defaultValue: 1001); //99

  await sharedPref.setDouble('pi', 3.14, notify: true); //notify = true by default

  await sharedPref.getDouble('pi'); //3.14

  await sharedPref.setBool('isPremium', true, notify: true); //notify = true by default

  await sharedPref.getBool('isPremium'); //true

  await sharedPref.setStringList("stringList", ["apple", "orange", "boom"]);

  await sharedPref.getStringList("stringList");

  await sharedPref.remove('user_token', notify: true); //notify = true by default

  await sharedPref.clear(notify: true); //notify = true by default

  await sharedPref.reload();

  final badKeys = {
    "key1"
        "key2"
  };
  await sharedPref.removeWhere((key, value) =>
      badKeys.contains(key));

  sharedPref.observe(key: 'token').listen((event) {
    // event = key
    print(event);
  });

  sharedPref.observeSet(keys: {'key1', 'key2', 'keyN'}).listen((event) {
    // event = key
    print(event);
  });
}

[Shared builder] Here is example of how to use SharedBuilder widget

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SharedBuilder(
          listenKeys: const {"key1", "key2"}, //Optional
          builder: (EncryptedSharedPreferences encryptedSharedPreferences, String? updatedKey) {
            return Text("value : ${encryptedSharedPreferences.getString("key1")}");
          },
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            EncryptedSharedPreferences.getInstance()
                .setString('key1', 'dataValue');
            Future.delayed(const Duration(seconds: 3), () {
              EncryptedSharedPreferences.getInstance()
                  .setString('key2', 'dataValue');
            });
          },
        ),
      ),
    );
  }
}

[Shared builder async] Here is example of how to use SharedBuilder widget

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SharedBuilderAsync(
          listenKeys: const {"key1", "key2"}, //Optional
          builder: (EncryptedSharedPreferencesAsync encryptedSharedPreferences,
              String? updatedKey) {
            return Text("value : $updatedKey");
          },
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            EncryptedSharedPreferencesAsync.getInstance()
                .setString('key1', 'dataValue');
            Future.delayed(const Duration(seconds: 3), () {
              EncryptedSharedPreferencesAsync.getInstance()
                  .setString('key2', 'dataValue');
            });
          },
        ),
      ),
    );
  }
}

Also you can add custom external encryptor

class CustomEncryptor extends IEncryptor {
  @override
  String decrypt(String key, String encryptedData) {
    //decryption logic
  }

  @override
  String encrypt(String key, String plainText) {
    //encryption logic
  }
}

void main() {
  final key = "";
  await EncryptedSharedPreferences.initialize(key: key, encryptor: CustomEncryptor());
  var sharedPref = EncryptedSharedPreferences.getInstance();
}

DevTools extension for inspecting values

alt text

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目