2011-11-21 171 views
3

我想在sqlite數據庫中存儲登錄名和密碼。此數據庫使用SQLCipher庫加密。但是,加密數據庫的密碼是個別問題。該密碼存儲在應用程序代碼中。登錄和密碼由用戶提供以登錄到應用程序。在C#中有SHA256類。如果我足夠使用這個課程?或者,我應該使用散列和鹽或其他方法?在sqlite數據庫中加密登錄名和密碼 - C#中的SHA 256#

感謝

+3

你應該散列+ salt用戶密碼。 – Nasreddine

回答

8

要在數據庫中存儲用戶密碼登錄的問題,您應該使用的哈希函數與鹽。

SHA 256是其中之一,但有更好的存在。我建議你使用PBKDF2派生函數。您可以使用.NET框架中提供的Rfc2898DeriveBytes類實現自己的PBKDF2哈希方法。

這裏是一個快速怎麼對做它

int saltSize = 256;  // Number of bytes of the salt 
int iterations = 1000; // Number of times we iterate the function 
         // The more we iterate, the more it is gonna take time. 
         //  The advantage of a great iterations number is to 
         //  make brutforce attack more painful. 
int hashSize = 20;  // Number of bytes of the hash (the output) 

var deriveBytes = new Rfc2898DeriveBytes("mypassword", saltSize, iterations); 
byte[] salt = deriveBytes.Salt; 
byte[] hash = deriveBytes.GetBytes(hashSize); 

你必須現在來存儲鹽和數據庫中的哈希值。使用Convert.FromBase64StringConvert.ToBase64Stringbyte[]獲得string,反之亦然。


另一種選擇是使用bcrypt。看到這個有趣的article