CREAR CLAVES PRIVADAS Y PUBLICAS

Esto se realiza a traves de la clase:
- RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
Clave Privada:
- RSA.ToXmlString(true);
- RSA.ExportParameters(true);
Clave Publica:
- RSA.ToXmlString(false);
- RSA.ExportParameters(true);
El codigo es el siguiente, los datos se guardan en archivos .xml:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CrearClavesPublicasPrivadas
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Código generado por el Diseñador de Windows Forms
///
/// Método necesario para admitir el Diseñador. No se puede modificar
/// el contenido del método con el editor de código.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 48);
this.button1.TabIndex = 0;
this.button1.Text = "Generar claves";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.textBox1.Location = new System.Drawing.Point(120, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(144, 26);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(120, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(152, 24);
this.label1.TabIndex = 2;
this.label1.Text = "Nombre clave";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 94);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text != "")
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(4096);
string PCB = RSA.ToXmlString(true);
string CPubB = RSA.ToXmlString(false);
string nombre = textBox1.Text;
nombre += "PCB.xml";
StreamWriter PCAwriter = new StreamWriter(nombre);
PCAwriter.Write(PCB);
PCAwriter.Close();
string nombre2 = textBox1.Text;
nombre2 += "CPubB.xml";
StreamWriter CPubAwriter = new StreamWriter(nombre2);
CPubAwriter.Write(CPubB);
CPubAwriter.Close();
MessageBox.Show("LAS CLAVES FUERON CREADAS CON EXITO","",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
else
{
MessageBox.Show("DEBE LLENAR EL CAMPO NOMBRE CLAVE.", "",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}


2 Comentarios :
la clase RSACrypto... viene con C# o te la descargaste de otro lado??' necesito ver el codigo fuente para saber como han implementado el algoritmo RSA... podrias ayudarme en algo con esto???
Hola, se ve rebueno el ejemplesillo.
Te expongo mi problema:
1. me compre un Token iKey 2032 de SafeNet y un certificado, con 2 llaves una publica y otra privada.
2. ahora me gustaria poder usar esas llaves para firmar mis documentos.
3. Segun tu ejemplo, se crea la llave publica y privada con las rutinas de Biblioteca de Crypto... pero (MI DUDA) ¿cómo le hago para usar mis llaves, para firmar con ellas un xml o doc por ejemplo, las que tengo en el token o mejor dicho las que ve el IExplorer?
Te agradezco desde ya cualquier auda que me puedas dar.
Atte.
Pablo
Post a Comment
<< Home