using System; using System.Collections.Generic; using System.Linq; using System.Text; using Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RSACng rsa = new RSACng(); X509Certificate2 cert; List<byte> arrBinaryPublicKey = new List<byte>(); byte[] oid = { 0x30, 0xD, 0x6, 0x9, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0xD, 0x1, 0x1, 0x1, 0x5, 0x0 }; // Object ID for RSA //rsa.FromXmlString(xmlFormatedRSAKey); cert = rsa.Key.CreateSelfSignedCertificate(new X500DistinguishedName("CN=something")); //Transform the public key to PEM Base64 Format arrBinaryPublicKey = cert.PublicKey.EncodedKeyValue.RawData.ToList(); arrBinaryPublicKey.Insert(0, 0x0); // Add NULL value CalculateAndAppendLength(ref arrBinaryPublicKey); arrBinaryPublicKey.Insert(0, 0x3); arrBinaryPublicKey.InsertRange(0, oid); CalculateAndAppendLength(ref arrBinaryPublicKey); arrBinaryPublicKey.Insert(0, 0x30); //End Transformation Console.WriteLine(); Console.WriteLine("-----BEGIN PUBLIC KEY-----"); Console.WriteLine(System.Convert.ToBase64String(arrBinaryPublicKey.ToArray())); Console.WriteLine("-----END PUBLIC KEY-----"); } private static void CalculateAndAppendLength(ref List<byte> arrBinaryData) { int nLen; nLen = arrBinaryData.Count; if (nLen <= byte.MaxValue) { arrBinaryData.Insert(0, Convert.ToByte(nLen)); arrBinaryData.Insert(0, 0x81); //This byte means that the length fits in one byte } else { arrBinaryData.Insert(0, Convert.ToByte(nLen % (byte.MaxValue + 1))); arrBinaryData.Insert(0, Convert.ToByte(nLen / (byte.MaxValue + 1))); arrBinaryData.Insert(0, 0x82); //This byte means that the length fits in two byte } } } }
Labels: .NET Framework, Cryptography, PEM, RSA, Security, XML