快意人生
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 窗体设计器生成的代码

        
/// <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)  评论(0)    收藏  举报