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/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;
  }
}

相关推荐

  1. Flutter RSAPEM

    2024-07-11 06:12:07       22 阅读
  2. Git生成

    2024-07-11 06:12:07       52 阅读
  3. php 函数 三

    2024-07-11 06:12:07       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 06:12:07       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 06:12:07       55 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 06:12:07       46 阅读
  4. Python语言-面向对象

    2024-07-11 06:12:07       56 阅读

热门阅读

  1. CentOS 系统监控项

    2024-07-11 06:12:07       19 阅读
  2. UCOS-III 与UCOS-III主要功能差异

    2024-07-11 06:12:07       14 阅读
  3. 用 adb 来模拟手机插上电源和拔掉电源的情形

    2024-07-11 06:12:07       19 阅读
  4. OpenResty程序如何连接开启了TLS的Redis?

    2024-07-11 06:12:07       22 阅读
  5. Jitsi Meet指定用户成为主持人

    2024-07-11 06:12:07       17 阅读
  6. Rust编程-编写自动化测试

    2024-07-11 06:12:07       24 阅读
  7. 开源大势所趋

    2024-07-11 06:12:07       21 阅读
  8. Sqlmap中文使用手册 - Target模块参数使用

    2024-07-11 06:12:07       23 阅读
  9. Grind 75 - Leetcode146 LRU缓存

    2024-07-11 06:12:07       23 阅读
  10. vue3 学习笔记02 -- 配置路由router+导航守卫

    2024-07-11 06:12:07       24 阅读