using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security;
using System.Security.Cryptography;
using System.Xml ;
using System.Text ;
namespace Security


{

/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form

{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button btnEncr;
private System.Windows.Forms.Button btnDecr;
private System.Windows.Forms.Button btnCancal;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.GroupBox G2;
private System.Windows.Forms.GroupBox G1;
private System.Windows.Forms.TextBox txtDecrStr;
private System.Windows.Forms.TextBox txtEncrKey;
private System.Windows.Forms.TextBox txtDecrKey;
private System.Windows.Forms.TextBox txtEncrStr;
private System.Windows.Forms.TextBox txtEncr;
private System.Windows.Forms.TextBox txtDecr;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()

{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if (components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtEncrStr = new System.Windows.Forms.TextBox();
this.txtEncr = new System.Windows.Forms.TextBox();
this.txtDecr = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.btnEncr = new System.Windows.Forms.Button();
this.btnDecr = new System.Windows.Forms.Button();
this.btnCancal = new System.Windows.Forms.Button();
this.txtEncrKey = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.txtDecrKey = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.txtDecrStr = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.G2 = new System.Windows.Forms.GroupBox();
this.G1 = new System.Windows.Forms.GroupBox();
this.G2.SuspendLayout();
this.G1.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 0;
this.label1.Text = "源字符串:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 1;
this.label2.Text = "加密字符串:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 16);
this.label3.TabIndex = 2;
this.label3.Text = "解密字符串:";
//
// txtEncrStr
//
this.txtEncrStr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtEncrStr.Location = new System.Drawing.Point(88, 16);
this.txtEncrStr.Name = "txtEncrStr";
this.txtEncrStr.Size = new System.Drawing.Size(456, 21);
this.txtEncrStr.TabIndex = 3;
this.txtEncrStr.Text = "";
this.txtEncrStr.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtEncrStr_KeyPress);
this.txtEncrStr.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtEncrStr_KeyDown);
//
// txtEncr
//
this.txtEncr.BackColor = System.Drawing.Color.Linen;
this.txtEncr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtEncr.Location = new System.Drawing.Point(88, 96);
this.txtEncr.Name = "txtEncr";
this.txtEncr.ReadOnly = true;
this.txtEncr.Size = new System.Drawing.Size(456, 21);
this.txtEncr.TabIndex = 4;
this.txtEncr.Text = "";
//
// txtDecr
//
this.txtDecr.BackColor = System.Drawing.Color.Linen;
this.txtDecr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtDecr.Location = new System.Drawing.Point(88, 96);
this.txtDecr.Name = "txtDecr";
this.txtDecr.ReadOnly = true;
this.txtDecr.Size = new System.Drawing.Size(456, 21);
this.txtDecr.TabIndex = 5;
this.txtDecr.Text = "";
//
// label4
//
this.label4.Font = new System.Drawing.Font("隶书", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label4.Location = new System.Drawing.Point(224, 8);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 23);
this.label4.TabIndex = 6;
this.label4.Text = "加密解密";
//
// btnEncr
//
this.btnEncr.BackColor = System.Drawing.Color.Gainsboro;
this.btnEncr.Location = new System.Drawing.Point(88, 64);
this.btnEncr.Name = "btnEncr";
this.btnEncr.TabIndex = 7;
this.btnEncr.Text = "加 密";
this.btnEncr.Click += new System.EventHandler(this.btnEncr_Click);
//
// btnDecr
//
this.btnDecr.BackColor = System.Drawing.Color.Gainsboro;
this.btnDecr.Location = new System.Drawing.Point(88, 64);
this.btnDecr.Name = "btnDecr";
this.btnDecr.TabIndex = 8;
this.btnDecr.Text = "解 密";
this.btnDecr.Click += new System.EventHandler(this.btnDecr_Click);
//
// btnCancal
//
this.btnCancal.BackColor = System.Drawing.Color.Gainsboro;
this.btnCancal.Location = new System.Drawing.Point(272, 312);
this.btnCancal.Name = "btnCancal";
this.btnCancal.TabIndex = 9;
this.btnCancal.Text = "取 消";
this.btnCancal.Click += new System.EventHandler(this.btnCancal_Click);
//
// txtEncrKey
//
this.txtEncrKey.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtEncrKey.Location = new System.Drawing.Point(88, 40);
this.txtEncrKey.Name = "txtEncrKey";
this.txtEncrKey.PasswordChar = '*';
this.txtEncrKey.Size = new System.Drawing.Size(456, 21);
this.txtEncrKey.TabIndex = 11;
this.txtEncrKey.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(16, 39);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(72, 23);
this.label5.TabIndex = 10;
this.label5.Text = "密 钥:";
//
// txtDecrKey
//
this.txtDecrKey.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtDecrKey.Location = new System.Drawing.Point(88, 40);
this.txtDecrKey.Name = "txtDecrKey";
this.txtDecrKey.PasswordChar = '*';
this.txtDecrKey.Size = new System.Drawing.Size(456, 21);
this.txtDecrKey.TabIndex = 15;
this.txtDecrKey.Text = "";
//
// label6
//
this.label6.Location = new System.Drawing.Point(16, 40);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(72, 16);
this.label6.TabIndex = 14;
this.label6.Text = "密 钥:";
//
// txtDecrStr
//
this.txtDecrStr.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtDecrStr.Location = new System.Drawing.Point(88, 16);
this.txtDecrStr.Name = "txtDecrStr";
this.txtDecrStr.Size = new System.Drawing.Size(456, 21);
this.txtDecrStr.TabIndex = 13;
this.txtDecrStr.Text = "";
//
// label7
//
this.label7.Location = new System.Drawing.Point(16, 16);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(72, 16);
this.label7.TabIndex = 12;
this.label7.Text = "源字符串:";
//
// G2
//
this.G2.Controls.Add(this.txtDecr);
this.G2.Controls.Add(this.label3);
this.G2.Controls.Add(this.label7);
this.G2.Controls.Add(this.txtDecrStr);
this.G2.Controls.Add(this.txtDecrKey);
this.G2.Controls.Add(this.label6);
this.G2.Controls.Add(this.btnDecr);
this.G2.Location = new System.Drawing.Point(8, 176);
this.G2.Name = "G2";
this.G2.Size = new System.Drawing.Size(560, 128);
this.G2.TabIndex = 16;
this.G2.TabStop = false;
this.G2.Text = "解密";
//
// G1
//
this.G1.Controls.Add(this.txtEncrKey);
this.G1.Controls.Add(this.txtEncrStr);
this.G1.Controls.Add(this.label1);
this.G1.Controls.Add(this.label2);
this.G1.Controls.Add(this.label5);
this.G1.Controls.Add(this.btnEncr);
this.G1.Controls.Add(this.txtEncr);
this.G1.Location = new System.Drawing.Point(8, 40);
this.G1.Name = "G1";
this.G1.Size = new System.Drawing.Size(560, 128);
this.G1.TabIndex = 17;
this.G1.TabStop = false;
this.G1.Text = "加密";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(576, 343);
this.Controls.Add(this.G2);
this.Controls.Add(this.btnCancal);
this.Controls.Add(this.label4);
this.Controls.Add(this.G1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "加密解密";
this.Load += new System.EventHandler(this.Form1_Load);
this.G2.ResumeLayout(false);
this.G1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion


/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()

{
Application.Run(new Form1());
}

private void btnEncr_Click(object sender, System.EventArgs e)

{
try

{
TripleDESCryptoServiceProvider MyDes = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MyMd5 = new MD5CryptoServiceProvider();
MyDes.Key=MyMd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(txtEncrKey.Text.ToString().Trim()));
MyDes.Mode=CipherMode.ECB;
ICryptoTransform DesEncrypt= MyDes.CreateEncryptor();
byte[] buffer=System.Text.Encoding.GetEncoding("GB2312").GetBytes(txtEncrStr.Text.ToString().Trim());
txtEncr.Text =Convert.ToBase64String(DesEncrypt.TransformFinalBlock(buffer,0,buffer.Length ));
}
catch(Exception ex)

{
MessageBox.Show(ex.Message,"错误!");
}
}

private void btnDecr_Click(object sender, System.EventArgs e)

{
try

{
TripleDESCryptoServiceProvider MyDes = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MyMd5 = new MD5CryptoServiceProvider();
MyDes.Key=MyMd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(txtDecrKey.Text.ToString().Trim()));
MyDes.Mode=CipherMode.ECB;
ICryptoTransform DesEncrypt= MyDes.CreateDecryptor();
byte[] buffer=Convert.FromBase64String(txtDecrStr.Text.ToString().Trim());
txtDecr.Text =System.Text.Encoding.GetEncoding("GB2312").GetString(DesEncrypt.TransformFinalBlock(buffer,0,buffer.Length ));
}
catch(Exception ex)

{
MessageBox.Show(ex.Message,"错误!");
}
}

private void btnCancal_Click(object sender, System.EventArgs e)

{
this.Close();
}

private void Form1_Load(object sender, System.EventArgs e)

{
}

private void txtEncrStr_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{
}
private void txtEncrStr_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

{
}

}
}
posted on
2006-07-13 10:25
快意人生
阅读(
175)
评论()
收藏
举报