Click here to Skip to main content
15,174,572 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have generated RSA public and private key in my application and I want to stor it into two different files. Can anyone help me?

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using System.Xml;

namespace RSAKeyGeneration
    class Program
        static void Main(string[] args)
            // Declaring Variables //
            string SourceData;
            byte[] tmpSource;

            // Enter your Message Text //
            Console.WriteLine("Enter the Message you want to Encrypt");
            SourceData = Console.ReadLine();

            //Create a byte array from Source data
            tmpSource = ASCIIEncoding.ASCII.GetBytes(SourceData);
            Console.WriteLine(" Key pair is generating....Please wait for while");

            //RSA key pair Generator generates the RSA kay pair based on the Random Number and the strength of key required 
            RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator();
            rsaKeyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
            AsymmetricCipherKeyPair keyPair = rsaKeyPairGenerator.GenerateKeyPair();

            //Extracting the private key from the pair
            RsaKeyParameters privatekey = (RsaKeyParameters)keyPair.Private;
            RsaKeyParameters publickey = (RsaKeyParameters)keyPair.Public;

            //To print the public key in pem format
            TextWriter textWriter1 = new StringWriter();
            PemWriter pemWriter1 = new PemWriter(textWriter1);
            string print_publickey = textWriter1.ToString();
            Console.WriteLine("Public key is: {0}", print_publickey);

            //To print the private key in pem format
            TextWriter textWriter2 = new StringWriter();
            PemWriter pemWriter2 = new PemWriter(textWriter2);
            string print_privatekey = textWriter2.ToString();
            Console.WriteLine("Private key is: {0}", print_privatekey);

            // want to save public key and private key in local file

Updated 6-Oct-20 3:21am

The simplest way might be to use:
File.WriteAllText(@"C:\myPublicKey.txt", print_publickey);
See: File.WriteAllText Method (System.IO) | Microsoft Docs[^]
Maciej Los 4-Jan-19 7:08am
You should use the proper classes provided by the .NET framework: Generating Keys for Encryption and Decryption | Microsoft Docs[^].
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))

    using (TextWriter textWriter = new StreamWriter(publicKeyPath, false))
        PemWriter pemWriter = new PemWriter(textWriter);


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900