MD5加密算法
用MD5进行密码的加密。
写一个类用来存放MD5算法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace HRMSys.DAL
{
public class MD5
{
public static string GetMD5(string sDataIn)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] bytValue, bytHash;
bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
bytHash = md5.ComputeHash(bytValue);
md5.Clear();
string sTemp = "";
for (int i = 0; i < bytHash.Length; i++)
{
sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
}
return sTemp.ToLower();
}
}
}
2.然后在主方法中调用这个MD5方法就可以了。
private void button1_Click(object sender, RoutedEventArgs e)
{
string s = GetMD5("123456");
MessageBox.Show(s);
}
3.这样MessageBox.Show();出来的一串字符就是加密过后的结果。你可以将其写入到数据库中去。


浙公网安备 33010602011771号