Visual Stdio C# 调用MD5

新建windows窗体应用程序

窗口设计成下图样式

123

From1.cs程序代码(非设计代码)

using System.Security.Cryptography;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using CACenter20.DAL;

using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string pw;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pw = textBox2.Text;
          //  Md5 md5 = new Md5();
          //  md5.StrToMD5(pw);
            DBOperation dbo = new DBOperation();
            string MD5Pwd =  dbo.Encrypt(pw);
            textBox3.Text = MD5Pwd;
            MessageBox.Show(MD5Pwd);

        }

    }
}

 

在此项目中新建一个类Class1.cs

代码如下:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
using System.IO;
using CACenter20.DAL;
namespace CACenter20.DAL
{
    /// <summary>
    /// Summary description for DBOperation.
    /// </summary>
    public class DBOperation
    {
        public string Encrypt(string strPwd)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);//将字符编码为一个字节序列 
        byte[] md5data = md5.ComputeHash(data);//计算data字节数组的哈希值 
        md5.Clear();
        string str = "";
        for (int i = 0; i < md5data.Length - 1; i++)
        {
            str += md5data[i].ToString("x").PadLeft(2, '0');
        }
        return str;
     } 
    }
}

 

 

好了,以上就是所有代码。

posted @ 2012-03-23 12:30  engagedin  阅读(1564)  评论(0编辑  收藏  举报