MD5加密解密类(asp.net)&使用MD5过时处理

加密类

#region ========加密========
/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
    return Encrypt(Text, "cong");
}
/// <summary> 
/// 加密数据 
/// </summary> 
/// <param name="Text"></param> 
/// <param name="sKey"></param> 
/// <returns></returns> 
public static string Encrypt(string Text, string sKey)
{
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    byte[] inputByteArray;
    inputByteArray = Encoding.Default.GetBytes(Text);
    des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    StringBuilder ret = new StringBuilder();
    foreach (byte b in ms.ToArray())
    {
        ret.AppendFormat("{0:X2}", b);
    }
    return ret.ToString();
}

#endregion


#region ========解密========


/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
    return Decrypt(Text, "cong");
}
/// <summary> 
/// 解密数据 
/// </summary> 
/// <param name="Text"></param> 
/// <param name="sKey"></param> 
/// <returns></returns> 
public static string Decrypt(string Text, string sKey)
{
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    int len;
    len = Text.Length / 2;
    byte[] inputByteArray = new byte[len];
    int x, i;
    for (x = 0; x < len; x++)
    {
        i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
        inputByteArray[x] = (byte)i;
    }
    des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    return Encoding.Default.GetString(ms.ToArray());
}

#endregion

在.net 4.5版本下,使用System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile进行MD5加密时,会出现已过时,如下图:

MD5加密过时显示.png

我们可以用下面的方法替代之:

命名空间:System.Web.Security程序集:System.Web(在 system.web.dll 中)

/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private static string Md5Hash(string input)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
    sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
 


 
 

本来我也以为System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile中的MD5和常用的一样

可今天一试,结果有很大不同,
比如test,HashPasswordForStoringInConfigFile编码成
C8059E2EC7419F590E79D7F1B774BFE6
而应该是098f6bcd4621d373cade4e832627b4f6


而且不同的机器不同的结果,有些结果正确
一看MSDN的解释,原来是
Given a password and a string identifying the hash type, this routine produces a hash password suitable for storing in a configuration file.

为了和以前的代码兼容和平台兼容,只好自己重新写了MD5的算法,利用System.Security.Cryptography.MD5CryptoServiceProvider
代码如下,大家执行一下就知道了,我就不多说了。


   <script language="C#" runat="server">
   string qswhMD5(string str){
     /************qiushuiwuhen(2002-9-27)***************/
     byte[] b=System.Text.Encoding.Default.GetBytes(str);
     b=new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
     string ret="";
     for(int i=0;i<b.Length;i++)
      ret+=b[i].ToString("x").PadLeft(2,'0');
     return ret;
   }
   public void encryptString(Object sender, EventArgs e)
   {
     myMD5.Text=qswhMD5(txtClear.Text);
     MD5.Text =System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtClear.Text, "MD5") ;
   }
   </script>
   <body onload=document.all.txtClear.select();>
   <form runat="server">
    明文:<asp:Textbox id="txtClear" runat="server" />
    <asp:Button runat="server" text="Md5摘要" onClick="encryptString" ID="Button1" />
    <br/>通常用的 MD5:
    <br/><asp:label id="myMD5" runat="server" /> <br/>
    <br/>HashPasswordForStoringInConfigFile中的 MD5:
    <br/><asp:label id="MD5" runat="server" />
   </form>


猜你喜欢

转载自www.cnblogs.com/hnsongbiao/p/9054913.html