Convert RSACryptoServiceProvider RSA XML key to PKCS8

4.8k views Asked by At

I use RSACryptoServiceProvider in my C# code, and it's working good I could Export RSA XML private and public keys, but for some reason I need to convert this key to *.pem with format (PKCS8 Unencrypted)

my private key like

<RSAKeyValue>
  <Modulus>zYs6baSaRDgLc5fG3/ozwGwS8OAvxd9quE2su+7//Lnz2WuIbe8jMc5kL7p2gsPZUEoB88SFHdGZSYiZJhERDc/1iQ4VsW2/ra8DBW+1m3K33EDvvI3rJ0NIyuRNuwoYql3hYdY0cdW6Gru9ZARCpptgBSJEiUSPjlzDEXz3RaU=</Modulus>
  <Exponent>AQAB</Exponent>
  <P>89aDVrzNZuDfkCCUR6LKlxHul64zb4EBUN4vr6VZ4LrjEqT6m7+a/8lIrgSK03R1hDdOzeg79eS0OLH5cbe6xQ==</P>
  <Q>18vAJpkwmhDSMjcPAE/4LufpZ6ukaOgkcPj9LngZSQlIt/D19LT/4NqZ2BrukX5bFCL6WPPLrJmmYlG/Jj+NYQ==</Q>
  <DP>whMgoi7PxWyCRDLKEmJNoQQSxQNPyRYCuBzAE6sl6CqXhUVuefx9H81TXb4byMXOqxdiT5zh7IrFrsJo6cv0iQ==</DP>
  <DQ>ylGZAZc2XfvPVo0QX3aadT+xMSVXq5OaNuD6XorzCerOZsZfkuHv1NyeyGHXskkJE0QH+0X3ewdDW4LQ+Ac1gQ==</DQ>
  <InverseQ>v4nkldQ2UKhJUZiyJbKnp2xopzfmreDQTTtGbaF4X0UP5pDGJndmUhj1SHITUAVqwUBa+hSl8TvzcO4IRpkOFg==</InverseQ>
  <D>JopbL9Dj7jO2DfwhGcE/7+4z1P+sZYVhbOJ/p5OGY1x4uUVdBQQIjN++0GY9r4JKnq/p0swAC9nPzMZ0tPsRP7gV5x7fmySlMP4tGpALDHbDgGfN098m2+JqK+/MIMYTAdLbnAcwk/1Lp8mB7Urm/OF/M0LOUzRQ1mwXVXNG/sE=</D>
</RSAKeyValue>

I want to convert it to (PKCS8 Unencrypted) like below

-----BEG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XRAWYig9zjq8garYEmIznoIc02i
4Q/X1uu1RzUCgYEAxzQFxj/4hsuUeLrIVsgWz+Tc6eZoYapmqdt/wNkUqIslLoko
e5suhy2OPkrKLck/yfMDWH8eT7kKvpRkSac12v4f0asJPv7tY3snHAHNJZa/yXvW
Nzw4MJ1rpPAlIpvML3JoLiM3yQsV6haM0ulzVKO9biIQd2wzIs6DPgd15DECgYEA
lQ2vaAW4GEcVdgJvRfznt9xx/XyHMwqSIYfOZFCRn1ZFktmpKu3g40v8U59SkIFE
2c4THeUzCkN2v3dkE40+WuL1dJZdPAcLw7V+Oj0glRw0Q/X0hSbz5LMhgQ5VXLmK
LP//183it351h0jkh9MVu3QAGLr1AEBq5pr/KgH2vrECgYEAr6y3cCo/gdUi6v3Y
39B6LTcmqeKZbpqh3LzBl+I6Ke9t/TOqCZhwtuolMDkFLhZ9woGDeB1VBfV7yUW4
xyF3rE6uMSEZyD5ivku7VC2Gsz/2XuRGl8iToGXgGxzzg6HMQr6Hw36+0VFzzxSo
xn9/6PuHd1bNooOv1S3s0WVkTJw=
-----END PRIVATE KEY-----

I'm not professional in this security staff, and I'm not sure could we convert it of not? could anyone help with code sample?

1

There are 1 answers

5
President James K. Polk On BEST ANSWER

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