Flutter数据加密插件myan_encrypt的使用

Flutter数据加密插件myan_encrypt的使用

特性

  • 正常加密
  • HMAC加密
  • 另一种加密方式

开始使用

pubspec.yaml 文件中添加以下依赖:

dependencies:
  myan_encrypt: ^latest_version

使用示例

下面是一个完整的示例,展示了如何使用 myan_encrypt 插件进行字符串加密和解密。

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:myan_encrypt/myan_encrypt.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Flutter String Encryptor')),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                TextButton(
                  onPressed: () {
                    // 使用 NormalEncrypt 进行普通加密
                    NormalEncrypt normalEncrypt = NormalEncrypt('_encryptionKey');
                    log(normalEncrypt.encrypt('plainText'));
                  },
                  child: const Text('Normal Encrypt'),
                ),
                TextButton(
                  onPressed: () {
                    // 使用 NormalEncrypt 进行普通解密
                    NormalEncrypt normalEncrypt = NormalEncrypt('_encryptionKey');
                    log(normalEncrypt.decrypt(normalEncrypt.encrypt('plainText')));
                  },
                  child: const Text('Normal Decrypt'),
                ),
              ],
            ),
            const Divider(),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                TextButton(
                  onPressed: () {
                    // 使用 EncryptUtil 进行 HMAC 加密
                    EncryptUtil encryptUtil = EncryptUtil('_encryptionKey', '_hmacKey');
                    log(encryptUtil.encrypt('plainText'));
                  },
                  child: const Text('Encrypt I'),
                ),
                TextButton(
                  onPressed: () {
                    // 使用 EncryptUtil 进行 HMAC 解密
                    EncryptUtil encryptUtil = EncryptUtil('_encryptionKey', '_hmacKey');
                    log(encryptUtil.decrypt(encryptUtil.encrypt('plainText')));
                  },
                  child: const Text('Decrypt I'),
                ),
              ],
            ),
            const Divider(),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                TextButton(
                  onPressed: () {
                    // 使用 EncryptUtil2 进行另一种加密
                    EncryptUtil2 encryptUtil2 = EncryptUtil2('encryptionKey');
                    log(encryptUtil2.encrypt('plainText'));
                  },
                  child: const Text('Encrypt II'),
                ),
                TextButton(
                  onPressed: () {
                    // 使用 EncryptUtil2 进行另一种解密
                    EncryptUtil2 encryptUtil2 = EncryptUtil2('encryptionKey');
                    log(encryptUtil2.decrypt(encryptUtil2.encrypt('plainText')));
                  },
                  child: const Text('Decrypt II'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter数据加密插件myan_encrypt的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter数据加密插件myan_encrypt的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用myan_encrypt插件进行数据加密和解密的示例代码。这个插件提供了基本的加密和解密功能,可以用于保护敏感数据。

首先,确保你已经在pubspec.yaml文件中添加了myan_encrypt依赖:

dependencies:
  flutter:
    sdk: flutter
  myan_encrypt: ^最新版本号  # 请替换为实际可用的最新版本号

然后,运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter项目中使用这个插件。以下是一个简单的示例,展示如何加密和解密字符串数据:

import 'package:flutter/material.dart';
import 'package:myan_encrypt/myan_encrypt.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final MyEncrypt _encrypt = MyEncrypt();
  String? originalText = "Hello, Flutter!";
  String? encryptedText = "";
  String? decryptedText = "";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Myan Encrypt Example'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("Original Text: $originalText"),
              SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  // 使用密钥加密数据
                  String key = "mySecretKey123"; // 请使用更强的密钥
                  encryptedText = _encrypt.encrypt(originalText!, key);
                  setState(() {});
                },
                child: Text("Encrypt"),
              ),
              SizedBox(height: 16),
              Text("Encrypted Text: $encryptedText"),
              SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  // 使用相同的密钥解密数据
                  String key = "mySecretKey123"; // 请确保与加密时使用的密钥相同
                  decryptedText = _encrypt.decrypt(encryptedText!, key);
                  setState(() {});
                },
                child: Text("Decrypt"),
              ),
              SizedBox(height: 16),
              Text("Decrypted Text: $decryptedText"),
            ],
          ),
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,它包含三个主要部分:

  1. 原始文本:显示要加密的原始文本。
  2. 加密按钮:点击按钮后,使用指定的密钥对原始文本进行加密,并显示加密后的文本。
  3. 解密按钮:点击按钮后,使用相同的密钥对加密后的文本进行解密,并显示解密后的文本。

请注意,MyEncrypt类及其encryptdecrypt方法可能依赖于特定的加密算法(如AES)。在实际应用中,你应该确保使用强密钥,并遵循最佳安全实践来保护你的数据。

此外,由于myan_encrypt插件的具体实现和API可能会随着版本更新而变化,因此请参考该插件的官方文档以获取最新和最准确的信息。如果插件提供了更高级的加密选项或配置,你可能需要根据具体需求进行调整。

回到顶部