Vb.net 将MD5哈希写入文件时会出现大量无关字符 更新:

Vb.net 将MD5哈希写入文件时会出现大量无关字符 更新:,vb.net,Vb.net,我做了一些编辑来清理那些无用的工作 我以前知道,在文件写入过程中会发生“某些事情”,而我个人并没有意识到这一点 多亏了评论中的钚,我知道问题出在我如何处理这件事上,但我不知道如何正确处理,研究也没有发现任何结果 我正在尝试编写一个文件,该文件必须以md5校验和作为前缀 结果应该是 [hash of "the brown fox"]the brown fox 在我测试的示例文件中,这是我想要复制的哈希。以UTF-8或十六进制查看显示,这些是其中唯一的字符 ¶ Â趓Naþ¼Wô}¾} VB

我做了一些编辑来清理那些无用的工作

我以前知道,在文件写入过程中会发生“某些事情”,而我个人并没有意识到这一点

多亏了评论中的钚,我知道问题出在我如何处理这件事上,但我不知道如何正确处理,研究也没有发现任何结果


我正在尝试编写一个文件,该文件必须以md5校验和作为前缀

结果应该是

[hash of "the brown fox"]the brown fox
在我测试的示例文件中,这是我想要复制的哈希。以UTF-8或十六进制查看显示,这些是其中唯一的字符

¶ Â趓Naþ¼Wô}¾}
VB.net Debug.print正是这样产生的

Hash.tostring: ¶ Â趓Naþ¼Wô}¾}
hash.length: 16
hash.tostring.length: 16
utf8hash.length: 28 -- I think this is part of the problem?
文件写入,如UTF-8或十六进制编辑器中所示

¶ Â趓Naþ¼Wô}¾}
这是我大部分努力的一个变体

    Dim HashMaker As New MD5CryptoServiceProvider()
    Dim HashBytes As Byte() = HashMaker.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()))
    Dim Hash As New StringBuilder()
    For hx = 0 To HashBytes.Length - 1
        Hash.Append(Chr(HashBytes(hx)))
    Next

    Dim HashMaker As New MD5CryptoServiceProvider()
    Dim HashBytes As Byte() = HashMaker.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()))
    Dim Hash As New StringBuilder()
    For hx = 0 To HashBytes.Length - 1
        Hash.Append(Chr(HashBytes(hx)))
    Next

    Debug.Print(Hash.ToString())

    Dim fHeader As Byte() = New Byte(7) {}
    Array.Copy(b, fHeader, 8)

    Dim utf8hash As Byte() = Encoding.UTF8.GetBytes(Hash.ToString())
    System.IO.File.WriteAllText(fname & "_long", "")
    Dim s As New System.IO.FileStream(fname & "_long", System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
    s.Write(b, 0, 8)
    s.Write(utf8hash, 0, utf8hash.Length)
    s.Write(Encoding.UTF8.GetBytes(sb.ToString()), 0, sb.Length)
    s.Close()
我试过另一种方法

        Dim utf8 As String = ""
        For hx = 0 To HashBytes.Length - 1
            Hash.Append(Chr(HashBytes(hx)))
            utf8 &= Chr(HashBytes(hx))
        Next

        Dim fHeader As Byte() = New Byte(7) {}
        Array.Copy(b, fHeader, 8)

        Dim utf8hash As Byte() = Encoding.UTF8.GetBytes(Hash.ToString())
        Debug.Print("Hash.tostring: " & Hash.ToString())
        Debug.Print("hash.length: " & Hash.Length)
        Debug.Print("hash.tostring.length: " & Hash.ToString.Length)
        Debug.Print("utf8hash.length: " & utf8.Length)
        System.IO.File.WriteAllText(fname & "_long", "")
        Dim s As New System.IO.FileStream(fname & "_long", System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
        s.Write(b, 0, 8)
        s.Write(Encoding.UTF8.GetBytes(utf8), 0, 16)
        s.Write(Encoding.UTF8.GetBytes(sb.ToString()), 0, sb.Length)
关于一个独立的例子: 下面的代码将此打印到调试窗口

    kÍF!ÓsÊÞNƒ&'´ö (it is prefixed by a tab, part of the computed hash)
但将其写入文件(如十六进制编辑器或将编码设置为UTF-8中所示)

ÂkÃF!ÓsÊÞNÆ’&'´ö

结果证明我需要做的是将所有字节组装成一个单字节数组,就像这样

前8个字节是一种报头,后16个字节是剩余字节的MD5校验和

我希望这对某人有帮助

Dim FileBytes(8 + HashBytes.Length + StringBytes.Length - 1) As Byte
Array.Copy(b, 0, FileBytes, 0, 8)
Array.Copy(HashBytes, 0, FileBytes, 8, 16)
Array.Copy(StringBytes, 0, FileBytes, 24, StringBytes.Length)
System.IO.File.WriteAllBytes(fname, FileBytes)

哈希太长了,读不下去了。HexYou的字符串应该是一个BASE64字符串(特别是如果你以后用它来验证)或者编码它,因为它应该打开Onter选项。不是一个字节,而是一个string@Plutonix我的实时代码与
&H01
完全不同。我正在快速创建一个自包含的示例。结果证明,为了复制结果,我不需要那个字节数组,但我忘记了删除它。抱歉。重点仍然是,
Chr(HashBytes(hx)
是对二进制数据进行编码的错误方法,如果您计划使用它来验证哈希,则不应“清理”它。嘿,我在想怎么做时遇到了问题。我正在将哈希写入文件,并得到奇怪的字符。我尝试了“清理”那些字符是因为我试图匹配的哈希没有它们,结果发现它们是因为我做错了什么而出现的。我知道我一定是做错了什么或错过了一个步骤,但很难弄清楚那是什么。无论如何,感谢你的洞察力。
Dim FileBytes(8 + HashBytes.Length + StringBytes.Length - 1) As Byte
Array.Copy(b, 0, FileBytes, 0, 8)
Array.Copy(HashBytes, 0, FileBytes, 8, 16)
Array.Copy(StringBytes, 0, FileBytes, 24, StringBytes.Length)
System.IO.File.WriteAllBytes(fname, FileBytes)