首先,先简单介绍一下MD5
MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来, 经md2、md3和md4发展而来。
MD5具有很好的安全性(因为它具有不可逆的特征,加过密的密文经过解密后和加密前的东东相同的可能性极小)
引用 using System.Security.Cryptography; using System.Text;
具体代码如下(写在按钮的Click事件里): byte[] result = Encoding.Default.GetBytes(this.tbPass.Text.Trim()); //tbPass为输入密码的文本框 MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); this.tbMd5pass.Text = BitConverter.ToString(output).Replace("-",""); //tbMd5pass为输出加密文本的文本框
方法二
C# md5加密(上) string a; //加密前数据 string b; //加密后数据 b=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a,"MD5")
using System; using System.Security.Cryptography;
方法2
public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider(); byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString); byte[] targetData = md5.ComputeHash(fromData); string byte2String = null;
for (int i=0; i<targetData.Length; i++) { byte2String += targetData[i].ToString("x");
}
return byte2String;
}
using System.Security.Cryptography;
/// <summary>
/// 给一个字符串进行MD5加密
/// </summary>
/// <param >待加密字符串</param>
/// <returns>加密后的字符串</returns> public static string MD5Encrypt(string strText) {
MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText)); return System.Text.Encoding.Default.GetString(result);
}
C# MD5加密 using System.Security.Cryptography;
private void btnOK_Click(object sender, System.EventArgs e)
{
string strConn = "server=192.168.0.51;database=chengheng;User id=sa; pwd=123";
if(texName.Text.Trim()=="")
{
this.RegisterStartupScript("sf","<script language=‘javascript‘>alert(‘用户名不能为空‘);document.all(‘texName‘).focus()</script>"); return;
}
else if(texPassword.Text.Trim()=="")
{
this.RegisterStartupScript("sfs","<script language=‘javascript‘>alert(‘密码不能为空‘);
document.all(‘texPassword‘).focus()</script>");
return;
} else
{
//将获取的密码加密与数据库中加了密的密码相比较 byte[] by = md5.ComputeHash(utf.GetBytes(texPassword.Text.Trim()));
string resultPass = System.Text.UTF8Encoding.Unicode.GetString(by); conn.ConnectionString=strConn;
SqlCommand comm = new SqlCommand();
string name = texName.Text.Trim().ToString();
comm.CommandText="select Ruser_pwd,Ruser_nm from Ruser where Accountno = @name";
comm.Parameters.Add("@name",SqlDbType.NVarChar,40);
comm.Parameters["@name"].Value=name;
try{
conn.Open();
comm.Connection=conn;
SqlDataReader dr=comm.ExecuteReader();
if(dr.Read())
{
//用户存在,对密码进行检查
if(dr.GetValue(0).Equals(resultPass))
{
string user_name=dr.GetValue(1).ToString();
string user_Accountno=texName.Text.Trim();
Session["logon_name"]=user_name;
Session["logon_Accountno"]=user_Accountno;
//登录成功,进行页面导向
}else
{
this.RegisterStartupScript("wp","<script language=‘javascript‘>alert(‘密码错误,请检查。‘)</script>");
}
}
else
{
this.RegisterStartupScript("nu","<script language=javascript>alert(‘用户名不存在,请检查。‘)</script>");
}
}
catch(Exception exec)
{
this.RegisterStartupScript("wc","<script language=javascript>alert(‘网络连接有异,请稍后重试。‘)</script>");
}
finally
{
conn.Close();
}
}
}
方法三 C# MD5加密
C#开发笔记 一、C# MD5-16位加密实例,32位加密实例(两种方法)
环境:vs.net2005/sql server2000/xp测试通过 1.MD5 16位加密实例 using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography;
namespace md5
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(UserMd5("8"));
Console.WriteLine(GetMd5Str("8"));
}
/**//// <summary>
/// MD5 16位加密 加密后密码为大写
/// </summary>
/// <param></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
/**//// <summary>
/// MD5 16位加密 加密后密码为小写
/// </summary>
/// <param></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
t2 = t2.ToLower();
return t2;
}
/**//// <summary>
/// MD5 32位加密
/// </summary>
/// <param></param>
/// <returns></returns>
static string UserMd5(string str)
{
string cl = str;
string pwd = "";
MD5 md5 = MD5.Create();
//实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符