生成公钥-私钥对并将其显示在 asp.net 的文本框中

发布于 2024-11-17 14:49:38 字数 832 浏览 3 评论 0原文

任何机构都可以解释 RSAParameters 的参数 我见过像 p,d,e,q,... 这样的参数 我需要其中的私钥和公钥,

我得到了链接

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters%28v=vs.90%29.aspx[^]

我正在使用示例代码,如下所示 谁能说这是对还是错 示例代码:

  //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) 的结果在公钥和私钥的示例代码中是否正确,

非常感谢

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

i got the link

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters%28v=vs.90%29.aspx[^]

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);

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)

where i had done is the correct thing or not in the sample code for the public and private keys

thanks alot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-11-24 14:49:38

使用 BouncyCastle API

http://www.bouncycastle.org/

以及类似以下内容:

public AsymmetricCipherKeyPair GenerateKeys(int keySizeInBits)
{
  RsaKeyPairGenerator r = new RsaKeyPairGenerator();
  r.Init(new KeyGenerationParameters(new SecureRandom(),
    keySizeInBits));
  AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
  return keys;
}

您可以访问具有 .Public 和 .Private 属性以及格式正确的字符串的对象。

不久前我遇到了类似的问题,这是我能找到的最佳解决方案。我手头没有确切的代码,但如果需要,我会在进入办公室时发布它,但上述应该可行。

使用代码更新

这是我用来生成公钥/私钥的代码。

  using Org.BouncyCastle.Crypto;
  using Org.BouncyCastle.Crypto.Generators;
  using Org.BouncyCastle.Security;

  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();


}

Using the BouncyCastle API

http://www.bouncycastle.org/

and something similiar to the following:

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.

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.

Updated with Code

This is the code I used to generate public/private keys.

  using Org.BouncyCastle.Crypto;
  using Org.BouncyCastle.Crypto.Generators;
  using Org.BouncyCastle.Security;

  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();


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文