在sqlite数据库中加密登录名和密码 - 在C#中使用SHA 256

Rob*_*ert 4 c# encryption

我想在sqlite数据库中存储登录名和密码.此数据库使用SQLCipher库加密.但加密数据库的密码是单独的问题.此密码存储在应用程序代码中.用户提供登录和密码以登录应用程序.在C#中有SHA256类.如果我使用这个课程就够了吗?或者我应该使用哈希和盐或其他方法?

谢谢

Oti*_*iel 9

要在数据库中存储用户密码以进行登录,您应该使用带有salt的哈希函数.

SHA 256是其中之一,但存在更好的.我建议你使用PBKDF2派生功能.您可以PBKDF2使用Rfc2898DeriveBytes.NET框架中提供的类来实现自己的散列方法.

这是一个快速的操作方法:

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);
Run Code Online (Sandbox Code Playgroud)

您现在只需将salt和hash存储在数据库中.使用Convert.FromBase64String和从a Convert.ToBase64String获得string,byte[]反之亦然.


另一种选择是使用bcrypt.看到这篇有趣的文章.