Flutter数据加密插件aes256cipher的使用
Flutter数据加密插件aes256cipher的使用
🛡️ AES256Cipher
AES256Cipher 转换文本为安全模块。
📌 开始使用
这个项目是一个 Flutter 插件包的起点,专门包含平台特定的实现代码,目前仅支持 Android 和 iOS(后续版本将支持更多平台)。
如果你需要开始 Flutter 开发的帮助,可以查看在线文档,其中包含教程、示例、移动开发指导以及完整的 API 参考。
🍔 添加依赖
在 pubspec.yaml
文件中添加以下依赖:
flutter pub add aes256cipher
导入该库:
import 'package:aes256cipher/aes256cipher.dart';
🚀 使用方法
创建实例 🌱
必须输入长度为32的字符作为 key
参数。请勿在不同地方使用不同的 key
。
参数说明:
参数 | 是否必需 | 类型 | 默认值 |
---|---|---|---|
key | ✔️ | String | 无 |
ivSpec | ❌ | bool | true |
创建实例的示例代码:
final AES256Cipher aes256Cipher = AES256Cipher(key: "A1234567B1234567C1234567D1234567");
加密 🔐
final String value = "Something Sentence";
final String encryptResult = aes256Cipher.encrypt(value);
// encryptResult: OUPswS1JeArwaeKSvGtaAeb3C+Sm8UookvDIwwGk9c2XNhtqClmRADo1r4MXUGiY
解密 🔓
final String decryptResult = aes256Cipher.decrypt(encryptResult);
// decryptResult: Something Sentence
iOS 和 Android 示例
⛑️ ivSpec 参数
ivSpec
参数用于提高加密的安全性,默认值为 true
。如果设置为 false
,则每次返回相同的加密结果。
ivSpec = true
final String target = "Something Sentence";
final AES256Cipher cipher = AES256Cipher(key: "ABCDEFGH" * 4, ivSpec: true);
final String result = await cipher.encrypt(target);
Log.d("result:$result");
// result: d6RyFwWMFVcQN7juca9+ZH7L4ISYxbUblvLMe1XIU7yLIVEeJMzysW2FY22LRfX7
// length: 64
ivSpec = false
final String target = "Something Sentence";
final AES256Cipher cipher = AES256Cipher(key: "ABCDEFGH" * 4, ivSpec: false);
final String result = await cipher.encrypt(target);
Log.d("result:$result");
// result: ZEXCHBrpfp05oLP69/KNVO3Baxmrtcrxgx9ZZgysVY0=
// length: 44
完整示例代码
import 'package:aes256cipher/aes256cipher.dart';
import 'package:flutter/material.dart';
import 'package:flutter_logcat/flutter_logcat.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> {
late final AES256Cipher aes256Cipher;
String platformVersion = "Unknown";
String encryptResult = "";
String decryptResult = "";
// This code will be solve your doubt.
final String test1 = "dQuPSKM9NB3WxoHlBNkhlU924xnuJkW9UHBr91oFatepq41N3iIdhEqUTV5UHl77";
final String test2 = "u3mQx2xrhxLh2wDmKjV2Owk8aPxJoJAe5dUlRgb3V7NdqlpUlSxk6yX6r5Q0h3L+";
final TextStyle titleStyle = const TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w600,
);
[@override](/user/override)
void initState() {
super.initState();
aes256Cipher = AES256Cipher(key: "A1234567B1234567C1234567D1234567", ivSpec: false);
loadVersion();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AES256Cipher Example Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text("Platform:", style: titleStyle),
Text(platformVersion),
const SizedBox(height: 12.0,),
const Text("aesKey:",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w600
),
),
const Text("A1234567B1234567C1234567D1234567"),
const SizedBox(height: 12.0,),
Row(
children: [
ElevatedButton(
onPressed: () async {
// key: a * 32
encryptResult = await aes256Cipher.encrypt("Something Sentence");
Log.i("encryptResult:$encryptResult");
Log.i("length:${encryptResult.length}");
// length: Always:65 = iv + encrypted
// length: Always:45 = encrypted
// length: 64(iOS) = iv + encrypted
// length: 44(iOS) = encrypted
setState(() {});
},
child: const Text("encrpyt")),
const Text(" << \"Something Sentence\"")
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(encryptResult),
),
Visibility(
visible: encryptResult.isNotEmpty,
child: const Padding(
padding: EdgeInsets.only(left: 18.0),
child: Text("⌄\n⌄\n⌄"),
),
),
ElevatedButton(
onPressed: () async {
// String result = await aes256Cipher.decrypt("something");
decryptResult = await aes256Cipher.decrypt(encryptResult);
final testResult1 = await aes256Cipher.decrypt(test1);
final testResult2 = await aes256Cipher.decrypt(test2);
Log.x("decryptResult:$decryptResult");
Log.x("decryptResult1:$testResult1");
Log.x("decryptResult2:$testResult2");
setState(() {});
},
child: const Text("decrypt"),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(decryptResult),
),
],
),
),
),
);
}
void loadVersion() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
platformVersion = await aes256Cipher.getPlatformVersion();
setState(() {});
},);
}
}
更多关于Flutter数据加密插件aes256cipher的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter数据加密插件aes256cipher的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
aes256cipher
是一个用于在 Flutter 中进行 AES-256 加密和解密的插件。它提供了一种简单的方式来加密和解密数据,保护用户的敏感信息。
安装 aes256cipher
插件
首先,你需要在 pubspec.yaml
文件中添加 aes256cipher
插件的依赖:
dependencies:
flutter:
sdk: flutter
aes256cipher: ^1.0.0 # 检查最新版本
然后运行 flutter pub get
来安装依赖。
使用 aes256cipher
插件
以下是如何使用 aes256cipher
插件进行 AES-256 加密和解密的基本示例:
-
导入插件
在需要使用加密和解密功能的 Dart 文件中导入
aes256cipher
插件:import 'package:aes256cipher/aes256cipher.dart';
-
加密数据
使用
AES256Cipher.encrypt
方法对数据进行加密。你需要提供一个密钥(key)和要加密的数据(plainText)。void encryptData() async { String key = 'your-256-bit-secret-key'; // 必须是32个字符长度的字符串 String plainText = 'Hello, World!'; String encryptedText = await AES256Cipher.encrypt(key, plainText); print('Encrypted Text: $encryptedText'); }
-
解密数据
使用
AES256Cipher.decrypt
方法对加密数据进行解密。你需要提供相同的密钥(key)和加密后的数据(encryptedText)。void decryptData() async { String key = 'your-256-bit-secret-key'; // 必须是32个字符长度的字符串 String encryptedText = '...'; // 这里填入加密后的字符串 String decryptedText = await AES256Cipher.decrypt(key, encryptedText); print('Decrypted Text: $decryptedText'); }
完整示例
以下是一个完整的示例,展示了如何加密和解密数据:
import 'package:flutter/material.dart';
import 'package:aes256cipher/aes256cipher.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AES256Cipher Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: encryptData,
child: Text('Encrypt Data'),
),
ElevatedButton(
onPressed: decryptData,
child: Text('Decrypt Data'),
),
],
),
),
),
);
}
void encryptData() async {
String key = 'your-256-bit-secret-key'; // 必须是32个字符长度的字符串
String plainText = 'Hello, World!';
String encryptedText = await AES256Cipher.encrypt(key, plainText);
print('Encrypted Text: $encryptedText');
}
void decryptData() async {
String key = 'your-256-bit-secret-key'; // 必须是32个字符长度的字符串
String encryptedText = '...'; // 这里填入加密后的字符串
String decryptedText = await AES256Cipher.decrypt(key, encryptedText);
print('Decrypted Text: $decryptedText');
}
}