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

标签 c# encryption

我想在 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.FromBase64StringConvert.ToBase64Stringbyte[] 获取 string,反之亦然。


另一种选择是使用 bcrypt .看到这个有意思article .

关于c# - 在 sqlite 数据库中加密登录名和密码 - C# 中的 SHA 256,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218066/

相关文章:

c# - WPF、MVVM 和 PRISM - 没有为此对象定义无参数构造函数

c# - 为什么任务取消发生在调用者线程上?

c# - 从窗口服务显示窗口窗体

c# - 我想要一个 c# IMAP 客户端。我可以继续使用这个新库吗?

encryption - Midori128 密文不正确

c# - 运行 MSBuild 时发生内部故障 - 无法加载文件或程序集 System.Net.Primitives

ssl - OpenSSL x509 实用程序 PEM 到 DER 的转换失败,出现 "PEM_read_bio:no start line"

c - 二进制字符指针中的字节数

encryption - 如何解密通过 whatsapp web 发送的二进制消息

java - 加密是否保留 Base64 编码