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

Flutter RSA公钥转PEM

需添加依赖:pointycastle​​​​​​​

参考链接:https://github.com/bcgit/pc-dart/issues/165 

import 'dart:convert';
import 'dart:typed_data';import 'package:pointycastle/pointycastle.dart';
import 'package:pointycastle/src/platform_check/platform_check.dart';
import "package:pointycastle/export.dart";class RSAUtils {////// [生成RSA公私密钥对](https://github.com/bcgit/pc-dart/blob/master/tutorials/rsa.md)///static AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey> generateKeyPair({int bitLength = 2048}) {final keyGen = RSAKeyGenerator();//初始化final secureRandom = SecureRandom('Fortuna')..seed(KeyParameter(Platform.instance.platformEntropySource().getBytes(32)));keyGen.init(ParametersWithRandom(RSAKeyGeneratorParameters(BigInt.parse('65537'), bitLength, 64),secureRandom));final pair = keyGen.generateKeyPair();final myPublic = pair.publicKey as RSAPublicKey;final myPrivate = pair.privateKey as RSAPrivateKey;return AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey>(myPublic, myPrivate);}/// https://github.com/bcgit/pc-dart/blob/master/tutorials/asn1.md/// https://github.com/bcgit/pc-dart/issues/165static String publicKey2PemString(RSAPublicKey publicKey) {const beginPublicKey = '-----BEGIN PUBLIC KEY-----';const endPublicKey = '-----END PUBLIC KEY-----';// Create the top level sequencevar topLevelSeq = ASN1Sequence();// Create the sequence holding the algorithm informationvar algorithmSeq = ASN1Sequence();var paramsAsn1Obj = ASN1Object.fromBytes(Uint8List.fromList([0x5, 0x0]));algorithmSeq.add(ASN1ObjectIdentifier.fromIdentifierString('1.2.840.113549.1.1.1'));algorithmSeq.add(paramsAsn1Obj);// Create the constructed ASN1BitString/*var modulus = ASN1Integer(publicKey.modulus);var exponent = ASN1Integer(publicKey.exponent);var publicKeySeqBitString = ASN1BitString(elements : [modulus, exponent], tag: ASN1Tags.BIT_STRING_CONSTRUCTED);*/var keySequence = ASN1Sequence();keySequence.add(ASN1Integer(publicKey.modulus));keySequence.add(ASN1Integer(publicKey.exponent));keySequence.encode(encodingRule: ASN1EncodingRule.ENCODING_DER);var publicKeySeqBitString = ASN1BitString(stringValues: keySequence.encodedBytes!);// Add ASN1 objects to the top level sequencetopLevelSeq.add(algorithmSeq);topLevelSeq.add(publicKeySeqBitString);// topLevelSeq.encode();// Encode base64var dataBase64 = base64.encode(topLevelSeq.encodedBytes!);var chunks = _chunk(dataBase64, 64);return '$beginPublicKey\n${chunks.join('\n')}\n$endPublicKey';}static List<String> _chunk(String s, int chunkSize) {var chunked = <String>[];for (var i = 0; i < s.length; i += chunkSize) {var end = (i + chunkSize < s.length) ? i + chunkSize : s.length;chunked.add(s.substring(i, end));}return chunked;}
}
http://www.lryc.cn/news/398242.html

相关文章:

  • 下载站怎么防护DDOS攻击
  • docker拉取镜像-配置阿里云镜像加速
  • ai写作软件哪个好?亲测好用的5款工具分享
  • Blender 中导出模型fbx
  • 景联文科技以高质量多模态数据集赋能AI大模型,精准匹配提升模型性能
  • 【ROS2】中级:tf2-编写监听器(Python)
  • 用QFramework重构飞机大战(Siki Andy的)(下02)(06-0? 游戏界面及之后的所有面板)
  • Apifox报错404:网络错误,请检查网络,或者稍后再试的解决办法
  • 腾讯混元大模型集成LangChain
  • C++心决之stl中那些你不知道的秘密(string篇)
  • date 命令学习
  • 前端vue后端java使用easyexcel框架下载表格xls数据工具类
  • C#,开发过程中技术点GPT问答记录
  • wifi中的PSR技术
  • 电子签章 签到 互动 打卡 创意印章 支持小程序 H5 App
  • Vscode插件推荐——智能切换输入法(Smart IME)
  • SpringBoot实战:轻松实现接口数据脱敏
  • 我们水冷使制动电阻功率密度成倍增加-水冷电阻设计工厂
  • 模板语法指令语法——02
  • Comparable 和 Comparator 接口的区别
  • Python requests爬虫
  • Docker 基本管理及部署
  • Ubuntu下安装配置和调优Docker,支持IPV6
  • Proteus + Keil单片机仿真教程(六)多位LED数码管的动态显示
  • WEB开发-HTML页面更新部分内容
  • 休息时间c++
  • zabbix 自定义监控项及触发器
  • easyExcel 不规则模板导入数据
  • 前端调试技巧(npm Link,vscode调试,浏览器调试等)
  • SSL证书到期自动巡检脚本-推送钉钉告警