2011-11-21 173 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