zoukankan      html  css  js  c++  java
  • HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法

    今天在开发一个软件时,同时在B/S版和C/S版中都要用到MD5加密,在
    ASP.NET中使用的是


        /**//// <summary>
        /// 取得MD5加密串
        /// </summary>
        /// <param name="input">源明文字符串</param>
        /// <returns>密文字符串</returns>
        public static string GetMD5Hash(string input)
        ...{
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strOriginal, "MD5");
        }
    的加密方式;
    而在WinForm中用的是

        /**//// <summary>
        /// 取得MD5加密串
        /// </summary>
        /// <param name="input">源明文字符串</param>
        /// <returns>密文字符串</returns>
        public static string GetMD5Hash(string input)
        ...{
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.Default.GetBytes(input);
            bs = md5.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            ...{
                s.Append(b.ToString("x2").ToUpper());
            }
            string password = s.ToString();
            return password;
        }
    但发现同样一个字符串,两种方式得出的密文是不一样的,最后才发现原来是编码方式导致的,ASP.NET里默认的是UTF-8编码,把WinForm程序里的System.Text.Encoding.Default.GetBytes(input);替换成System.Text.Encoding.UTF8.GetBytes(input);即可
    最终正确版:

        /**//// <summary>
        /// 取得MD5加密串
        /// </summary>
        /// <param name="input">源明文字符串</param>
        /// <returns>密文字符串</returns>
        public static string GetMD5Hash(string input)
        ...{
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = md5.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            ...{
                s.Append(b.ToString("x2").ToUpper());
            }
            string password = s.ToString();
            return password;
        }

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ChaoYang0502/archive/2008/05/15/2448156.aspx

  • 相关阅读:
    【codevs1690】开关灯 (线段树 区间修改+区间求和 (标记))
    【codevs1191】数轴染色 (线段树 区间修改+固定区间查询)
    【机器学习】李航 统计学习方法 知识点总结
    【机器学习】生成模型 判别模型
    【机器学习】贝叶斯定理、精准推断、最大似然估计、连续特征参数估计、EM算法
    python queue 讲解
    【tensorflow】 CNN卷积神经网络原理讲解+图片识别应用(附源码)
    URL解析过程
    Python 可迭代对象迭代器生成器的区别
    【Linux】 修改主机名
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1745394.html
Copyright © 2011-2022 走看看