当前位置: 首页> 教育> 培训 > Flutter RSA公钥转PEM

Flutter RSA公钥转PEM

时间:2025/7/11 15:07:40来源:https://blog.csdn.net/jjf19891208/article/details/140337008 浏览次数:0次

需添加依赖: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/165static String publicKey2PemString(RSAPublicKey publicKey) {const beginPublicKey = '-----BEGIN PUBLIC KEY-----';const endPublicKey = '-----END PUBLIC KEY-----';// Create the top level sequencevar topLevelSeq = ASN1Sequence();// Create the sequence holding the algorithm informationvar 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 sequencetopLevelSeq.add(algorithmSeq);topLevelSeq.add(publicKeySeqBitString);// topLevelSeq.encode();// Encode base64var 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;}
}
关键字:Flutter RSA公钥转PEM

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: