简体   繁体   中英

.NET, BouncyCastle : How to save public Key then import it from a File?

Situation

Hi, I am using the bouncy castle API in my .NET project. Until now i can generate randomly the 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[] as well.

Question How can i save the keys into a File ? and how can I import them to after saving them?

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

var certif_dsa = new X509Certificate2(certificat_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();

for more information send me a sessage to aliah32@hotmail.fr

have you tried adding and app configuration file to your project? It allows you to easily save runtime settings in a key/value format(XML)

you can then create a configurationmanager object in your app to save and read your keys; which in your case is the API key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM