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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端通过哈希算法进行盐加密,后端需要使用相同的盐和哈希算法进行解密。下面是Java中的一个示例代码: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SaltHash { public static void main(String[] args) throws NoSuchAlgorithmException { String password = "123456"; String salt = "abcdefg"; String encryptedPassword = encryptPassword(password, salt); System.out.println("Encrypted Password: " + encryptedPassword); String decryptedPassword = decryptPassword(encryptedPassword, salt); System.out.println("Decrypted Password: " + decryptedPassword); } public static String encryptPassword(String password, String salt) throws NoSuchAlgorithmException { String saltedPassword = salt + password; MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(saltedPassword.getBytes()); byte[] byteData = md.digest(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } public static String decryptPassword(String encryptedPassword, String salt) throws NoSuchAlgorithmException { // In hash algorithms, decryption is not possible. We can only compare encrypted and decrypted passwords. String decryptedPassword = encryptPassword(encryptedPassword, salt); return decryptedPassword; } } ``` 在上面的示例中,我们使用了SHA-256哈希算法来加密密码。在加密过程中,我们将盐和密码拼接在一起,然后对其进行哈希。在解密过程中,我们不能直接解密加密后的密码,因为哈希算法不支持解密。我们只能对输入的密码进行哈希,然后与加密后的密码进行比较,以判断密码是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值