MD5加密

MD5加密:对数据进行加密,不可逆,这里用于对密码进行加密

1.MD5加密帮助类

    /// <summary>
    /// MD5加密类型(2.0新增)
    /// </summary>
    public class MD5Helper
    {
        /// <summary>
        /// 加密账号密码
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns>加密后的密码</returns>
        public static string EncryptPassword(string account, string password)
        {
            if (string.IsNullOrWhiteSpace(account) || string.IsNullOrWhiteSpace(password))
            {
                return password;
            }

            string encryptPassword = string.Empty;
            encryptPassword = Encrypt(string.Format("{0}={1}", account, password));
            return encryptPassword;
        }

        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="str">加密前数据</param>
        /// <returns>加密后数据</returns>
        public static string Encrypt(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return string.Empty;
            }

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = Encoding.Unicode.GetBytes(str);
            byte[] targetData = md5.ComputeHash(fromData);
            string result = string.Empty;
            for (int i = 0; i < targetData.Length; i++)
            {
                result += targetData[i].ToString("x");
            }
            return result;
        }
    }
View Code

 

 

2.调用

string password = MD5Helper.EncryptPassword(Account, Password);
View Code

 

posted @ 2015-01-23 09:40  zhuifeng0724  阅读(163)  评论(0编辑  收藏  举报