在使用 C#.NET 2.0(在 Vista 上)的 WinForm 应用程序中,我使用 SHA1 哈希从字符串创建哈希并将哈希存储在文本文件中(使用 UTF-8 编码)。我想在一个条件下使用存储在文本文件中的哈希值。当我在 Vista 中运行该项目时,它可以正常工作(即条件结果为真),但是当我在 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 中出现故障。如果有一些逻辑问题,它不应该在任何操作系统中工作。
谢谢。