.NET生成常用16、32位MD5加密的两种方法
2013-08-27 16:15
411 查看
//MD5加密函数比较复杂,在.NET中我们不需要编写底层的算法。 //平台已经提供两个生成MD5加密的方法: //经过改动一点就可以生成如现在DVBBS等论坛中使用的MD5密码 //⑴:使用C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Security.dll public static string MD5(string Password,int Length) { if (Length!=16&&Length!=32) throw new System.ArgumentException("Length参数无效,只能为16位或32位"); System.Security.Cryptography.MD5CryptoServiceProvider MD5=new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] b= MD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Password)); System.Text.StringBuilder StrB=new System.Text.StringBuilder(); for(int i=0;i<b.Length;i++) StrB.Append(b[i].ToString("x").PadLeft(2,'0')); if (Length==16) return StrB.ToString(8,16); else return StrB.ToString(); } //⑵:在ASP。NET中可以直接使用System.Web.Security名称空间的FormsAuthentication类 public string md5(string str,int code) { if(code==16) //16位MD5加密(取32位加密的9~25字符) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower().Substring(8,16) ; } else//32位加密 { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower(); } }
相关文章推荐
- .NET生成常用16、32位MD5加密的两种方法
- .NET生成常用16、32位MD5加密的两种方法
- ubuntu上生成pycharm快捷桌面方式的两种常用方法
- java常用工具方法值MD5加密,解决得不到32位字符串的问题
- 16、2015.08.03第十七节课 C#1(.net和C#的关系、VS与.net的对应关系、VS2012常用的几种应用程序、C#定义一个类的方法、类页面内容的解释、定义Person的类、调用Person类的方法、命名规范、数值类型)
- asp.net与.net编程常用函数与方法汇总
- java多线程实现的常用两种方法
- DataGridView扩展方法行号、全选、导出到Excel(引用excel组件、生成html两种方式)
- 16-集合框架-03-常用对象API(集合框架-方法演示_1)
- php发送http请求的两种常用方法
- JqueryMobile动态生成listView并实现刷新的两种方法
- .net常用的传值的方法
- .net生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法
- VC++ 调用 C#生成DLL的两种方法
- 本文介绍两种方法,让大家在32位Win7中也能尽享大内存带来的爽快感觉!
- PHP 生成Excel表格两种方法
- c#中两种常用的异步调用方法
- 恢复Cisco路由器密码的两种常用方法
- 使用PHP生成二维码的两种方法(带logo图像)
- 数据归一化和两种常用的归一化方法