C# .NET、BouncyCastle:如何保存公钥,然后从文件中导入?

C# .NET、BouncyCastle:如何保存公钥,然后从文件中导入?,c#,.net,rsa,bouncycastle,C#,.net,Rsa,Bouncycastle,形势 您好,我正在.NET项目中使用bouncy castle API。到目前为止,我可以使用 RsaKeyPairGenerator g = new RsaKeyPairGenerator(); g.Init(new KeyGenerationParameters(new SecureRandom(), 1024)); AsymmetricCipherKeyPair keys = g.GenerateKeyPair(); privateKey = keys.Private; publicKey

形势

您好,我正在.NET项目中使用bouncy castle API。到目前为止,我可以使用

RsaKeyPairGenerator g = new RsaKeyPairGenerator();
g.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keys = g.GenerateKeyPair();
privateKey = keys.Private;
publicKey = keys.Public;
我还可以加密/解密
字节[]

问题
如何将密钥保存到文件中?保存后如何将其导入?

您是否尝试将和应用程序配置文件添加到项目中? 它允许您以键/值格式(XML)轻松保存运行时设置


然后,您可以在应用程序中创建configurationmanager对象以保存和读取密钥;在您的例子中,哪一个是API密钥这是我的代码项目的一部分:(谁将公钥保存到文件中)

var certif_dsa=新X509Certificate2(Certificate_dsa,“密码”)

var pubkey_dsa=certif_dsa.GetPublicKeyString()

StreamWriter fluxInfos_dsa=null;//公共图书馆的工作人员

                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

不,我没有这样做,因为我无法如此轻松地获取密钥值。经过一些研究,我发现我需要序列化它们以提取键value.gotcha。对不起,我误解了你的意思。我以为您正在寻找一种方法来保存私钥和公钥的各个值。您是正确的,序列化/反序列化是保存整个对象的最佳选择。这可能是一个很好的起点: