使用哈希加鹽法來爲密碼加密(補充JAVA的實現)

使用哈希加鹽法來爲密碼加密
轉自:http://www.cnblogs.com/jfzhu/p/4023439.html
轉載請註明出處
 
(一)爲何要用哈希函數來加密密碼
若是你須要保存密碼(好比網站用戶的密碼),你要考慮如何保護這些密碼數據,象下面那樣直接將密碼寫入數據庫中是極不安全的,由於任何能夠打開數據庫的人,都將能夠直接看到這些密碼。
 
 
解決的辦法是將密碼加密後再存儲進數據庫,比較經常使用的加密方法是使用哈希函數(Hash Function)。哈希函數的具體定義,你們能夠在網上或者相關書籍中查閱到,簡單地說,它的特性以下:
 
(1)原始密碼經哈希函數計算後獲得一個哈希值
(2)改變原始密碼,哈希函數計算出的哈希值也會相應改變
(3) 一樣的密碼,哈希值也是相同的
(4) 哈希函數是單向、不可逆的。也就是說從哈希值,你沒法推算出原始的密碼是多少
 
有了哈希函數,咱們就能夠將密碼的哈希值存儲進數據庫。用戶登陸網站的時候,咱們能夠檢驗用戶輸入密碼的哈希值是否與數據庫中的哈希值相同。
 

 

因爲哈希函數是不可逆的,即便有人打開了數據庫,也沒法看到用戶的密碼是多少。
那麼存儲通過哈希函數加密後的密碼是否就是安全的了呢?咱們先來看一下幾種常見的破解密碼的方法。
 
(二)幾種常見的破解密碼的方法
最簡單、常見的破解方式當屬字典破解(Dictionary Attack)和暴力破解(Brute Force Attack)方式。這兩種方法說白了就是猜密碼。
 
字典破解和暴力破解都是效率比較低的破解方式。若是你知道了數據庫中密碼的哈希值,你就能夠採用一種更高效的破解方式,查表法(Lookup Tables)。還有一些方法,好比逆向查表法(Reverse Lookup Tables)、彩虹表(Rainbow Tables)等,都和查表法大同小異。如今咱們來看一下查表法的原理。
 
查表法不像字典破解和暴力破解那樣猜密碼,它首先將一些比較經常使用的密碼的哈希值算好,而後創建一張表,固然密碼越多,這張表就越大。當你知道某個密碼的哈希值時,你只須要在你創建好的表中查找該哈希值,若是找到了,你就知道對應的密碼了。
 
(三)爲密碼加鹽(Salt)
從上面的查表法能夠看出,即使是將原始密碼加密後的哈希值存儲在數據庫中依然是不夠安全的。那麼有什麼好的辦法來解決這個問題呢?答案是加鹽。
 
鹽(Salt)是什麼?就是一個隨機生成的字符串。咱們將鹽與原始密碼鏈接(concat)在一塊兒(放在前面或後面均可以),而後將concat後的字符串加密。採用這種方式加密密碼,查表法就不靈了(由於鹽是隨機生成的)。
 
 
(四)在.NET中的實現
在.NET中,生成鹽可使用RNGCryptoServiceProvider類,固然也可使用GUID。哈希函數的算法咱們可使用SHA(Secure Hash Algorithm)家族算法,固然哈希函數的算法有不少,好比你也能夠採用MD5。這裏順便提一下,美國政府之前普遍採用SHA-1算法,在2005年被我國山東大學的王小云教授發現了安全漏洞,因此如今比較經常使用SHA-1加長的變種,好比SHA-256。在.NET中,可使用SHA256Managed類。
 
下面來看一段代碼演示如何在.NET中實現給密碼加鹽加密。加密後的密碼保存在MySQL數據庫中。
 
 
下面的代碼演示如何註冊一個新賬戶。鹽的生成可使用新Guid,也可使用RNGCryptoServiceProvider 類。將byte[]轉換爲string,可使用Base64String(我在之前的博客中介紹過Base64 編碼),也可使用下面的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");
}
}
 
(五)總結
單單使用哈希函數來爲密碼加密是不夠的,須要爲密碼加鹽來提升安全性,鹽的長度不能太短,而且鹽的產生應該是隨機的。
 
 
========= 補充:JAVA的實現=========
 
1)數據庫(mysql)裏有兩個字段:
`password` varchar(100) DEFAULT NULL,
`password_salt` varchar(50) DEFAULT NULL,
 
2)生成隨機鹽,加密
import java.util.UUID;
 
//用UUID生成隨機鹽
String salt = UUID.randomUUID().toString();
password = DigestUtil.sha256Digest(password + salt));
//TODO 將salt和password一塊兒保存到數據庫裏
 
public class DigestUtil {
private static String DEFAULT_ENCODING = "UTF-8";
private static String SHA_256 = "SHA-256";
 
public static String sha256Digest(String str) {
return Digest.digest(str, SHA_256, DEFAULT_ENCODING);
}
}
 
import java.security.MessageDigest;
public class Digest {
public static String digest(String str, String alg, String charencoding) {
try {
byte[] data = str.getBytes(charencoding);
MessageDigest md = MessageDigest.getInstance(alg);
return Hex.toHex(md.digest(data));
} catch (Exception var5) {
throw new RuntimeException("digest fail!", var5);
}
}
}
相關文章
相關標籤/搜索