【发布时间】:2017-04-10 06:49:09
【问题描述】:
目前我是通过openssl进行这个操作,生成的文件没有问题
openssl dgst -sha256 -sign privateKey.key -out file.txt.signature file.txt
现在,我们想使用 C# 自动生成文件,但我无法得到相同的结果。
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(CreateToken("key...", "text"));
Console.ReadLine();
}
public static string CreateToken(string key, string message)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return System.Text.Encoding.UTF8.GetString(hashmessage);
}
}
我是新手,正确的方法是什么?
我没有正确检索信息吗?我应该直接从文件中获取内容吗?
非常感谢。
【问题讨论】:
-
通常你做的不仅仅是散列消息。有关攻击和防御的良好、平易近人的历史,请参阅RSA signatures and Rabin–Williams signatures: the state of the art。您应该对第 5 节 哈希随机化 感兴趣。
标签: c# openssl digital-signature sha256