我想在 sqlite 数据库中存储登录名和密码。此数据库使用 SQLCipher 库加密。但是加密数据库的密码是单独的问题。此密码存储在应用程序代码中。用户提供登录名和密码以登录应用程序。在 C# 中有 SHA256 类。如果我使用这个类是否足够?或者更确切地说,我应该使用散列和盐或其他方法?
谢谢
最佳答案
要将用户密码存储在数据库中用于登录事务,您应该使用带盐的哈希函数。
SHA 256
是其中之一,但还有更好的。我建议您使用 PBKDF2
微分函数。您可以使用 Rfc2898DeriveBytes
实现您自己的 PBKDF2
哈希方法.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);
您现在只需将盐和散列存储在您的数据库中。使用 Convert.FromBase64String
和 Convert.ToBase64String
从 byte[]
获取 string
,反之亦然。
关于c# - 在 sqlite 数据库中加密登录名和密码 - C# 中的 SHA 256,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218066/