Vista 和 XP 中的 SHA1 哈希值不同

发布于 2024-08-08 08:10:45 字数 1042 浏览 4 评论 0原文

在使用 C#.NET 2.0(在 Vista 上)的 WinForm 应用程序中,我使用 SHA1 哈希从字符串创建哈希并将哈希存储在文本文件中(使用 UTF-8 编码)。我想在条件中使用存储在文本文件中的哈希值。当我在 Vista 中运行该项目时,它可以正常运行(即条件结果为 true),但是当我在 XP 中运行时,该项目无法运行。

Vista 和 XP 中创建哈希的方式有什么不同吗?

代码摘录

byte[] HashValue;
byte[] MessageBytes = Encoding.UTF8.GetBytes(strPlain);
SHA1Managed SHhash = new SHA1Managed();
StringBuilder strHex = new StringBuilder("");
HashValue = SHhash.ComputeHash(MessageBytes);
foreach (byte b in HashValue)
{
    strHex.AppendFormat("{0:x2}", b);
}
// storing strHex in a text file with UTF-8 encoding

测试条件

string newHash = Program.GetHash("This will be hashed.");
// GetHash() does has the same code as above, but instead of storing hash in file in return
// hash.
bool validHash = newHash.Equals(oldHash);
// old has is the one stored in file
if (validHash)
{
    // some code
}

[编辑]

主要问题是相同的代码 在 Vista 中工作正常,但出现故障 在XP中。如果有一些逻辑 问题是它不应该在任何操作系统中工作。

谢谢。

In a WinForm application using C#.NET 2.0 (on Vista), I am using SHA1 hash to create a hash from a string and store the hash in a text file (with UTF-8 encoding). I want to use the hash stored in text file to in a condition. When I run the project in Vista it works properly (i.e. the condition results in true), but when I run in XP the project does not run.

Is the way hash created in Vista different from XP?

Code extract

byte[] HashValue;
byte[] MessageBytes = Encoding.UTF8.GetBytes(strPlain);
SHA1Managed SHhash = new SHA1Managed();
StringBuilder strHex = new StringBuilder("");
HashValue = SHhash.ComputeHash(MessageBytes);
foreach (byte b in HashValue)
{
    strHex.AppendFormat("{0:x2}", b);
}
// storing strHex in a text file with UTF-8 encoding

Test condition

string newHash = Program.GetHash("This will be hashed.");
// GetHash() does has the same code as above, but instead of storing hash in file in return
// hash.
bool validHash = newHash.Equals(oldHash);
// old has is the one stored in file
if (validHash)
{
    // some code
}

[Edit]

The main problem is the same code
works fine in Vista, but breaks down
in XP. If there is some logical
problem it should not work in any OS.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画离情绘悲伤 2024-08-15 08:10:45

您如何在机器之间传递二进制文件?当我在计算机上准备 ClickOnce 包时,我曾经在使用 7zip 提供的最大压缩模式压缩二进制文件并在另一端使用 winzip 解压缩时遇到哈希验证问题。

How are you passing the binaries between the machines? I once encountered a hash validation problem when zipping the binaries with the maximum compression mode 7zip offers and unzipping it with winzip on the other side, when I was preparing a ClickOnce package on my machine.

始终不够 2024-08-15 08:10:45

我很好奇为什么您提到与将哈希值存储在文本文件中相关的 UTF-8 编码。您是否尝试存储原始数据字节(以某种方式转换为 UTF-8),或者存储哈希值的十六进制表示形式?

通常,在文本文件中存储哈希值时,您会使用十六进制表示,例如:

3e2f9d9069abd6ace2cb18f7390a06c034a0f9dd

不需要专门使用 UTF-8 编码,因为上面是普通的 ASCII。

I am curious why you mention UTF-8 encoding in relation to storing the hash value in a text file. Are you attempting to store the raw data bytes, somehow converted to UTF-8, or are you storing a hexadecimal representation of the hash value?

Normally when storing a hash value in a text file, you would use the hex representation, such as:

3e2f9d9069abd6ace2cb18f7390a06c034a0f9dd

There would be no need to specifically use UTF-8 encoding, since the above is plain ordinary ASCII.

悲喜皆因你 2024-08-15 08:10:45

我怀疑文件中存储的旧哈希可能不正确。在每台机器上尝试一个简单的控制台应用程序片段。例如:

Console.WriteLine(Program.GetHash("This will be hashed."));

如果这些确实给出了相同的结果,那么它一定与比较例程有关(即可能是上面提到的 oldHash)。

还有一件事需要注意;我看到您正在使用 bool validHash 来存储比较结果,但之后检查 bool validSource 。这只是一个错误吗?

I suspect that the old hash stored in the file might be incorrect. Try out a simple console application snippet on each machine. Something like:

Console.WriteLine(Program.GetHash("This will be hashed."));

If these are indeed giving the same result then it must be something to do with the comparison routine (i.e. likely oldHash mentioned above).

One other thing to note; I see you're using bool validHash to store the comparison results however checking the boolean validSource afterwards. Is this just a mistype?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文