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;
  }
}
相关推荐
lqj_本人8 小时前
鸿蒙next选择 Flutter 开发跨平台应用的原因
flutter·华为·harmonyos
lqj_本人11 小时前
Flutter&鸿蒙next 状态管理框架对比分析
flutter·华为·harmonyos
起司锅仔15 小时前
Flutter启动流程(2)
flutter
hello world smile19 小时前
最全的Flutter中pubspec.yaml及其yaml 语法的使用说明
android·前端·javascript·flutter·dart·yaml·pubspec.yaml
lqj_本人19 小时前
Flutter 的 Widget 概述与常用 Widgets 与鸿蒙 Next 的对比
flutter·harmonyos
iFlyCai19 小时前
极简实现酷炫动效:Flutter隐式动画指南第二篇之一些酷炫的隐式动画效果
flutter
lqj_本人19 小时前
Flutter&鸿蒙next 中使用 MobX 进行状态管理
flutter·华为·harmonyos
lqj_本人19 小时前
Flutter&鸿蒙next 中的 setState 使用场景与最佳实践
flutter·华为·harmonyos
hello world smile20 小时前
Flutter常用命令整理
android·flutter·移动开发·android studio·安卓
lqj_本人1 天前
Flutter&鸿蒙next 中的 Expanded 和 Flexible 使用技巧详解
flutter·harmonyos