加密--HashPasswordForStoringInConfigFile过时问题

时间:2015-09-24 16:03:31   收藏:0   阅读:6152

最近公司在对一套代码进行重构,把原本的web form换成mvc。

刚刚好几天打算开始做下登录,登录则必然会涉及到密码加密的问题。
原本打算用旧的加密方法就行了,哪里知道其中的md5加密出现了这样的问题:
     System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");

  技术分享  

技术分享

虽然不影响使用,但是有条波浪线看起来很不舒服,于是决定替换掉。
先不说多的,上代码先。
    第一步:添加引用
  技术分享
  
  第二步:
         System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
        BitConverter.ToString( md5.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-",null);
运行下程序:
  技术分享
程序到这里就完成了。
 
         BitConverter.ToString( md5.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-",null);
        主要进行了三步操作
        第一步:获取指定字符串中的所有字符编码为一个字节序列;
        byte[] byteResult =  md5.ComputeHash(Encoding.UTF8.GetBytes(str));//获取加密后的序列
        第二步: 将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式,这里转换出来的十六进制字符串是"A8-F5-F1-67-F4-4F-49-64-E6-C9-98-DE-E8-27-11-0C"这样的,
        string strResult =   BitConverter.ToString(byteResult);//转换为十六进制
        第三步,去掉"-"符号
        string realResult = strResult  .Replace("-",null);
 
关于加密解密,可以看下HashAlgorithm 类以及它的派生类
https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.hashalgorithm%28v=vs.110%29.aspx
 

原文:http://www.cnblogs.com/Cowait/p/4835365.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!