Create a pair of public and private keys and show them in a text box in asp.net.

any body can explain the parameters of RSAP parameters; I saw parameters like p, d, e, q, ... I need a secret key and a public key from it

I got a link

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

I am using the sample code in a way that someone can say that this is correct 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 gave that the public key is {e, n}, where n = result (P * Q) The private key is {d, n}, where n = result (P * Q)

where I did is the right thing or not in the sample code for public and private keys

thanks alot

+5
source share
1 answer

Using the BouncyCastle API

http://www.bouncycastle.org/

and something similar 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 properties. Public and. Private with correctly formatted strings.

Some time ago I had a similar problem and this was the best solution I could find. I do not have the exact code, but I will publish it when I come to the office, if necessary, but the above should work.

Updated with code

This is the code that 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(); } 
+10
source

All Articles