Flutter数据加密插件gunjancryption的使用

Flutter数据加密插件gunjancryption的使用

Gunjancryption 是一个用于Flutter应用的加密插件,它提供了基于RSA和AES算法的强大加密、解密和签名功能。该插件适用于需要高性能、高安全性和高可靠性的混合加密工作流。

此插件支持对JSON数据进行加密、对有效负载进行签名以及解密混合加密的数据,使其成为在Flutter应用程序中保护敏感数据的理想选择。

功能

  • 混合加密:结合RSA和AES实现安全高效的加密。
  • 数字签名:生成和验证数字签名以确保有效负载的安全性。
  • RSA密钥管理:从PEM文件加载和管理RSA密钥。
  • 验证:验证加密数据的完整性并验证有效负载。
  • Base64编码/解码:轻松处理密钥和有效负载的Base64编码。

安装

将插件添加到你的 pubspec.yaml 文件中:

dependencies:
  gunjancryption: ^1.0.0

然后运行以下命令来获取依赖项:

flutter pub get

开始使用

导入插件

import 'package:gunjancryption/gunjancryption.dart';

示例用法

1. 加载RSA密钥

从存储在 assets 文件夹中的PEM文件加载公钥和私钥。

final gunjancryption = Gunjancryption();

final publicKey = await gunjancryption.loadPublicKeyFromPemFile(
  fullPath: 'assets/public_key.pem',
);

final privateKey = await gunjancryption.loadPrivateKeyFromPemFile(
  fullPath: 'assets/private_key.pem',
);

2. 加密JSON数据

使用RSA公钥加密JSON数据。

final rawJSONData = {'name': 'Alice', 'age': 25, 'city': 'Wonderland'};

final encryptedData = await gunjancryption.encryptJsonDataWithPrivateKey(
  publicKey: publicKey,
  rawJSONData: rawJSONData,
);

print('Encrypted Data: $encryptedData');

3. 解密混合加密的数据

使用RSA私钥解密混合加密的数据。

final encryptedDataObject = EncryptedData(
  encryptedKey: base64Encode(Uint8List(16)), // 模拟密钥
  iv: base64Encode(Uint8List(16)),          // 模拟IV
  encryptedData: base64Encode(Uint8List(64)), // 模拟加密的数据
  privateKey: privateKey,
);

final decryptedData = await gunjancryption.decryptRSAHybridData(
  encryptedDataObject,
);

print('Decrypted Data: $decryptedData');

4. 生成数字签名

为JSON有效负载生成数字签名。

final signature = await gunjancryption.getSignature(
  rawJSONData: {'transactionId': '12345', 'amount': 100, 'currency': 'USD'},
  fullKeyPath: 'assets/private_key.pem',
);

print('Digital Signature: $signature');

5. 验证加密数据

验证加密数据是否格式正确且有效。

final isValid = gunjancryption.verifyEncryptedData(encryptedDataObject);

print('Encrypted Data is Valid: $isValid');

详细API文档

loadPrivateKeyFromPemFile

签名

Future<RSAPrivateKey> loadPrivateKeyFromPemFile({required String fullPath});

描述: 从PEM文件加载RSA私钥。

参数

  • fullPath: 私钥PEM文件的完整路径。

返回值: 一个 RSAPrivateKey 对象。

loadPublicKeyFromPemFile

签名

Future<RSAPublicKey> loadPublicKeyFromPemFile({required String fullPath});

描述: 从PEM文件加载RSA公钥。

参数

  • fullPath: 公钥PEM文件的完整路径。

返回值: 一个 RSAPublicKey 对象。

encryptJsonDataWithPrivateKey

签名

Future<String> encryptJsonDataWithPrivateKey({
  required RSAPublicKey publicKey,
  required Map<String, Object> rawJSONData,
});

描述: 使用RSA公钥加密JSON数据。

参数

  • publicKey: RSA公钥。
  • rawJSONData: 要加密的JSON数据。

返回值: Base64编码的加密数据字符串。

decryptRSAHybridData

签名

Future<dynamic> decryptRSAHybridData(EncryptedData encryptedData);

描述: 使用RSA和AES算法解密混合加密的数据。

参数

  • encryptedData: 包含加密密钥、IV和数据的 EncryptedData 对象。

返回值: 解密后的JSON有效负载。

getSignature

签名

Future<String> getSignature({
  required Map<String, Object> rawJSONData,
  required String fullKeyPath,
});

描述: 使用RSA私钥为JSON有效负载生成数字签名。

参数

  • rawJSONData: 要签名的JSON有效负载。
  • fullKeyPath: 私钥PEM文件的完整路径。

返回值: Base64编码的数字签名字符串。

verifyEncryptedData

签名

bool verifyEncryptedData(EncryptedData data);

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

1 回复

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


gunjancryption 是一个用于 Flutter 的加密和解密数据的插件。它提供了简单的 API 来执行对称加密和解密操作。以下是如何在 Flutter 项目中使用 gunjancryption 插件的步骤:

1. 添加依赖项

首先,你需要在 pubspec.yaml 文件中添加 gunjancryption 插件的依赖项。

dependencies:
  flutter:
    sdk: flutter
  gunjancryption: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖项。

2. 导入插件

在你的 Dart 文件中导入 gunjancryption 插件。

import 'package:gunjancryption/gunjancryption.dart';

3. 使用加密和解密功能

gunjancryption 插件提供了 encryptdecrypt 方法来执行加密和解密操作。

加密数据

void encryptData() async {
  String plainText = "Hello, World!";
  String key = "mysecretkey12345"; // 密钥必须是 16、24 或 32 个字符长

  String encryptedText = await Gunjancryption.encrypt(plainText, key);
  print("Encrypted Text: $encryptedText");
}

解密数据

void decryptData() async {
  String encryptedText = "U2FsdGVkX1+..."; // 替换为实际的加密文本
  String key = "mysecretkey12345"; // 使用与加密时相同的密钥

  String decryptedText = await Gunjancryption.decrypt(encryptedText, key);
  print("Decrypted Text: $decryptedText");
}

4. 处理异常

在实际使用中,加密和解密操作可能会失败,因此最好使用 try-catch 块来处理异常。

void encryptAndDecrypt() async {
  String plainText = "Hello, World!";
  String key = "mysecretkey12345";

  try {
    String encryptedText = await Gunjancryption.encrypt(plainText, key);
    print("Encrypted Text: $encryptedText");

    String decryptedText = await Gunjancryption.decrypt(encryptedText, key);
    print("Decrypted Text: $decryptedText");
  } catch (e) {
    print("Error: $e");
  }
}

5. 注意事项

  • 密钥长度:密钥的长度必须是 16、24 或 32 个字符,分别对应 AES-128、AES-192 和 AES-256 加密算法。
  • 安全性:确保密钥的安全存储和管理,不要在客户端硬编码密钥。
  • 性能:加密和解密操作可能会影响性能,尤其是在处理大量数据时。

6. 示例代码

以下是一个完整的示例代码,展示了如何使用 gunjancryption 插件进行加密和解密。

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Gunjancryption Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              encryptAndDecrypt();
            },
            child: Text('Encrypt and Decrypt'),
          ),
        ),
      ),
    );
  }
}

void encryptAndDecrypt() async {
  String plainText = "Hello, World!";
  String key = "mysecretkey12345";

  try {
    String encryptedText = await Gunjancryption.encrypt(plainText, key);
    print("Encrypted Text: $encryptedText");

    String decryptedText = await Gunjancryption.decrypt(encryptedText, key);
    print("Decrypted Text: $decryptedText");
  } catch (e) {
    print("Error: $e");
  }
}
回到顶部