Flutter RSA公钥转PEM

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

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

Dart 复制代码
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/165
  static String publicKey2PemString(RSAPublicKey publicKey) {

    const beginPublicKey = '-----BEGIN PUBLIC KEY-----';
    const endPublicKey = '-----END PUBLIC KEY-----';

    // Create the top level sequence
    var topLevelSeq = ASN1Sequence();

    // Create the sequence holding the algorithm information
    var 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 sequence
    topLevelSeq.add(algorithmSeq);
    topLevelSeq.add(publicKeySeqBitString);
    // topLevelSeq.encode();

    // Encode base64
    var 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;
  }
}
相关推荐
coder_pig2 小时前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜4 小时前
flutter自学笔记5- dart 编码规范
flutter·dart
恋猫de小郭5 小时前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
sunly_1 天前
Flutter:导航,tab切换,顶部固定,列表分页滚动
开发语言·javascript·flutter
敲代码的小强1 天前
Flutter项目兼容鸿蒙Next系统
flutter·华为·harmonyos
Zh-jie2 天前
flutter 快速实现侧边栏
前端·javascript·flutter
truemi.732 天前
flutter --no-color pub get 超时解决方法
android·flutter
王家视频教程图书馆2 天前
flutter 使用dio 请求go语言后台数据接口展示瀑布流图片
flutter
迷雾漫步者2 天前
Flutter组件————AppBar
flutter·跨平台·dart
AiFlutter2 天前
Flutter 开关属性
flutter