C# 中使用java 的做法; 使用C#进行hmacsha256 加密
/// <summary> ///Base64Encode(HmacSHA256 华为 续费接口 Test /// </summary> /// <returns></returns> public string HuaweiTest() { return strBase64Encode(HmacSHA256("秘钥keys", "内容contents")); } /// <summary> /// HmacSHA256算法,返回的结果始终是32位 /// </summary> /// <param name="key"> 秘钥key 加密的键,可以是任何数据</param> /// <param name="content">加密内容-响应消息体 待加密的内容</param> /// <returns>加密密文</returns> public static byte[] HmacSHA256(string keys, string contents) { byte[] key = Encoding.GetEncoding("utf-8").GetBytes(keys); byte[] content = Encoding.GetEncoding("utf-8").GetBytes(contents); using (var hmacsha256 = new HMACSHA256(key)) { byte[] hashmessage = hmacsha256.ComputeHash(content); return hashmessage; } } ///编码 public static string strBase64Encode(byte[] bytes) { string encode = ""; try { encode = Convert.ToBase64String(bytes); } catch { encode = ""; } return encode; }