基于C#对用户密码使用MD5加密与解密
C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式。本文总结了通用的算法并结合了自己的一点小经验,分享给大家。
一.使用16位、32位、64位MD5方法对用户名加密
1)16位的MD5加密
///<summary>
///16位MD5加密
///</summary>
///<paramname="password"></param>
///<returns></returns>
publicstaticstringMD5Encrypt16(stringpassword)
{
varmd5=newMD5CryptoServiceProvider();
stringt2=BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)),4,8);
t2=t2.Replace("-","");
returnt2;
}
2)32位的MD5加密
///<summary>
///32位MD5加密
///</summary>
///<paramname="password"></param>
///<returns></returns>
publicstaticstringMD5Encrypt32(stringpassword)
{
stringcl=password;
stringpwd="";
MD5md5=MD5.Create();//实例化一个md5对像
//加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[]s=md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
//通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for(inti=0;i<s.Length;i++)
{
//将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd=pwd+s[i].ToString("X");
}
returnpwd;
}
3)64位的MD5加密
publicstaticstringMD5Encrypt64(stringpassword)
{
stringcl=password;
//stringpwd="";
MD5md5=MD5.Create();//实例化一个md5对像
//加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[]s=md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
returnConvert.ToBase64String(s);
}
4)使用MD5为用户密码加密
///<summary>
///加密用户密码
///</summary>
///<paramname="password">密码</param>
///<paramname="codeLength">加密位数</param>
///<returns>加密密码</returns>
publicstaticstringmd5(stringpassword,intcodeLength)
{
if(!string.IsNullOrEmpty(password))
{
//16位MD5加密(取32位加密的9~25字符)
if(codeLength==16)
{
returnSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5").ToLower().Substring(8,16);
}
//32位加密
if(codeLength==32)
{
returnSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5").ToLower();
}
}
returnstring.Empty;
}
由于MD5是不可逆的,所以加密之后就无法解密,取用户名和密码时候,需要再加密一边用户输入的数据与数据库中已加密的数据进行比对。如果比对结果一致,则可以判定登陆成功!代码如下所示:
///<summary>
///登陆
///</summary>
publicModel.UserInfoUserLogOn(stringUSERID,stringpwd,outstringstatusCode)
{
//假设已经通过用户ID获取到UserInfo的Model对象
Model.UserInfomodel=GetModel(USERID);
if(model!=null)
{
if(model.PASSWORD==MD5Encrypt64(pwd))
{
statusCode="登陆成功";
}
else{
statusCode=“密码错误”;
}
}
else
{
statusCode="用户不存在!";
model=null;
}
returnmodel;
}
5)通过DESCryptoServiceProvider对象对字符串进行加密解密
///<summary>
///DES数据加密
///</summary>
///<paramname="targetValue">目标值</param>
///<paramname="key">密钥</param>
///<returns>加密值</returns>
publicstaticstringEncrypt(stringtargetValue,stringkey)
{
if(string.IsNullOrEmpty(targetValue))
{
returnstring.Empty;
}
varreturnValue=newStringBuilder();
vardes=newDESCryptoServiceProvider();
byte[]inputByteArray=Encoding.Default.GetBytes(targetValue);
//通过两次哈希密码设置对称算法的初始化向量
des.Key=Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key,"md5").
Substring(0,8),"sha1").Substring(0,8));
//通过两次哈希密码设置算法的机密密钥
des.IV=Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key,"md5")
.Substring(0,8),"md5").Substring(0,8));
varms=newMemoryStream();
varcs=newCryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
foreach(bytebinms.ToArray())
{
returnValue.AppendFormat("{0:X2}",b);
}
returnreturnValue.ToString();
}
此种算法可以通过加密密钥进行解密,解密方法如下:
///<summary>
///DES数据解密
///</summary>
///<paramname="targetValue"></param>
///<paramname="key"></param>
///<returns></returns>
publicstaticstringDecrypt(stringtargetValue,stringkey)
{
if(string.IsNullOrEmpty(targetValue))
{
returnstring.Empty;
}
//定义DES加密对象
vardes=newDESCryptoServiceProvider();
intlen=targetValue.Length/2;
varinputByteArray=newbyte[len];
intx,i;
for(x=0;x<len;x++)
{
i=Convert.ToInt32(targetValue.Substring(x*2,2),16);
inputByteArray[x]=(byte)i;
}
//通过两次哈希密码设置对称算法的初始化向量
des.Key=Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key,"md5").
Substring(0,8),"sha1").Substring(0,8));
//通过两次哈希密码设置算法的机密密钥
des.IV=Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
(FormsAuthentication.HashPasswordForStoringInConfigFile(key,"md5")
.Substring(0,8),"md5").Substring(0,8));
//定义内存流
varms=newMemoryStream();
//定义加密流
varcs=newCryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
returnEncoding.Default.GetString(ms.ToArray());
}
以上内容是基于C#对用户密码使用MD5加密与解密的全部叙述,希望大家喜欢。