I want to Convert the Base64 Private and Public Keys to XMl String
Please Help !....
I want to Convert the Base64 Private and Public Keys to XMl String
Please Help !....
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted May 23, 2023, 7:53 AM EDT
Check this
using System; using System.Security.Cryptography; using System.Text; using System.Xml; public class Base64ToXmlConverter { public static string ConvertToXmlString(string base64PrivateKey, string base64PublicKey) { try { byte[] privateKeyBytes = Convert.FromBase64String(base64PrivateKey); byte[] publicKeyBytes = Convert.FromBase64String(base64PublicKey); RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); rsaProvider.ImportCspBlob(privateKeyBytes); string privateKeyXml = rsaProvider.ToXmlString(true); rsaProvider.ImportCspBlob(publicKeyBytes); string publicKeyXml = rsaProvider.ToXmlString(false); XmlDocument xmlDoc = new XmlDocument(); XmlElement rootElement = xmlDoc.CreateElement("RSAKeyValue"); xmlDoc.AppendChild(rootElement); XmlElement privateKeyElement = xmlDoc.CreateElement("PrivateKey"); privateKeyElement.InnerText = privateKeyXml; rootElement.AppendChild(privateKeyElement); XmlElement publicKeyElement = xmlDoc.CreateElement("PublicKey"); publicKeyElement.InnerText = publicKeyXml; rootElement.AppendChild(publicKeyElement); StringBuilder stringBuilder = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(stringBuilder)) { xmlDoc.WriteTo(writer); } string xmlString = stringBuilder.ToString(); return xmlString; } catch (Exception ex) { // Handle any exceptions that may occur during the conversion Console.WriteLine("An error occurred: " + ex.Message); return null; } } }