none
RSACryptoServiceProvider SSL RRS feed

  • Question

  • Good afternoon, 

        I'm trying to create a small application in C # that allows me to generate public and private keys. 
        The idea is to have an identical solution a openssl, but I have not much experience in the use of ssL , I'm try to generate files,
    file.csr - CSR file with the request to send; 
    file.key - File generated with the private key.

    How do I create two files with the RSACryptoServiceProvide, The idea is to have something similar to the openssl code.

          /*openssl req -new -subj "/C=Country(country.tex)/ST=Province
                /L=City" 
                -newkey rsa:2048 -nodes -out file.csr -keyout file.key
                
       openssl req -text -noout -in file.csr

    openssl pkcs12 -export -in file.crt -inkey file.key -out file.pfx

    */




    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Security;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;


    namespace SSL
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {

                string C, ST, L, CN, O, OU, E;
                RSACryptoServiceProvider rsa;


                C = Country.Text.Trim();
                ST = Province.Text.Trim();
                L = City.Text.Trim();
                

                /*openssl req -new -subj "/C=Country(country.tex)/ST=Province
                /L=City/O=Organization" -newkey rsa:2048 -nodes -out file.csr -keyout file.key*/


                const int PROVIDER_RSA_FULL = 1;
                string CONTAINER_NAME = "/C=" + C.ToString() + "/ST=" + ST.ToString() +"/L="+L.ToString();
                CspParameters cspParams;
                cspParams = new CspParameters(PROVIDER_RSA_FULL);
                cspParams.KeyContainerName = CONTAINER_NAME;
                cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
                cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
                rsa = new RSACryptoServiceProvider(cspParams);

                KeyBit.Text = rsa.ToXmlString(true).ToString();
                System.IO.File.WriteAllText(@"C:\TestFolder\file.csr", KeyBit.Text);
                

            }
        }
    }
    • Moved by Kristin Xie Tuesday, August 12, 2014 5:50 AM
    Monday, August 11, 2014 9:27 AM

Answers

  • Hello,

    This class does not generate the two file, usually it would store both the public key and private key to XML file.

    Checking this blog:

    http://blogs.msdn.com/b/alejacma/archive/2008/10/23/how-to-generate-key-pairs-encrypt-and-decrypt-data-with-net-c.aspx

    It shows detail to create the key pair. After inputting the two file name as: "Public.xml" and "Private.xml”, it would generate content as:

    Public.xml:

    <RSAKeyValue><Modulus>qxLFdG74gLq7GmuFDUSpECFnxPKmBiWNemYboqB7Q4H5WUOG9JdXQfzFameuX8IX31kY9F3wHjl2S/ayhae0Ic0LbT3Kx8qKgnPfnSYWr3pHLP3Pwj6K/IF7+08crFp+TNs5jEwxsST3Sz2O46AAVVwWx9wnJ+v0Sb7eYJl7gXU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
    

    Private.xml:

    <RSAKeyValue><Modulus>qxLFdG74gLq7GmuFDUSpECFnxPKmBiWNemYboqB7Q4H5WUOG9JdXQfzFameuX8IX31kY9F3wHjl2S/ayhae0Ic0LbT3Kx8qKgnPfnSYWr3pHLP3Pwj6K/IF7+08crFp+TNs5jEwxsST3Sz2O46AAVVwWx9wnJ+v0Sb7eYJl7gXU=</Modulus><Exponent>AQAB</Exponent><P>yLYS9S/O692uxlwmncnoC1JQqI1K334qbvygi9gLatTBMmFP+EVlLc1djEfv/Ot3d6S50YYOG6k91aitZi3AYw==</P><Q>2jKql9GrTxIu+pIKdDSnhsTIm1tbKQo39hw0QeC1LVtDwsyRZBf5GJZ7ropfhvnGrcrbkr+saYs47xuUlSoiRw==</Q><DP>rn7lEndDDXrIpTmedRYBgD5LU46rZoyYta86oPnuSy223WcPEIi8g0qFJx2vvpK0Zbc3ubozb8b+tE+WmTvzgw==</DP><DQ>NrmgaaVvlsxTtgqszopqPfXa7oQSMA/dCz/1p1QpfK+4JgJSsM0Gb7FGYfdYkCzTNMF9IqgqIOqHJAlB0cuYfw==</DQ><InverseQ>EnKbLLZAFSG02PdnhR0byOZsNFpntqb2ikqL16LRKcgn7a1dLSb/vKK/Inm+V5uy8KkdtSunO0BqfGe4zyKhJA==</InverseQ><D>QrFTPDSk+Z6Sodan7MfHNywmxc+pLP1H3h9fh7OTynopyQR74O/9SxLtrbFU+Cz1XJPiGCZlHYteVHgNGfEK1n1Gv4Kmv++LpMjEONPOoeXFlHMlp8hglE8b0l+gIkHCfeIvy0irWFFAXzK07PLWFuMuJI0624kyoF/zfds93MM=</D></RSAKeyValue>
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by rmp1980 Friday, August 15, 2014 12:10 PM
    Wednesday, August 13, 2014 9:25 AM
    Moderator

