I am using bouncy castle for some odd encryption needed for my one client and here is how I generated the file for the public key (I should add that my client only has one key pair used for government submissions, so I only needed the file from their key once off):
public static void getPublicKeyFile()
{
string publicKeyPath = Path.Combine(@"C:\SomeFolder", "publicKey.pem");
if (File.Exists(publicKeyPath))
{
File.Delete(publicKeyPath);
}
using (TextWriter textWriter = new StreamWriter(publicKeyPath, false))
{
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject((RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(strPublicKey)));
pemWriter.Writer.Flush();
}
}