#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