SALT+HASH撒鹽加密


#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

 

看到個高級的關於撒鹽的網址 http://blog.jobbole.com/61872/#article-comment


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM