c# - 如何在本地正确存储密码

标签 c# encryption

我一直在阅读 MSDN 上的这篇文章 Rfc2898DeriveBytes .这是他们提供的示例加密代码。

string pwd1 = passwordargs[0];
// Create a byte array to hold the random value. 
byte[] salt1 = new byte[8];
using (RNGCryptoServiceProvider rngCsp = ne RNGCryptoServiceProvider())
{
    // Fill the array with a random value.
    rngCsp.GetBytes(salt1);
}

//data1 can be a string or contents of a file.
string data1 = "Some test data";
//The default iteration count is 1000 so the two methods use the same iteration count.
int myIterations = 1000;
try
{
    Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1,salt1,myIterations);
    Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, salt1);
    // Encrypt the data.
    TripleDES encAlg = TripleDES.Create();
    encAlg.Key = k1.GetBytes(16);
    MemoryStream encryptionStream = new MemoryStream();
    CryptoStream encrypt = newCryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write);
    byte[] utfD1 = new System.Text.UTF8Encoding(false).GetBytes(data1);

    encrypt.Write(utfD1, 0, utfD1.Length);
    encrypt.FlushFinalBlock();
    encrypt.Close();
    byte[] edata1 = encryptionStream.ToArray();
    k1.Reset();

我的问题是,我如何正确地将散列数据读入/写入文本文件?

我的主要目标是做这个 developer是在做。我需要在本地存储密码。当我的应用程序提示用户输入密码时,用户将输入密码,然后我的应用程序将从文本文件中读取并验证用户输入的密码是否确实正确。我该怎么做?

最佳答案

您通常会存储密码的哈希值,然后当用户输入密码时,您会根据输入的密码计算哈希值并将其与存储的哈希值进行比较——也就是说,仅仅哈希通常是不够的(从安全的角度来看) ) 并且您应该改用 PKBDF2(基于密码的 key 派生函数 2)之类的函数。这是一篇以更详尽的方式涵盖所有这些信息的文章以及示例代码(页面底部):http://www.codeproject.com/Articles/704865/Salted-Password-Hashing-Doing-it-Right

这里是 codereview 的链接,我猜它指的是与上述文章相同的实现。

关于c# - 如何在本地正确存储密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355719/

相关文章:

php - 比 md5 更短的 php 密码?

ios - 如何使用 openssl 解密使用 CCCrypt 在 Objective-C 中创建的文件?

java - 编辑代码示例以指定 DES key 值

c# - ASP.NET 按钮单击事件未在 Bootstrap 模式中触发

c# - 以编程方式将 SQLite 转换为 .sql

c# - 系统.诊断.进程参数

c# - 工具条的组合框没有选定的值

c# - 将 Java Triple DES 结果与 C# 1 相匹配

android - 密码输出和输入流

c# - ILookup<TKey, TElement> 不应该在 TElement 中(声明)协变吗?