生成公私密钥对,并显示他们在asp.net文本框 [英] Generate public-private key pair and show them in textbox in asp.net
问题描述
任何机构可以解释RSAParameters参数
我曾见过像P,D,E,Q参数,...
我需要从它的私钥和公钥
any body can explain the parameters of RSAParameters i had seen the parameters like p,d,e,q,... i need the private key and public key from it
我得到的链接
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters%28v=vs.90%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters%28v=vs.90%29.aspx[^]
我现在用的样品code作为这样的
可任何人都可以说,这是正确与否
样品code:
i am using the sample code as like this can anybody can say it was right or not sample code:
//Generate a public/private key pair.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//Save the public key information to an RSAParameters structure.
RSAParameters RSAKeyInfo = RSA.ExportParameters(true);
//public key
TextBox5.Text = Convert.ToBase64String(RSAKeyInfo.Exponent);
// private key
TextBox6.Text = Convert.ToBase64String(RSAKeyInfo.D);
他们得到为,该
公钥是{E,N}其中n =(P * Q)的结果
私钥{D,N}其中n =(P * Q)的结果。
they had give as that the public key is {e,n} where n = result of the (P*Q) Private key is {d, n} where n = result of the (P*Q)
在那里我有做的是正确的事情,或没有在样品code的公钥和私钥
where i had done is the correct thing or not in the sample code for the public and private keys
很多
推荐答案
使用BouncyCastle的API
Using the BouncyCastle API
和以下类似的东西;
public AsymmetricCipherKeyPair GenerateKeys(int keySizeInBits)
{
RsaKeyPairGenerator r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(),
keySizeInBits));
AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
return keys;
}
您可以访问的对象,将有一个公开
和私人
属性与正确格式化字符串。
You can access an object that will have a .Public
and .Private
property with the correctly formatted strings.
我有一个类似的问题而回,这是我能找到的最好的解决方案。我没有确切的code出手,而是将发布它,当我进入办公室,如果需要,但上面的应该工作。
I had a similiar problem a while back and this was the best solution that I could find. I do not have the exact code to hand, but will post it when I get into the office if required, but the above should work.
更新,其中code
这是code我用来生成公钥/私钥。
This is the code I used to generate public/private keys.
public static AsymmetricCipherKeyPair GenerateKeys(int keySizeInBits)
{
var r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(),keySizeInBits));
var keys = r.GenerateKeyPair();
return keys;
}
static void Main(string[] args)
{
var keys = GenerateKeys(2048);
var publicKey = keys.Public.ToString();
var textWriter = new StreamWriter("private.key");
var pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Private);
pemWriter.Writer.Flush();
textWriter.Close();
textWriter = new StreamWriter("public.key");
pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Public);
pemWriter.Writer.Flush();
textWriter.Close();
Console.ReadKey();
}
这篇关于生成公私密钥对,并显示他们在asp.net文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!