【发布时间】:2012-10-25 14:30:33
【问题描述】:
我在 Java 中有以下代码:
byte[] secretKey = secretAccessKey.getBytes("UTF-8");
SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte[] bytes = data.getBytes("UTF-8");
byte[] rawHmac = mac.doFinal(bytes);
String result = javax.xml.bind.DatatypeConverter.printBase64Binary(rawHmac);
以及以下 C# 代码:
UTF8Encoding enc = new UTF8Encoding();
byte[] secretKey = enc.GetBytes(secretAccessKey);
HMACSHA256 hmac = new HMACSHA256(secretKey);
hmac.Initialize();
byte[] bytes = enc.GetBytes(data);
byte[] rawHmac = hmac.ComputeHash(bytes);
string result = Convert.ToBase64String(rawHmac);
字节数组“secretKey”和“bytes”是等价的,但字节数组“rawHmac”不同,字符串“result”不同。有谁知道为什么?
【问题讨论】:
-
还应该说“data”和“secretKey”是等价的字符串
标签: c# java encryption hmac sha256