当前位置:   article > 正文

Flutter RSA公钥转PEM

Flutter RSA公钥转PEM

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

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

  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:pointycastle/pointycastle.dart';
  4. import 'package:pointycastle/src/platform_check/platform_check.dart';
  5. import "package:pointycastle/export.dart";
  6. class RSAUtils {
  7. ///
  8. /// [生成RSA公私密钥对](https://github.com/bcgit/pc-dart/blob/master/tutorials/rsa.md)
  9. ///
  10. static AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey> generateKeyPair(
  11. {int bitLength = 2048}) {
  12. final keyGen = RSAKeyGenerator();
  13. //初始化
  14. final secureRandom = SecureRandom('Fortuna')
  15. ..seed(KeyParameter(
  16. Platform.instance.platformEntropySource().getBytes(32)));
  17. keyGen.init(ParametersWithRandom(
  18. RSAKeyGeneratorParameters(BigInt.parse('65537'), bitLength, 64),
  19. secureRandom));
  20. final pair = keyGen.generateKeyPair();
  21. final myPublic = pair.publicKey as RSAPublicKey;
  22. final myPrivate = pair.privateKey as RSAPrivateKey;
  23. return AsymmetricKeyPair<RSAPublicKey, RSAPrivateKey>(myPublic, myPrivate);
  24. }
  25. /// https://github.com/bcgit/pc-dart/blob/master/tutorials/asn1.md
  26. /// https://github.com/bcgit/pc-dart/issues/165
  27. static String publicKey2PemString(RSAPublicKey publicKey) {
  28. const beginPublicKey = '-----BEGIN PUBLIC KEY-----';
  29. const endPublicKey = '-----END PUBLIC KEY-----';
  30. // Create the top level sequence
  31. var topLevelSeq = ASN1Sequence();
  32. // Create the sequence holding the algorithm information
  33. var algorithmSeq = ASN1Sequence();
  34. var paramsAsn1Obj = ASN1Object.fromBytes(Uint8List.fromList([0x5, 0x0]));
  35. algorithmSeq.add(ASN1ObjectIdentifier.fromIdentifierString('1.2.840.113549.1.1.1'));
  36. algorithmSeq.add(paramsAsn1Obj);
  37. // Create the constructed ASN1BitString
  38. /*var modulus = ASN1Integer(publicKey.modulus);
  39. var exponent = ASN1Integer(publicKey.exponent);
  40. var publicKeySeqBitString = ASN1BitString(elements : [modulus, exponent], tag: ASN1Tags.BIT_STRING_CONSTRUCTED);*/
  41. var keySequence = ASN1Sequence();
  42. keySequence.add(ASN1Integer(publicKey.modulus));
  43. keySequence.add(ASN1Integer(publicKey.exponent));
  44. keySequence.encode(encodingRule: ASN1EncodingRule.ENCODING_DER);
  45. var publicKeySeqBitString = ASN1BitString(stringValues: keySequence.encodedBytes!);
  46. // Add ASN1 objects to the top level sequence
  47. topLevelSeq.add(algorithmSeq);
  48. topLevelSeq.add(publicKeySeqBitString);
  49. // topLevelSeq.encode();
  50. // Encode base64
  51. var dataBase64 = base64.encode(topLevelSeq.encodedBytes!);
  52. var chunks = _chunk(dataBase64, 64);
  53. return '$beginPublicKey\n${chunks.join('\n')}\n$endPublicKey';
  54. }
  55. static List<String> _chunk(String s, int chunkSize) {
  56. var chunked = <String>[];
  57. for (var i = 0; i < s.length; i += chunkSize) {
  58. var end = (i + chunkSize < s.length) ? i + chunkSize : s.length;
  59. chunked.add(s.substring(i, end));
  60. }
  61. return chunked;
  62. }
  63. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/825271
推荐阅读
相关标签
  

闽ICP备14008679号