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;
  }
}
相关推荐
比格丽巴格丽抱7 小时前
flutter项目苹果编译运行打包上线
flutter·ios
SoaringHeart8 小时前
Flutter进阶:基于 MLKit 的 OCR 文字识别
前端·flutter
AiFlutter12 小时前
Flutter通过 Coap发送组播
flutter
嘟嘟叽1 天前
初学 flutter 环境变量配置
flutter
iFlyCai1 天前
深入理解Flutter生命周期函数之StatefulWidget(一)
flutter·生命周期·dart·statefulwidget
sunly_1 天前
Flutter:photo_view图片预览功能
android·javascript·flutter
Summer不秃2 天前
Flutter中sqflite的使用案例
flutter
sunly_2 天前
Flutter:TweenAnimationBuilder自定义隐式动画
flutter
AiFlutter2 天前
Flutter-Web首次加载时添加动画
前端·flutter
Allen Su2 天前
【Flutter 问题系列第 84 篇】如何清除指定网络图片的缓存
flutter·缓存·如何清除指定网络图片的缓存·网络图片缓存