All replies

  • Hi rmp1980,

    I moved your thread to .NET Framework  >.NET Framework Class Libraries forum for better support.

    Have a nice day!

    Kristin

    Tuesday, August 12, 2014 5:52 AM
  • Hello,

    This class does not generate the two file, usually it would store both the public key and private key to XML file.

    Checking this blog:

    http://blogs.msdn.com/b/alejacma/archive/2008/10/23/how-to-generate-key-pairs-encrypt-and-decrypt-data-with-net-c.aspx

    It shows detail to create the key pair. After inputting the two file name as: "Public.xml" and "Private.xml”, it would generate content as:

    Public.xml:

    <RSAKeyValue><Modulus>qxLFdG74gLq7GmuFDUSpECFnxPKmBiWNemYboqB7Q4H5WUOG9JdXQfzFameuX8IX31kY9F3wHjl2S/ayhae0Ic0LbT3Kx8qKgnPfnSYWr3pHLP3Pwj6K/IF7+08crFp+TNs5jEwxsST3Sz2O46AAVVwWx9wnJ+v0Sb7eYJl7gXU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
    

    Private.xml:

    <RSAKeyValue><Modulus>qxLFdG74gLq7GmuFDUSpECFnxPKmBiWNemYboqB7Q4H5WUOG9JdXQfzFameuX8IX31kY9F3wHjl2S/ayhae0Ic0LbT3Kx8qKgnPfnSYWr3pHLP3Pwj6K/IF7+08crFp+TNs5jEwxsST3Sz2O46AAVVwWx9wnJ+v0Sb7eYJl7gXU=</Modulus><Exponent>AQAB</Exponent><P>yLYS9S/O692uxlwmncnoC1JQqI1K334qbvygi9gLatTBMmFP+EVlLc1djEfv/Ot3d6S50YYOG6k91aitZi3AYw==</P><Q>2jKql9GrTxIu+pIKdDSnhsTIm1tbKQo39hw0QeC1LVtDwsyRZBf5GJZ7ropfhvnGrcrbkr+saYs47xuUlSoiRw==</Q><DP>rn7lEndDDXrIpTmedRYBgD5LU46rZoyYta86oPnuSy223WcPEIi8g0qFJx2vvpK0Zbc3ubozb8b+tE+WmTvzgw==</DP><DQ>NrmgaaVvlsxTtgqszopqPfXa7oQSMA/dCz/1p1QpfK+4JgJSsM0Gb7FGYfdYkCzTNMF9IqgqIOqHJAlB0cuYfw==</DQ><InverseQ>EnKbLLZAFSG02PdnhR0byOZsNFpntqb2ikqL16LRKcgn7a1dLSb/vKK/Inm+V5uy8KkdtSunO0BqfGe4zyKhJA==</InverseQ><D>QrFTPDSk+Z6Sodan7MfHNywmxc+pLP1H3h9fh7OTynopyQR74O/9SxLtrbFU+Cz1XJPiGCZlHYteVHgNGfEK1n1Gv4Kmv++LpMjEONPOoeXFlHMlp8hglE8b0l+gIkHCfeIvy0irWFFAXzK07PLWFuMuJI0624kyoF/zfds93MM=</D></RSAKeyValue>
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by rmp1980 Friday, August 15, 2014 12:10 PM
    Wednesday, August 13, 2014 9:25 AM
    Moderator