3

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?

Jon
  • 16,212
  • 8
  • 50
  • 62
WolFSharp
  • 169
  • 4
  • 14
  • 1
    This might be a good place to start: http://stackoverflow.com/questions/844997/encrypting-a-bouncycastle-rsa-key-pair-and-storing-in-a-sql2008-database – iwalkbarefoot Mar 06 '11 at 12:33

2 Answers2

1

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

Mely
  • 317
  • 2
  • 4
  • 16
-1

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

Warebot
  • 1
  • 1
  • No, i did not do so because i can't get the keys value so easily. After some research i found that I need to serialize them to extract keys value. – WolFSharp Mar 06 '11 at 12:28
  • gotcha. sorry i misunderstood what you were trying to do. thought you were looking for a way to save the individual values for private and public keys. you are correct, serialize/deserialize is your best bet to save the entire object. – Warebot Mar 06 '11 at 13:00