1. 程式人生 > >MD5加密解密類(asp.net)&使用MD5過時處理

MD5加密解密類(asp.net)&使用MD5過時處理

bytes div height crypt then .text ble tco doc

加密類

#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>


MD5加密解密類(asp.net)&使用MD5過時處理