问题描述
我得到了以下HMAC密钥(十六进制格式):
I got the following HMAC key (in hexadecimal format):
52320e181a481f5e19507a75b3cae4d74d5cfbc328f7f2b738e9fb06b2e05b55b632c1c3d331dcf3baacae8d3000594f839d770f2080910b52b7b8beb3458c08
52320e181a481f5e19507a75b3cae4d74d5cfbc328f7f2b738e9fb06b2e05b55b632c1c3d331dcf3baacae8d3000594f839d770f2080910b52b7b8beb3458c08
我需要对此字符串签名:
I need to sign this string:
1100002842850CHF91827364
1100002842850CHF91827364
结果应为以下形式(十六进制格式):
The result should be this (in hexadecimal format):
2ad2f79111afd818c1dc0916d824b0a1
2ad2f79111afd818c1dc0916d824b0a1
我有以下代码:
string key = "52320e181a481f5e19507a75b3cae4d74d5cfbc328f7f2b738e9fb06b2e05b55b632c1c3d331dcf3baacae8d3000594f839d770f2080910b52b7b8beb3458c08";
string payload = "1100002842850CHF91827364";
byte[] keyInBytes = Encoding.UTF8.GetBytes(key);
byte[] payloadInBytes = Encoding.UTF8.GetBytes(payload);
var md5 = new HMACMD5(keyInBytes);
byte[] hash = md5.ComputeHash(payloadInBytes);
var result = BitConverter.ToString(hash).Replace("-", string.Empty);
但是,我没有得到结果.我在做什么错了?
However, I am not getting the result. What am I doing wrong?
推荐答案
而不是这样做:
byte[] keyInBytes = Encoding.UTF8.GetBytes(key);
您需要将密钥从十六进制字符串转换为字节数组.在这里您可以找到示例:
you need to convert key from a hex string to array of bytes. Here you can find example:
这篇关于使用C#用HMAC-MD5签名字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!