支付签名一直错误,按照签名步骤校验:第一步 k = v 正确对照,第二步链接商户key也是对的,到第三步生成sign ( HMACSHA256加密)与校验签名工具得出的sign 不对照,不知道哪里出了问题,使用了很多种HMACSHA256加密代码:
public static string GetHash(string message, string secret)
{
//第一种
secret = secret ?? "";
byte[] keyByte = Encoding.GetEncoding("UTF-8").GetBytes(secret);
byte[] messageBytes = Encoding.GetEncoding("UTF-8").GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
string result = BitConverter.ToString(hashmessage);
result = result.Replace("-", "");
return result.ToUpper();
}
//第二种
//byte[] clearBytes = Encoding.UTF8.GetBytes(message);
//byte[] keyByte = Encoding.UTF8.GetBytes(secret);
//SHA256 sha256 = new SHA256Managed();
//sha256.ComputeHash(clearBytes);
//byte[] hashedBytes = sha256.Hash;
//sha256.Clear();
//string output = BitConverter.ToString(hashedBytes).Replace("-", "");
//return output.ToUpper();
//第三种
//string result = "";
//var enc = Encoding.Default;
//byte[]
//baText2BHashed = enc.GetBytes(message),
//baSalt = enc.GetBytes(secret);
//System.Security.Cryptography.HMACSHA256 hasher = new HMACSHA256(baSalt);
//byte[] baHashedText = hasher.ComputeHash(baText2BHashed);
//result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray());
//return result.ToUpper();
}
求各位大神看看,有什么不对的帮我解答一下。
结贴:官方说的都是对的,
容易出错的点:
1.参数按照key=value的格式拼接,为str
2.HMACSHA256加密 生成sign 后转为大写,ps:Hmac_SHA256.GetHash(str.ToString(), mchkey)传参不要传错
第三种改成UTF-8试试
另外检查下API秘钥是否正确
key:123
data:123
计算后:3CAFE40F92BE6AC77D2792B4B267C2DA11E3F3087B93BB19C6C5133786984B44