Noticias Weblogs Foros Wiki Código
Sponsors:

Meta-Info

¿Que es?

Planeta Código es un agregador de weblogs sobre programación y desarrollo en castellano. Si eres lector te permite seguirlos de modo cómodo en esta misma página o mediante el fichero de subscripción.

rss subscripción

Sponsors

PlanetaCódigo en inglés

Puedes utilizar las siguientes imagenes para enlazar PlanetaCodigo:
planetacodigo

planetacodigo

Si tienes un weblog de programación y quieres ser añadido aquí, envíame un email solicitándolo.

Idea: Juanjo Navarro

Diseño: Albin

USANDO C# (C SHARP)

CREAR CLAVES PRIVADAS Y PUBLICAS

Mayo 30th, 2006 - [Enlace local]


Hola a todos, veremos a continuacion como crear claves publicas y privadas para el cifrado y la firma digital.
Diremos primero que ocupamos la clase del algoritmo RSA, estas claves son ocupadas por los distintos usuarios que actuan en el intercambio de mensajes o documentos, ocupamos la clave publica para cifrar informacion, y ocupamos la clave privada para adjuntar una firma a los distintos documentos, pueden adjuntarse varias firmas si es necesario.

Esto se realiza a traves de la clase:

Clave Privada:

Clave Publica:

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

» Leer más, comentarios, etc...