C#使用 Salt + Hash 来为密码加密

时间:2022-01-08 14:44:22

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

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

C#使用 Salt + Hash 来为密码加密

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

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

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

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

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

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

C#使用 Salt + Hash 来为密码加密

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

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

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

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

C#使用 Salt + Hash 来为密码加密

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

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

C#使用 Salt + Hash 来为密码加密

(三) 为密码加盐(salt)

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

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

C#使用 Salt + Hash 来为密码加密

(四) 在.net中的实现

在.net中,生成盐可以使用rngcryptoserviceprovider类,当然也可以使用guid。哈希函数的算法我们可以使用sha(secure hash algorithm)家族算法,当然哈希函数的算法有很多,比如你也可以采用md5。这里顺便提一下,美国*以前广泛采用sha-1算法,在2005年被我国山东大学的王小云教授发现了安全漏洞,所以现在比较常用sha-1加长的变种,比如sha-256。在.net中,可以使用sha256managed类。

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

C#使用 Salt + Hash 来为密码加密

下面的代码演示如何注册一个新帐户。盐的生成可以使用新guid,也可以使用rngcryptoserviceprovider 类。将byte[]转换为string,可以使用base64string,也可以使用下面的tohexstring方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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();
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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");
 }
}

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!

原文链接:http://www.cnblogs.com/freeliver54/p/3623299.html#undefined