Wednesday, May 17, 2006

CREAR VALOR HASH



La funcion Hash, se utiliza en los metodos de encriptacion, lo que hace es hacer un resumen del mensaje utilizado, por ejemplo en el proceso de firma digital se aplica la funcion al documento o mensaje a enviar, ya que los algoritmos de cifrado asimetricos son lentos y no soportan grandes cantidades de bits, pero por ahora eso es otra historia.
La imagen (aunque no muy buena), muestra un texto original, que es donde se ingresa un texto, en las otras pantallas muestra las distintas formas de ver el valor hash generado...
Por ahora nada mas...

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 visualSha1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
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
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
// textBox1
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(272, 72);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
// button1
this.button1.Location = new System.Drawing.Point(32, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(232, 32);
this.button1.TabIndex = 1;
this.button1.Text = "Crear Valor Hash SHA1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// textBox2
this.textBox2.BackColor = System.Drawing.SystemColors.InfoText;
this.textBox2.ForeColor = System.Drawing.SystemColors.HighlightText;
this.textBox2.Location = new System.Drawing.Point(16, 176);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(272, 88);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
// label1
this.label1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 24);
this.label1.TabIndex = 3;
this.label1.Text = "Texto Original";
// label2
this.label2.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(16, 152);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(224, 24);
this.label2.TabIndex = 4;
this.label2.Text = "Texto Cifrado normal";
// textBox3
this.textBox3.BackColor = System.Drawing.SystemColors.InfoText;
this.textBox3.ForeColor = System.Drawing.SystemColors.HighlightText;
this.textBox3.Location = new System.Drawing.Point(304, 32);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.ReadOnly = true;
this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox3.Size = new System.Drawing.Size(288, 104);
this.textBox3.TabIndex = 5;
this.textBox3.Text = "";
// textBox4
this.textBox4.BackColor = System.Drawing.SystemColors.InfoText;
this.textBox4.ForeColor = System.Drawing.SystemColors.HighlightText;
this.textBox4.Location = new System.Drawing.Point(304, 176);
this.textBox4.Multiline = true;
this.textBox4.Name = "textBox4";
this.textBox4.ReadOnly = true;
this.textBox4.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox4.Size = new System.Drawing.Size(288, 88);
this.textBox4.TabIndex = 6;
this.textBox4.Text = "";
// label3
this.label3.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.Location = new System.Drawing.Point(304, 8);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(264, 24);
this.label3.TabIndex = 7;
this.label3.Text = "Texto Cifrado hexadecimal";
// label4
this.label4.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label4.Location = new System.Drawing.Point(304, 152);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(216, 24);
this.label4.TabIndex = 8;
this.label4.Text = "Texto Cifrado decimal";
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(8, 19);
this.ClientSize = new System.Drawing.Size(600, 270);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
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 != "")
{
HashAlgorithm cod = new SHA1CryptoServiceProvider();
byte[] texto = Encoding.UTF8.GetBytes(textBox1.Text);
byte[] valor = cod.ComputeHash(texto);
cod.Clear();
textBox2.Text = Convert.ToBase64String(valor);
textBox3.Text = BitConverter.ToString(valor);
string suma;
foreach(byte b in valor)
{
suma = Convert.ToString(b);
textBox4.Text += suma + " ";
}
FileStream fs1 = new FileStream("original.arp",FileMode.Create,FileAccess.Write);
BinaryWriter sw = new BinaryWriter(fs1);
sw.Write(Encoding.ASCII.GetString(texto));
sw.Flush();
sw.Close();
fs1.Close();
fs1 = new FileStream("cifrado.arp",FileMode.Create,FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs1);
bw.Write(Convert.ToBase64String(valor));
bw.Close();
}
}
}
}

0 Comentarios :

Post a Comment

<< Home