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