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

SALT+HASH撒盐加密

时间:2015-12-03 11:16:23      阅读:493      评论:0      收藏:0      [点我收藏+]

标签:

#region 撒盐加密
            string salt = Guid.NewGuid().ToString();
            byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(model.Password + salt);
            byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
            string hashString = Convert.ToBase64String(hashBytes);
            model.Password = hashString;
            model.Salt = salt;
            #endregion
            #region 撒盐解密,用户存在的情况下
            string _salt = model.Salt;
            string password = "123456";//用户输入的数据,获取过来
            byte[] _passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt);
            byte[] _hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(_passwordAndSaltBytes);
            string _hashString = Convert.ToBase64String(_hashBytes);
            if (_hashString==model.Password)
            {
                return Json(new
                {
                    error=0,
                    msg="验证成功"
                });
            }
            #endregion

 

SALT+HASH撒盐加密

标签:

原文地址:http://www.cnblogs.com/danlis/p/5015438.html

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