using System.Collections.Generic;
using System.Security.Cryptography;
using System.Threading.Tasks;
public static void Main()
const string publicKey = @"-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwuMCKOdptWX0DBKKbHDw
XTZSiJxq3pdk97p6OUO9Rwp39RE/YYw0UDYryMbMO8Bhs5CXaXNvQHop2ljCoKtc
5o/njuWM2Y+SZhi8exG2dK1a82MCJBM9ds79UaV98R+ZgNEUVUtYuww4tdOCYWuL
PYaLo/cueEh6/fJEcirLr53S7EIY3uUOhvCo7HcGBrIvho6kEDxmsi08V5jeBNXM
QtI4Fc0mhy+ptHkBT4m6VMZxIo7dpH4RtWXpOR8PBeDbOQRMku43af0RQMpzj/nq
ZHpB6O1lIUHGaNWCEF7NE6+V1ELFS/BFlb/k1zJrou3EBoc5IZizJtFx3JLaax6v
-----END PUBLIC KEY-----";
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKey1 = MakePem(rsa.ExportSubjectPublicKeyInfo(), "PUBLIC KEY");
string privateKey = MakePem(rsa.ExportRSAPrivateKey(), "RSA PRIVATE KEY");
Console.WriteLine(publicKey1);
using var key = RSA.Create();
key.ImportFromPem(publicKey);
Console.WriteLine(key.ExportSubjectPublicKeyInfo());
catch (ArgumentException e)
Console.WriteLine(e.Message);
private static string MakePem(byte[] ber, string header)
StringBuilder builder = new StringBuilder("-----BEGIN ");
builder.AppendLine("-----");
string base64 = Convert.ToBase64String(ber);
const int LineLength = 64;
while (offset < base64.Length)
int lineEnd = Math.Min(offset + LineLength, base64.Length);
builder.AppendLine(base64.Substring(offset, lineEnd - offset));
builder.Append("-----END ");
builder.AppendLine("-----");
return builder.ToString();