关于C# 的hmacSHA1加密方法的使用
简短的来说:我发送http请求要从人家那获取一些信息,在http的header里面增加一些参数,其中就有特定的的参数需要用hmacSHA1来加密。
对方提供了JAVA的SDK,但没有C#的,java的主要加密实现为:
public static String generateMacSignature( String secret,String data) {
logger.debug("generateDate is:"+data);
logger.debug("secret is:"+secret);
byte[] byteHMAC = null;
try {
Mac mac = Mac.getInstance(HMAC_SHA1);
SecretKey secretKey = new SecretKeySpec(secret.getBytes("utf-8"),
HMAC_SHA1);
mac.init(secretKey);
byteHMAC = mac.doFinal(data.getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String result = new BASE64Encoder().encode(byteHMAC);
logger.debug("generateMacSignature is:"+result);
return result;
}
我从网上找了些相关资料,换成C#的为:
public static string HMACSHA1Encrypt(string EncryptText,string EncryptKey)
{
HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(EncryptKey));
byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(EncryptText));
//CryptoStream CStream = new CryptoStream(Stream.Null, myHMACSHA1, CryptoStreamMode.Write);
//CStream.Write(RstRes, 0, RstRes.Length);
StringBuilder EnText = new StringBuilder();
foreach (byte Byte in RstRes)
{
EnText.AppendFormat("{0:x2}", Byte);
}
return EnText.ToString();
}
但加密出来的结果始终是不对的,自然回来的信息也是:加密错误。
请问哪位大侠知道C#的hmacSHA1加密正确的方式是怎么样的?我已经在google上搜遍了
对方提供了JAVA的SDK,但没有C#的,java的主要加密实现为:
public static String generateMacSignature( String secret,String data) {
logger.debug("generateDate is:"+data);
logger.debug("secret is:"+secret);
byte[] byteHMAC = null;
try {
Mac mac = Mac.getInstance(HMAC_SHA1);
SecretKey secretKey = new SecretKeySpec(secret.getBytes("utf-8"),
HMAC_SHA1);
mac.init(secretKey);
byteHMAC = mac.doFinal(data.getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String result = new BASE64Encoder().encode(byteHMAC);
logger.debug("generateMacSignature is:"+result);
return result;
}
我从网上找了些相关资料,换成C#的为:
public static string HMACSHA1Encrypt(string EncryptText,string EncryptKey)
{
HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(EncryptKey));
byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(EncryptText));
//CryptoStream CStream = new CryptoStream(Stream.Null, myHMACSHA1, CryptoStreamMode.Write);
//CStream.Write(RstRes, 0, RstRes.Length);
StringBuilder EnText = new StringBuilder();
foreach (byte Byte in RstRes)
{
EnText.AppendFormat("{0:x2}", Byte);
}
return EnText.ToString();
}
但加密出来的结果始终是不对的,自然回来的信息也是:加密错误。
请问哪位大侠知道C#的hmacSHA1加密正确的方式是怎么样的?我已经在google上搜遍了
...全文
相关推荐
HMAC-SHA1 算法签名 亲测可用
目前很多应用的SDK都开始需要Authorization(鉴权),然网络浩瀚,杂乱无章。终于找到了可用的方案,已为腾讯官方做了一个C#的SDK,大家放心copy。
c#关于JWT跨域身份验证的实现代码
主要介绍了c#关于JWT跨域身份验证的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
微信小程序支付C#后端
主要为大家详细介绍了微信小程序支付C#后端源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
顺丰丰桥对接案例示例.rar
采用c#语言编写的调顺丰丰桥接口的案例,注释详细,案例非常容易上手,对于要做丰桥对接的新手来说很是适用
c#考试专用
c#大学考试用


