Last active
November 29, 2021 02:11
-
-
Save Jargon64/5b172c452827e15b21882f1d76a94be4 to your computer and use it in GitHub Desktop.
RSACryptoServiceProvider Extension to add FromXmlString and ToXmlString methods for ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
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));
}
It's no longer necessary since .NET Core 3.0. But it was pretty useful at the time. Thanks for posting!
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
The ToXmlString method could be written as: