C# xml格式公钥转pem格式公钥

转换

 1 public static string XmlPublicKeyToPem(string xmlPublicKey)
 2         {
 3             RSAParameters rsaParam;
 4             using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
 5             {
 6                 rsa.FromXmlString(xmlPublicKey);
 7                 rsaParam = rsa.ExportParameters(false);
 8             }
 9             RsaKeyParameters param = new RsaKeyParameters(false, new BigInteger(1, rsaParam.Modulus), new BigInteger(1, rsaParam.Exponent));
10 
11             string pemPublicKeyStr = null;
12             using (var ms = new MemoryStream())
13             {
14                 using (var sw = new StreamWriter(ms))
15                 {
16                     var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw);
17                     pemWriter.WriteObject(param);
18                     sw.Flush();
19 
20                     byte[] buffer = new byte[ms.Length];
21                     ms.Position = 0;
22                     ms.Read(buffer, 0, (int)ms.Length);
23                     pemPublicKeyStr = Encoding.UTF8.GetString(buffer);
24                 }
25             }
26             return pemPublicKeyStr;
27         }

需要下载BouncyCastle 

可以去官网中直接下载dll文件在需要的项目中引用dll

地址:https://bouncycastle.org/

c#版选择如下

 或者在管理nuget包中下载

 

posted @ 2022-05-18 19:12  流年sugar  阅读(503)  评论(0编辑  收藏  举报