gpt4 book ai didi

c# - 验证失败(C# .NET HMACSHA256 类)

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:43 25 4
gpt4 key购买 nike

我正在尝试学习 .NET API,并且我创建了一个程序来将源文件中的 key 与 XML 文件中的 key 进行比较。

我使用了以下示例(验证文档的第三种方法:

https://learn.microsoft.com/en-gb/dotnet/api/system.security.cryptography.hmacsha256?view=netframework-4.7.1

现在我的程序正在运行,但它总是说文件已被篡改,尽管我绝对确定它们没有被篡改,因为我刚刚创建了它们。

这是我的代码:

验证文档.cs

using System;
using System.IO;
using System.Security.Cryptography;
using System.Xml.Serialization;

public class VerifyDocument
{

public static void Main(string[] args)
{

XmlSerializer xml = new XmlSerializer(typeof(byte[]));
byte[] key;
string keyFile = args[1];
string sourceFile = args[0];
using (StreamReader reader = new StreamReader(keyFile)) {
key = (byte[]) xml.Deserialize(reader);
}

bool err = false;

using (HMACSHA256 hmac = new HMACSHA256(key)) // Initialize the keyed hash object.
{

byte[] storedHash = new byte[hmac.HashSize / 8]; // Create an array to hold the keyed hash value read from the file.

using (FileStream inStream = new FileStream(sourceFile, FileMode.Open)) // Create a FileStream for the source file.
{

inStream.Read(storedHash, 0, storedHash.Length); // Read in the storedHash.

byte[] computedHash = hmac.ComputeHash(inStream);
// compare the computed hash with the stored value

for (int i = 0; i < storedHash.Length; i++)
{
if (computedHash[i] != storedHash[i])
{
err = true;
}
}
}
}
if (err)
{
Console.WriteLine("Hash values differ! Signed file has been tampered with!");

}
else
{
Console.WriteLine("Hash values agree -- no tampering occurred.");

}

}

}

签名文档.cs

using System;
using System.IO;
using System.Security.Cryptography;
using System.Xml.Serialization;

public class HMACSHA256example
{

public static void Main(string[] args)
{


if (args.Length != 2) {
Console.WriteLine("Usage: [mono] SignDocument.exe <filename> <key>");
Environment.Exit(1);
} else
{
XmlSerializer xml = new XmlSerializer(typeof(byte[]));
byte[] key;
string keyFile = args[1];
string sourceFile = args[0];
string destFile = sourceFile + ".hash";
using (StreamReader reader = new StreamReader(keyFile)) {
key = (byte[]) xml.Deserialize(reader);
}

using (HMACSHA256 hmac = new HMACSHA256(key)) // Initialize the keyed hash object.
{
using (FileStream inStream = new FileStream(sourceFile, FileMode.Open))
{
using (FileStream outStream = new FileStream(destFile, FileMode.Create))
{

byte[] hashValue = hmac.ComputeHash(inStream); // Compute the hash of the input file.


outStream.Write(hashValue, 0, hashValue.Length); // Write the computed hash value to the output file.

}
}

}

}
}
}

创建 key .cs

using System;
using System.IO;
using System.Security.Cryptography;
using System.Xml.Serialization;

namespace COMP3911.Crypto {


class CreateKey {
static void Main(string[] args) {

string input;

if (args.Length == 0) {
Console.WriteLine("Usage: [mono] CreateKey.exe <filename>");
Environment.Exit(1);
}

byte[] secretkey = new Byte[64];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
// The array is now filled with cryptographically strong random bytes.
rng.GetBytes(secretkey);
}


// XML
string keyfile = args[0] + ".key";
using (StreamWriter output = new StreamWriter(keyfile, false)) {
XmlSerializer xml = new XmlSerializer(typeof(byte[]));
xml.Serialize(output, secretkey);
}
}
}

}

如有任何帮助,我们将不胜感激!

最佳答案

您的“签名”程序读取 inStream 并将 HMAC 写入 outStream

您的“验证”程序读取一个文件并期望它是 HMAC 后跟数据。

要么“签名”需要倒回inStream,然后将其复制到outStream,要么您需要更改“验证”以独立查看两个文件。

关于c# - 验证失败(C# .NET HMACSHA256 类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46832549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com