.NET, BouncyCastle: how to save a public key and then import it from a file?

Situation

Hi, I am using Bouncy Castle API in my .NET project. So far, I can generate random private and public keys using

RsaKeyPairGenerator g = new RsaKeyPairGenerator();
g.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keys = g.GenerateKeyPair();
privateKey = keys.Private;
publicKey = keys.Public;

I can encrypt / decrypt byte[].

Question How to save keys to a file? and how can i import them after saving them?

+3
source share
2 answers

this is part of my code project: (who should save the public key to a file)

var certif_dsa = new X509Certificate2 (certat_dsa, "password");

var pubkey_dsa = certif_dsa.GetPublicKeyString ();

StreamWriter fluxInfos_dsa = null; // le fichier de la clé publique trouver le au debug

                using (fluxInfos_dsa = new StreamWriter("CléPub.txt"))
                {
                    string ligne = " ****  Le fichier de clé publique :  ***** ";

                    fluxInfos_dsa.WriteLine(ligne);
                    fluxInfos_dsa.WriteLine();
                    fluxInfos_dsa.WriteLine("début : ");
                    fluxInfos_dsa.WriteLine(pubkey_dsa);
                    fluxInfos_dsa.WriteLine();
                    fluxInfos_dsa.WriteLine();
                    fluxInfos_dsa.WriteLine(" Fin de la clé publique. ");


                }
                fluxInfos_dsa.Close();

aliah32@hotmail.fr

+1

? / (XML)

ConfigurationManager ; API

-1

Source: https://habr.com/ru/post/1796629/


All Articles