Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

RSACryptoServiceProvider SSL

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

        }
    }
}

Viewing all articles
Browse latest Browse all 8156

Trending Articles