(一) 為什么要用哈希函數(shù)來(lái)加密密碼
如果你需要保存密碼(比如網(wǎng)站用戶的密碼),你要考慮如何保護(hù)這些密碼數(shù)據(jù),象下面那樣直接將密碼寫入數(shù)據(jù)庫(kù)中是極不安全的,因?yàn)槿魏慰梢源蜷_(kāi)數(shù)據(jù)庫(kù)的人,都將可以直接看到這些密碼。
解決的辦法是將密碼加密后再存儲(chǔ)進(jìn)數(shù)據(jù)庫(kù),比較常用的加密方法是使用哈希函數(shù)(hash function)。哈希函數(shù)的具體定義,大家可以在網(wǎng)上或者相關(guān)書籍中查閱到,簡(jiǎn)單地說(shuō),它的特性如下:
(1)原始密碼經(jīng)哈希函數(shù)計(jì)算后得到一個(gè)哈希值
(2)改變?cè)济艽a,哈希函數(shù)計(jì)算出的哈希值也會(huì)相應(yīng)改變
(3) 同樣的密碼,哈希值也是相同的
(4) 哈希函數(shù)是單向、不可逆的。也就是說(shuō)從哈希值,你無(wú)法推算出原始的密碼是多少
有了哈希函數(shù),我們就可以將密碼的哈希值存儲(chǔ)進(jìn)數(shù)據(jù)庫(kù)。用戶登錄網(wǎng)站的時(shí)候,我們可以檢驗(yàn)用戶輸入密碼的哈希值是否與數(shù)據(jù)庫(kù)中的哈希值相同。
由于哈希函數(shù)是不可逆的,即使有人打開(kāi)了數(shù)據(jù)庫(kù),也無(wú)法看到用戶的密碼是多少。
那么存儲(chǔ)經(jīng)過(guò)哈希函數(shù)加密后的密碼是否就是安全的了呢?我們先來(lái)看一下幾種常見(jiàn)的破解密碼的方法。
(二) 幾種常見(jiàn)的破解密碼的方法
最簡(jiǎn)單、常見(jiàn)的破解方式當(dāng)屬字典破解(dictionary attack)和暴力破解(brute force attack)方式。這兩種方法說(shuō)白了就是猜密碼。
字典破解和暴力破解都是效率比較低的破解方式。如果你知道了數(shù)據(jù)庫(kù)中密碼的哈希值,你就可以采用一種更高效的破解方式,查表法(lookup tables)。還有一些方法,比如逆向查表法(reverse lookup tables)、彩虹表(rainbow tables)等,都和查表法大同小異。現(xiàn)在我們來(lái)看一下查表法的原理。
查表法不像字典破解和暴力破解那樣猜密碼,它首先將一些比較常用的密碼的哈希值算好,然后建立一張表,當(dāng)然密碼越多,這張表就越大。當(dāng)你知道某個(gè)密碼的哈希值時(shí),你只需要在你建立好的表中查找該哈希值,如果找到了,你就知道對(duì)應(yīng)的密碼了。
(三) 為密碼加鹽(salt)
從上面的查表法可以看出,即便是將原始密碼加密后的哈希值存儲(chǔ)在數(shù)據(jù)庫(kù)中依然是不夠安全的。那么有什么好的辦法來(lái)解決這個(gè)問(wèn)題呢?答案是加鹽。
鹽(salt)是什么?就是一個(gè)隨機(jī)生成的字符串。我們將鹽與原始密碼連接(concat)在一起(放在前面或后面都可以),然后將concat后的字符串加密。采用這種方式加密密碼,查表法就不靈了(因?yàn)辂}是隨機(jī)生成的)。
(四) 在.net中的實(shí)現(xiàn)
在.net中,生成鹽可以使用rngcryptoserviceprovider類,當(dāng)然也可以使用guid。哈希函數(shù)的算法我們可以使用sha(secure hash algorithm)家族算法,當(dāng)然哈希函數(shù)的算法有很多,比如你也可以采用md5。這里順便提一下,美國(guó)政府以前廣泛采用sha-1算法,在2005年被我國(guó)山東大學(xué)的王小云教授發(fā)現(xiàn)了安全漏洞,所以現(xiàn)在比較常用sha-1加長(zhǎng)的變種,比如sha-256。在.net中,可以使用sha256managed類。
下面來(lái)看一段代碼演示如何在.net中實(shí)現(xiàn)給密碼加鹽加密。加密后的密碼保存在mysql數(shù)據(jù)庫(kù)中。
下面的代碼演示如何注冊(cè)一個(gè)新帳戶。鹽的生成可以使用新guid,也可以使用rngcryptoserviceprovider 類。將byte[]轉(zhuǎn)換為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(); } |
下面的代碼演示了如何檢驗(yàn)登錄用戶的密碼是否正確。首先檢驗(yàn)用戶名是否存在,如果存在,獲得該用戶的鹽,然后用該鹽和用戶輸入的密碼來(lái)計(jì)算哈希值,并和數(shù)據(jù)庫(kù)中的哈希值進(jìn)行比較。
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" ); } } |
總結(jié):?jiǎn)螁问褂霉:瘮?shù)來(lái)為密碼加密是不夠的,需要為密碼加鹽來(lái)提高安全性,鹽的長(zhǎng)度不能過(guò)短,并且鹽的產(chǎn)生應(yīng)該是隨機(jī)的。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/freeliver54/p/3623299.html#undefined