Skip to content

Instantly share code, notes, and snippets.

@Jargon64
Last active November 29, 2021 02:11
Show Gist options
  • Save Jargon64/5b172c452827e15b21882f1d76a94be4 to your computer and use it in GitHub Desktop.
Save Jargon64/5b172c452827e15b21882f1d76a94be4 to your computer and use it in GitHub Desktop.
RSACryptoServiceProvider Extension to add FromXmlString and ToXmlString methods for ASP.NET Core
using System;
using System.Security.Cryptography;
using System.Xml;
namespace RSACryptoServiceProviderExtensions
{
public static class RSACryptoServiceProviderExtensions
{
public static void FromXmlString(this RSACryptoServiceProvider rsa, string xmlString)
{
RSAParameters parameters = new RSAParameters();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue"))
{
foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
{
switch (node.Name)
{
case "Modulus": parameters.Modulus = Convert.FromBase64String(node.InnerText); break;
case "Exponent": parameters.Exponent = Convert.FromBase64String(node.InnerText); break;
case "P": parameters.P = Convert.FromBase64String(node.InnerText); break;
case "Q": parameters.Q = Convert.FromBase64String(node.InnerText); break;
case "DP": parameters.DP = Convert.FromBase64String(node.InnerText); break;
case "DQ": parameters.DQ = Convert.FromBase64String(node.InnerText); break;
case "InverseQ": parameters.InverseQ = Convert.FromBase64String(node.InnerText); break;
case "D": parameters.D = Convert.FromBase64String(node.InnerText); break;
}
}
} else
{
throw new Exception("Invalid XML RSA key.");
}
rsa.ImportParameters(parameters);
}
public static string ToXmlString(this RSACryptoServiceProvider rsa)
{
RSAParameters parameters = rsa.ExportParameters(true);
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
Convert.ToBase64String(parameters.Modulus),
Convert.ToBase64String(parameters.Exponent),
Convert.ToBase64String(parameters.P),
Convert.ToBase64String(parameters.Q),
Convert.ToBase64String(parameters.DP),
Convert.ToBase64String(parameters.DQ),
Convert.ToBase64String(parameters.InverseQ),
Convert.ToBase64String(parameters.D));
}
}
}
@Wind010
Copy link

Wind010 commented Mar 6, 2018

This would be preferable:

        public static string ToXmlString(this RSACryptoServiceProvider rsa, bool includePrivateParameters = false)
        {
            RSAParameters parameters = rsa.ExportParameters(includePrivateParameters);

            if (includePrivateParameters)
            {
                return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
                    Convert.ToBase64String(parameters.Modulus),
                    Convert.ToBase64String(parameters.Exponent),
                    Convert.ToBase64String(parameters.P),
                    Convert.ToBase64String(parameters.Q),
                    Convert.ToBase64String(parameters.DP),
                    Convert.ToBase64String(parameters.DQ),
                    Convert.ToBase64String(parameters.InverseQ),
                    Convert.ToBase64String(parameters.D));
            }

            return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
                Convert.ToBase64String(parameters.Modulus),
                Convert.ToBase64String(parameters.Exponent));
        }

@aienabled
Copy link

It's no longer necessary since .NET Core 3.0. But it was pretty useful at the time. Thanks for posting!

@lennane
Copy link

lennane commented May 12, 2021

I'm going to try this. The fact that the MS version does not support Framework is a problem because .NET identity in .NET Core is too difficult to modify for API work. I'll stick with Framework and this module will help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment