当前位置: 首页 > news >正文

基于最新版的flutter pointycastle: ^3.9.1的AES加密

基于最新版的flutter pointycastle: ^3.9.1的AES加密

  • 自己添加pointycastle: ^3.9.1库
  • config.dart
  • aes_encrypt.dart

自己添加pointycastle: ^3.9.1库

config.dart

import 'dart:convert';
import 'dart:typed_data';class Config {static String password = '成都推理计算科技'; // 16字节(128位)的AES密钥static Uint8List iv = Uint8List.fromList([0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]); // 初始化向量(IV),对于AES/CBC/PKCS7Padding是必需的。自己按照ASCII表填点对自己有用的static int aesSize = 128;//可以填128,192,256static String aesSalt=latin1.decode([10, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);//salt可以用库里面的函数生成
}

aes_encrypt.dart

import 'dart:convert';
import 'dart:typed_data';import 'package:pointycastle/pointycastle.dart';
import 'package:pointycastle/src/platform_check/platform_check.dart';
import 'config.dart';class AesEncrypt {final iv = Config.iv;final password = Config.password;final aesSize = Config.aesSize;final aesSalt = Config.aesSalt;AesEncrypt();Uint8List aesCbcEncrypt(Uint8List key, Uint8List iv, Uint8List paddedPlaintext) {if (![128, 192, 256].contains(key.length * 8)) {throw ArgumentError.value(key, 'key', 'invalid key length for AES');}if (iv.length * 8 != 128) {throw ArgumentError.value(iv, 'iv', 'invalid IV length for AES');}if (paddedPlaintext.length * 8 % 128 != 0) {throw ArgumentError.value(paddedPlaintext, 'paddedPlaintext', 'invalid length for AES');}final cbc = BlockCipher('AES/CBC')..init(true, ParametersWithIV(KeyParameter(key), iv)); // true=encryptfinal cipherText = Uint8List(paddedPlaintext.length); // allocate spacevar offset = 0;while (offset < paddedPlaintext.length) {offset += cbc.processBlock(paddedPlaintext, offset, cipherText, offset);}assert(offset == paddedPlaintext.length);return cipherText;}Uint8List aesCbcDecrypt(Uint8List key, Uint8List iv, Uint8List cipherText) {if (![128, 192, 256].contains(key.length * 8)) {throw ArgumentError.value(key, 'key', 'invalid key length for AES');}if (iv.length * 8 != 128) {throw ArgumentError.value(iv, 'iv', 'invalid IV length for AES');}if (cipherText.length * 8 % 128 != 0) {throw ArgumentError.value(cipherText, 'cipherText', 'invalid length for AES');}final cbc = BlockCipher('AES/CBC')..init(false, ParametersWithIV(KeyParameter(key), iv)); // false=decryptfinal paddedPlainText = Uint8List(cipherText.length); // allocate spacevar offset = 0;while (offset < cipherText.length) {offset += cbc.processBlock(cipherText, offset, paddedPlainText, offset);}assert(offset == cipherText.length);return paddedPlainText;}String bin2hex(Uint8List bytes, {String? separator, int? wrap}) {var len = 0;final buf = StringBuffer();for (final b in bytes) {final s = b.toRadixString(16);if (buf.isNotEmpty && separator != null) {buf.write(separator);len += separator.length;}if (wrap != null && wrap < len + 2) {buf.write('\n');len = 0;}buf.write('${(s.length == 1) ? '0' : ''}$s');len += 2;}return buf.toString();}Uint8List hex2bin(String hexStr) {if (hexStr.length % 2 != 0) {throw const FormatException('not an even number of hexadecimal characters');}final result = Uint8List(hexStr.length ~/ 2);for (var i = 0; i < result.length; i++) {result[i] = int.parse(hexStr.substring(2 * i, 2 * (i + 1)), radix: 16);}return result;}Uint8List pad(Uint8List bytes, int blockSizeBytes) {final padLength = blockSizeBytes - (bytes.length % blockSizeBytes);final padded = Uint8List(bytes.length + padLength)..setAll(0, bytes);Padding('PKCS7').addPadding(padded, bytes.length);return padded;}Uint8List unpad(Uint8List padded) => padded.sublist(0, padded.length - Padding('PKCS7').padCount(padded));Uint8List passphraseToKey(String passPhrase, {String salt = '', int iterations = 30000, required int bitLength}) {if (![128, 192, 256].contains(bitLength)) {throw ArgumentError.value(bitLength, 'bitLength', 'invalid for AES');}final numBytes = bitLength ~/ 8;final kd = KeyDerivator('SHA-256/HMAC/PBKDF2')..init(Pbkdf2Parameters(utf8.encode(salt), iterations, numBytes));return kd.process(utf8.encode(passPhrase));}Uint8List? generateRandomBytes(int numBytes) {if (_secureRandom == null) {// First invocation: create _secureRandom and seed it_secureRandom = SecureRandom('Fortuna');_secureRandom!.seed(KeyParameter(Platform.instance.platformEntropySource().getBytes(32)));}// Use it to generate the random bytesfinal iv = _secureRandom!.nextBytes(numBytes);return iv;}SecureRandom? _secureRandom;Uint8List encrypt(String textToEncrypt) {final cipherText = aesCbcEncrypt(passphraseToKey(password, salt: aesSalt, bitLength: aesSize), iv, pad(utf8.encode(textToEncrypt), 16));return cipherText;}String decrypt(List<int> cipherListInt) {Uint8List cipherText = Uint8List.fromList(cipherListInt);final paddedDecryptedBytes =aesCbcDecrypt(passphraseToKey(password, salt: aesSalt, bitLength: aesSize), iv, cipherText);final decryptedBytes = unpad(paddedDecryptedBytes);final decryptedText = utf8.decode(decryptedBytes);return decryptedText;}
}
http://www.lryc.cn/news/404040.html

相关文章:

  • K8S内存资源配置
  • 【多任务YOLO】 A-YOLOM: You Only Look at Once for Real-Time and Generic Multi-Task
  • 数学建模--灰色关联分析法
  • NetSuite Saved Search迁移工具
  • Java IO模型深入解析:BIO、NIO与AIO
  • 《从C/C++到Java入门指南》- 9.字符和字符串
  • Adobe国际认证详解-视频剪辑
  • 昇思25天学习打卡营第19天|MindNLP ChatGLM-6B StreamChat
  • .NET在游戏开发中有哪些成功的案例?
  • 搜维尔科技:我们用xsens完成了一系列高难度的运动项目并且捕获动作
  • 深入探讨:Node.js、Vue、SSH服务与SSH免密登录
  • Unity UGUI 之 Toggle
  • Git报错:error: fsmonitor--daemon failed to start处理方法
  • 【项目】星辰博客介绍
  • 从0开始的STM32HAL库学习6
  • Elasticsearch ILM 热节点迁移至冷节点 IO 打满、影响读写解决方案探讨
  • STM32中PC13引脚可以当做普通引脚使用吗?如何配置STM32的TAMPER?
  • k8s学习——创建测试镜像
  • 重塑水资源管理的新篇章:深度剖析智慧水利解决方案的前沿技术与应用,探索其如何推动水利行业向智能化、高效化、可持续化方向迈进
  • C#实现数据采集系统-查询报文处理和响应报文分析处理
  • 【音视频】AAC编码器与ffmpeg生成AAC数据
  • Linux openEuler_24.03部署MySQL_8.4.0 LTS安装实测验证安装以及测试连接全过程实操手册
  • 【Elasticsearch7】3-基本操作
  • 给定一整数数组,其中有p种数出现了奇数次,其他数都出现了偶数次,怎么找到这p个数?
  • RICHTEK立锜科技 WIFI 7电源参考设计
  • CUDA编程00 - 配置CUDA开发环境
  • HTML5大作业三农有机,农产品,农庄,农旅网站源码
  • Spark的动态资源分配算法
  • Python 爬虫技术 第06节 HTTP协议与Web基础知识
  • js | 原型链