Generate public-private key pair and show them in textbox in asp.net
Asked Answered
P

1

5

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

Photo answered 30/6, 2011 at 6:59 Comment(2)
Remember that not all byte arrays can be shown as valid strings and that they don't have to survive a round trip from byte[] to string to byte[]Jungle
The Exponent property should return an exponent like "AQAB" instead of a public key...it would make more sense in your case to have tried the Modulus propertyConsolatory
L
11

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


}
Labourer answered 30/6, 2011 at 7:4 Comment(1)
when I view var "publicKey" I see "Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters". Is this expected?Importation

© 2022 - 2024 — McMap. All rights reserved.