将 Flash 转换为 C# 时出错
我在 Flash 中有以下方法,它写入两个 ByteArray,然后对它们进行 base64 编码
private function generateSignature(data:String, secretKey:String):String {
var secretKeyByteArray:ByteArray = new ByteArray();
secretKeyByteArray.writeUTFBytes(secretKey);
secretKeyByteArray.position = 0;
var dataByteArray:ByteArray = new ByteArray();
dataByteArray.writeUTFBytes(data);
dataByteArray.position = 0;
var hmac:HMAC = new HMAC(new SHA1());
var signatureByteArray:ByteArray = hmac.compute(secretKeyByteArray, dataByteArray);
return Base64.encodeByteArray(signatureByteArray);
}
在我的 C# 中,我有:
string GenerateSignature(string secretKey, string base64Policy)
{
byte[] secretBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(base64Policy);
HMACSHA1 hmac = new HMACSHA1(secretBytes);
byte[] signature = hmac.ComputeHash(dataBytes);
return ByteToString(signature);
}
但是,与 C# 版本相比,我从 Flash 版本获得了不同的结果集。谁能发现任何明显错误的地方吗?
编辑
这是 ByteToString 方法:
static string ByteToString(byte[] buffer)
{
string binary = string.Empty;
for (int i = 0; i < buffer.Length; i++)
{
binary += buffer[i].ToString("X2"); // Hex Format
}
return binary;
}
I have the following method in Flash which writes two ByteArrays
and then base64 encodes them
private function generateSignature(data:String, secretKey:String):String {
var secretKeyByteArray:ByteArray = new ByteArray();
secretKeyByteArray.writeUTFBytes(secretKey);
secretKeyByteArray.position = 0;
var dataByteArray:ByteArray = new ByteArray();
dataByteArray.writeUTFBytes(data);
dataByteArray.position = 0;
var hmac:HMAC = new HMAC(new SHA1());
var signatureByteArray:ByteArray = hmac.compute(secretKeyByteArray, dataByteArray);
return Base64.encodeByteArray(signatureByteArray);
}
In my C#, I have:
string GenerateSignature(string secretKey, string base64Policy)
{
byte[] secretBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] dataBytes = Encoding.UTF8.GetBytes(base64Policy);
HMACSHA1 hmac = new HMACSHA1(secretBytes);
byte[] signature = hmac.ComputeHash(dataBytes);
return ByteToString(signature);
}
But, I get a different result set from the Flash version compared to the C# version. Can anyone spot anything that is obviously wrong?
EDIT
Here is the ByteToString
method:
static string ByteToString(byte[] buffer)
{
string binary = string.Empty;
for (int i = 0; i < buffer.Length; i++)
{
binary += buffer[i].ToString("X2"); // Hex Format
}
return binary;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来几乎一样;也就是说,我建议使用
Convert.ToBase64String
而不是ByteToString
方法,其中的字符串连接通常被认为是非常糟糕的做法。我也不能 100% 确定 Base 64 编码在逻辑上是否与将每个字节附加到字符串中的
byte.ToString("X2")
相同(尽管很可能是这样)。除此之外,调试并找出两者开始偏离的位置应该不会太困难......
It looks pretty much the same; that said, I would suggest using
Convert.ToBase64String
instead of yourByteToString
method, the string concatenation in there is generally considered very bad practice.I am also not 100% sure if Base 64 encoding is logically the same as appending
byte.ToString("X2")
for each byte to a string (although it could well be).Other than that, it shouldn't be too difficult to debug and figure out where the two start to deviate...
您的 ByteToString 函数仅返回签名的十六进制版本。您的 Flash 版本采用 Base 64 对其进行编码。将某些内容转换为十六进制字符串与 Base 64 编码之间存在很大差异。
使用
Convert.ToBase64String
而不是 ByteToString。Your ByteToString function merely returns a hexadecimal version of the signature. Your flash version base 64 encodes it. There's a big difference between converting something to a hexadecimal string, and base 64 encoding.
Use
Convert.ToBase64String
instead of ByteToString.Flash 可能不会发出 BOM(字节顺序标记),而 C# (.Net) 会发出 BOM。
根据 Flash 文档,您是在 C# 端使用正确的编码;所以这是唯一可能的选择(它没有关于 BOM 的任何内容)。
Flash might not be emitting the BOM (Byte Order Mark), where C# (.Net) does.
According to the Flash Documentation you are using the correct encoding on the C# end; so this is the only possible alternative (it didn't have anything to say about the BOM).