Хотите сохранить открытый и закрытый ключи RSA на локальном диске
Я сгенерировал открытый и закрытый ключи RSA в своем приложении и хочу сохранить их в двух разных файлах. Кто-нибудь может мне помочь?
Что я уже пробовал:
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(); Console.WriteLine(); Console.WriteLine(" Key pair is generating....Please wait for while"); Console.WriteLine(); //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); pemWriter1.WriteObject(publickey); pemWriter1.Writer.Flush(); string print_publickey = textWriter1.ToString(); Console.WriteLine("Public key is: {0}", print_publickey); Console.WriteLine(); //To print the private key in pem format TextWriter textWriter2 = new StringWriter(); PemWriter pemWriter2 = new PemWriter(textWriter2); pemWriter2.WriteObject(privatekey); pemWriter2.Writer.Flush(); string print_privatekey = textWriter2.ToString(); Console.WriteLine("Private key is: {0}", print_privatekey); Console.WriteLine(); // want to save public key and private key in local file } } }