散列字符串不等于来自控制台C#的复制散列字符串

Rid*_*lah -1 c# encryption hash cryptography

我创建了哈希.比我在控制台上打印它.复制哈希值并将其放入代码进行比较.但它产生的不一样.

        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();
Run Code Online (Sandbox Code Playgroud)

安慰

先感谢您

Jon*_*eet 6

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

string computedHashString = Convert.ToBase64String(computedHash);
Run Code Online (Sandbox Code Playgroud)

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