分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

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

发布时间:2023-09-06 01:54责任编辑:白小东关键词:加密解密

加密类

#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过时处理

原文地址:https://www.cnblogs.com/hnsongbiao/p/9054913.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved