zoukankan      html  css  js  c++  java
  • Web安全--使用Salt + Hash将密码加密后再存储进数据库

    转载原地址 http://www.bozhiyue.com/mianshiti/_net/2016/0728/314239.html

    (一) 为什么要用哈希函数来加密密码

    如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到这些密码。

    解决的办法是将密码加密后再存储进数据库,比较常用的加密方法是使用哈希函数(Hash Function)。哈希函数的具体定义,大家可以在网上或者相关书籍中查阅到,简单地说,它的特性如下:

    (1)原始密码经哈希函数计算后得到一个哈希值

    (2)改变原始密码,哈希函数计算出的哈希值也会相应改变

    (3) 同样的密码,哈希值也是相同的

    (4) 哈希函数是单向、不可逆的。也就是说从哈希值,你无法推算出原始的密码是多少

    有了哈希函数,我们就可以将密码的哈希值存储进数据库。用户登录网站的时候,我们可以检验用户输入密码的哈希值是否与数据库中的哈希值相同。

    由于哈希函数是不可逆的,即使有人打开了数据库,也无法看到用户的密码是多少。

    那么存储经过哈希函数加密后的密码是否就是安全的了呢?我们先来看一下几种常见的破解密码的方法。

    (二) 几种常见的破解密码的方法

    最简单、常见的破解方式当属字典破解(Dictionary Attack)和暴力破解(Brute Force Attack)方式。这两种方法说白了就是猜密码。

    image

    字典破解和暴力破解都是效率比较低的破解方式。如果你知道了数据库中密码的哈希值,你就可以采用一种更高效的破解方式,查表法(Lookup Tables)。还有一些方法,比如逆向查表法(Reverse Lookup Tables)、彩虹表(Rainbow Tables)等,都和查表法大同小异。现在我们来看一下查表法的原理。

    查表法不像字典破解和暴力破解那样猜密码,它首先将一些比较常用的密码的哈希值算好,然后建立一张表,当然密码越多,这张表就越大。当你知道某个密码的哈希值时,你只需要在你建立好的表中查找该哈希值,如果找到了,你就知道对应的密码了。

    image

    (三) 为密码加盐(Salt)

    从上面的查表法可以看出,即便是将原始密码加密后的哈希值存储在数据库中依然是不够安全的。那么有什么好的办法来解决这个问题呢?答案是加盐。

    盐(Salt)是什么?就是一个随机生成的字符串。我们将盐与原始密码连接(concat)在一起(放在前面或后面都可以),然后将concat后的字符串加密。采用这种方式加密密码,查表法就不灵了(因为盐是随机生成的)。

    Picture1

    (四) 在.NET中的实现

    在.NET中,生成盐可以使用RNGCryptoServiceProvider类,当然也可以使用GUID。哈希函数的算法我们可以使用SHA(Secure Hash Algorithm)家族算法,当然哈希函数的算法有很多,比如你也可以采用MD5。这里顺便提一下,美国政府以前广泛采用SHA-1算法,在2005年被我国山东大学的王小云教授发现了安全漏洞,所以现在比较常用SHA-1加长的变种,比如SHA-256。在.NET中,可以使用SHA256Managed类。

    下面来看一段代码演示如何在.NET中实现给密码加盐加密。加密后的密码保存在MySQL数据库中。

    image

    下面的代码演示如何注册一个新帐户。盐的生成可以使用新Guid,也可以使用RNGCryptoServiceProvider 类。将byte[]转换为string,可以使用Base64String(我在以前的博客中介绍过Base 64 Encoding 编码),也可以使用下面的ToHexString方法。

    复制代码 代码如下:

    protected void ButtonRegister_Click(object sender, EventArgs e) 

        string username = TextBoxUserName.Text; 
        string password = TextBoxPassword.Text; 
        // random salt 
        string salt = Guid.NewGuid().ToString();

        // random salt 
        // you can also use RNGCryptoServiceProvider class            
        //System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); 
        //byte[] saltBytes = new byte[36]; 
        //rng.GetBytes(saltBytes); 
        //string salt = Convert.ToBase64String(saltBytes); 
        //string salt = ToHexString(saltBytes); 

        byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(password + salt);            
        byte[] hashBytes = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);

        string hashString = Convert.ToBase64String(hashBytes);

        // you can also use ToHexString to convert byte[] to string 
        //string hashString = ToHexString(hashBytes); 

        var db = new TestEntities(); 
        usercredential newRecord = usercredential.Createusercredential(username, hashString, salt); 
        db.usercredentials.AddObject(newRecord); 
        db.SaveChanges(); 
    }

    string ToHexString(byte[] bytes) 

        var hex = new StringBuilder(); 
        foreach (byte b in bytes) 
        { 
            hex.AppendFormat("{0:x2}", b); 
        } 
        return hex.ToString(); 
    }


    下面的代码演示了如何检验登录用户的密码是否正确。首先检验用户名是否存在,如果存在,获得该用户的盐,然后用该盐和用户输入的密码来计算哈希值,并和数据库中的哈希值进行比较。

    复制代码 代码如下:

    protected void ButtonSignIn_Click(object sender, EventArgs e) 

    string username = TextBoxUserName.Text; 
    string password = TextBoxPassword.Text; 

    var db = new TestEntities(); 
    usercredential record = db.usercredentials.Where(x => string.Compare(x.UserName, username, true) == 0).FirstOrDefault(); 
    if (record == default(usercredential)) 

    throw new ApplicationException("invalid user name and password"); 


    string salt = record.Salt; 
    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 == record.PasswordHash) 

    // user login successfully 

    else 

    throw new ApplicationException("invalid user name and password"); 

    }


    总结:单单使用哈希函数来为密码加密是不够的,需要为密码加盐来提高安全性,盐的长度不能过短,并且盐的产生应该是随机的。

  • 相关阅读:
    AtCoder Beginner Contest 205
    Codeforces Round #725 (Div. 3)
    Educational Codeforces Round 110 (Rated for Div. 2)【A
    Codeforces Round #722 (Div. 2)
    AtCoder Beginner Contest 203(Sponsored by Panasonic)
    AISing Programming Contest 2021(AtCoder Beginner Contest 202)
    PTA 520 钻石争霸赛 2021
    Educational Codeforces Round 109 (Rated for Div. 2)【ABCD】
    AtCoder Beginner Contest 200 E
    Educational Codeforces Round 108 (Rated for Div. 2)【ABCD】
  • 原文地址:https://www.cnblogs.com/wphl-27/p/5959049.html
Copyright © 2011-2022 走看看