【问题标题】:How to manage private data in a C# code?如何在 C# 代码中管理私有数据?
【发布时间】:2014-05-20 13:53:51
【问题描述】:

我正在制作一个 WPF C# 应用程序,根据我在许多论坛的其他主题上阅读的内容,可以从 .exe 文件重建 C# 应用程序的代码。

现在在我的代码中有一个包含数据库登录数据的字符串,并且我也在考虑使用 simmetric cryptography 将加密密码发送到 db,因此客户端的代码将包含 simmetric 密钥,但是这个问题将使我为制作安全应用程序所做的所有努力都付诸东流。

如何解决这个安全问题,尤其是在我的情况下?

【问题讨论】:

标签: c# wpf security cryptography


【解决方案1】:

解决方案是在数据库中散列密码而不加密。哈希是字符串的单向转换,不能反转。

然后,您对用户提供的输入值进行哈希处理,并将其与您在数据库中的值进行比较。如果哈希匹配,他们可以登录,否则会显示错误。

 static string GetMd5Hash(MD5 md5Hash, string input)
    {

        // Convert the input string to a byte array and compute the hash. 
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes 
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data  
        // and format each one as a hexadecimal string. 
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string. 
        return sBuilder.ToString();
    }

    // Verify a hash against a string. 
    static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
    {
        // Hash the input. 
        string hashOfInput = GetMd5Hash(md5Hash, input);

        // Create a StringComparer an compare the hashes.
        StringComparer comparer = StringComparer.OrdinalIgnoreCase;

        if (0 == comparer.Compare(hashOfInput, hash))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

从这里MSDN site

【讨论】:

  • 不要使用未加盐的 MD5 哈希 - 非常不安全。
  • 这只是一个演示,而不是端到端的解决方案。它旨在指向正确的做事方式。是的,应该对哈希进行哈希处理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
  • 2021-03-14
  • 2018-06-15
  • 1970-01-01
相关资源
最近更新 更多