c#示例
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框架下MD5实现已经集成于System.Web.Security名称空间,只需简单调用即获取结果:
string 结果字符串=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(输入字符串,"MD5");


以下包装函数根据code参数的不同(可取16或32),分别返回参数STR的16位和32位MD5加密字串。
(16位字串用于模拟动网论坛等国内常见论坛的MD5实现)


public string md5(string str,int code)
{

if(code==16)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToLower().Substring(8,16) ;
}

if(code==32)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5");
}

return "00000000000000000000000000000000";
}


System.Web.Security名称空间封装的其他方法,可参见MSDN。

http://workgroup.cn/CS/blogs/aspnet/archive/2006/04/03/.net_4668B6670B4E8476_MD5.aspx
posted on 2007-03-08 00:21  mbskys  阅读(138)  评论(0)    收藏  举报