初级:.net框架下的MD5
2004-11-06 21:47
525 查看
近日发现有程序员在.NET项目中自己编写MD5算法实现,为避免不必要的资源消耗,特撰此文。
.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。
*本处以C#为例
.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。
*本处以C#为例
相关文章推荐
- C#对称加密与非对称加密实例
- C#添加、读取Word脚注尾注的方法
- C语言使用openSSL库DES模块实现加密功能详解
- Java使用AES加密和解密的实例详解
- 将15位身份证补全为18位身份证的算法示例详解
- 使用Python进行AES加密和解密的示例代码
- C++算法系列之日历生成的算法代码
- 谁来关心我们这些程序员
- c#接简单数据库操作类
- Microsoft Visual Studio .NET 2003制作安装程序
- Java测试规范(引用)
- duwamish7的感悟!
- Java面试题目
- 英雄无敌程序员版
- 一个非常不错的业务规则管理器
- 统一权限系统URight1.5
- delphi中的时间操作技术(2)
- 计算器原代码的问题