码迷,mamicode.com
首页 > 其他好文 > 详细

MD5加密

时间:2015-01-23 10:41:22      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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

 

MD5加密

标签:

原文地址:http://www.cnblogs.com/ywblog/p/4243296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!