【问题标题】:Hash String does not equal to Copied Hash String from console C#哈希字符串不等于从控制台 C# 复制的哈希字符串
【发布时间】:2018-07-16 15:28:38
【问题描述】:

我创建了哈希。比我在控制台中打印它。复制哈希值并将其放入代码进行比较。但这会导致它们不一样。

        String input = "Hello";
        String key = "Key";
        Byte[] hashKey = Encoding.UTF8.GetBytes(key);

        HMACSHA1 hmac = new HMACSHA1(hashKey);
        Byte[] computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(input));
        String computedHashString = Encoding.UTF8.GetString(computedHash);
        Console.WriteLine("Hash value of your input: .{0}.", computedHashString);

        if ("↨:?☼??W?u$YLR;?←?T?j" == computedHashString)
        {
            Console.WriteLine("They are same!");
        }
        else
        {
            Console.WriteLine("They are NOT same!");
        }

        Console.ReadLine();

提前谢谢你

【问题讨论】:

    标签: c# encryption hash cryptography


    【解决方案1】:

    哈希的结果不是 UTF-8 编码的文本,不应该这样对待。将其转换为十六进制或 base64。例如:

    string computedHashString = Convert.ToBase64String(computedHash);
    

    从根本上说,您需要谨慎对待数据。将哈希结果转换为文本就像尝试将 mp3 文件加载到图片查看器中,或者尝试将应用压缩算法的结果解压缩。

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2021-04-25
      • 2019-07-29
      • 2021-12-23
      • 2011-04-28
      • 1970-01-01
      • 2013-01-20
      • 2011-08-18
      • 2019-08-08
      相关资源
      最近更新 更多