Convert RSACryptoServiceProvider RSA XML key to PKCS8

家住魔仙堡 提交于 2019-12-04 08:16:58

You can use the BouncyCastle C# library to do this. Here is an example:

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.OpenSsl;

namespace DotNetToPkcs8Pem
{
    public class DotNetToPkcs8Pem
    {

        public static void Convert(string privXmlFilename, string privPkcs8Filename) {
            StringBuilder sb = new StringBuilder ();
            string line;
            var xmlIn = new StreamReader (privXmlFilename);
            while ((line = xmlIn.ReadLine ()) != null) {
                sb.Append (line);
            }
            var xmlKey = sb.ToString ();
            var rsa = RSA.Create ();
            rsa.FromXmlString (xmlKey);
            var bcKeyPair = DotNetUtilities.GetRsaKeyPair(rsa);
            var pkcs8Gen = new Pkcs8Generator (bcKeyPair.Private);
            var pemObj = pkcs8Gen.Generate ();
            var pkcs8Out = new StreamWriter (privPkcs8Filename, false);
            var pemWriter = new PemWriter (pkcs8Out);
            pemWriter.WriteObject (pemObj);
            pkcs8Out.Close ();
        }

        public static void Main (string[] args)
        {
            var xmlFile = "exportedDotNetPrivKey.xml";
            var pkcs8File = "privkey.pk8";
            Convert (xmlFile, pkcs8File);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!