C# 中的MD5加密

来源:互联网 发布:华为电视盒安装软件 编辑:程序博客网 时间:2024/05/17 04:54

以前在asp里要自己写md5的加密方法。但在C#中已经存在一个类实现了md5加密,我们只需调用即可:

核心即:System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("要加密的字符串","MD5")

可以自己封装一下便于在项目里使用:

public string MyMd5(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(); } }


Md5是不可逆的加密,所以只能把要比较的东东也md5加密后,和现有的加密后的字符串比较是否相同。

原创粉丝点击