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;
  }
}
相关推荐
WDeLiang6 小时前
Flutter - UIKit开发相关指南 - 导航
flutter·ios·dart
程序猿阿伟11 小时前
《Flutter社交应用暗黑奥秘:模式适配与色彩的艺术》
前端·flutter
融云20 小时前
集成指南:如何采用融云 Flutter IMKit 实现双端丝滑社交体验
flutter
EndingCoder1 天前
跨平台移动开发框架React Native和Flutter性能对比
flutter·react native·react.js
Double Point1 天前
`RotationTransition` 是 Flutter 中的一个动画组件,用于实现旋转动画效果
flutter
亚洲小炫风1 天前
flutter 项目工程文件夹组织结构
flutter·flutter工程结构
Double Point1 天前
Flutter 中 vsync
flutter
Double Point1 天前
ScaleTransition 是 Flutter 中的一个动画组件,用于实现缩放动画效果。
flutter
saxihuangxing2 天前
flutter build apk出现的一些奇怪的编译错误
flutter
恋猫de小郭2 天前
Flutter 合并 ‘dot-shorthands‘ 语法糖,Dart 开始支持交叉编译
android·flutter·ios