[CryptoAPI] RSA private key to PEM
Question
Forge component by João Barata

Hi Guru's

I see that there is a privatekey from PEM but there is no privatekey to PEM. Would that not be great to have that added to this component? I understand that it's possible to convert the XML output of the RSA_NewKey online but would be nice to do that automatically.

Kind regards!

Would this maybe work?

https://stackoverflow.com/questions/27086117/convert-rsacryptoserviceprovider-rsa-xml-key-to-pkcs8


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 ();
        }
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.