我正在尝试从指定的密钥为我们的银行创建签名,但我的结果与我从银行获得的信息不一样。谁能看到我做错了什么?
链接到bank以供参考(瑞典语文本)
示例数据位于引文标记内..:)
Filedata:“00000000”
密钥:“1234567890ABCDEF1234567890ABCDEF”
预期结果:“FF365893D899291C3BF505FB3175E880”
我的结果:“05CD81829E26F44089FD91A9CFBC75DB”
我的代码:
// Using ASCII teckentabell
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
// Using HMAC-SHA256
byte[] keyByte = encoding.GetBytes("1234567890ABCDEF1234567890ABCDEF");
HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes("00000000");
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
byte[] truncArray = new byte[16];
Array.Copy(hashmessage, truncArray, truncArray.Length);
// conversion of byte to string
string sigill = ByteArrayToString(truncArray);
// show sigill
MessageBox.Show("Sigill:\n" + sigill, "Sigill", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
答案 0 :(得分:4)
Key
是一个十六进制数字字符串,表示二进制密钥,而不是单个字符串。
要获得正确的输出,您需要将其转换为字节数组:
var key = "1234567890ABCDEF1234567890ABCDEF";
byte[] keyByte = new byte[key.Length / 2];
for (int i = 0; i < key.Length; i += 2)
{
keyByte[i / 2] = Convert.ToByte(key.Substring(i, 2), 16);
}
HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes("00000000");
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
byte[] truncArray = new byte[16];
Array.Copy(hashmessage, truncArray, truncArray.Length);