简体   繁体   English

将Flash转换为C#时出错

[英]Error when converting Flash to C#

I have the following method in Flash which writes two ByteArrays and then base64 encodes them 我在Flash中有以下方法,该方法写入两个ByteArrays ,然后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);
}

In my C#, I have: 在我的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);
}

But, I get a different result set from the Flash version compared to the C# version. 但是,与C#版本相比,我从Flash版本获得了不同的结果集。 Can anyone spot anything that is obviously wrong? 谁能发现明显错误的东西?

EDIT 编辑

Here is the ByteToString method: 这是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;
}

It looks pretty much the same; 看起来几乎一样; that said, I would suggest using Convert.ToBase64String instead of your ByteToString method, the string concatenation in there is generally considered very bad practice. 也就是说,我建议您使用Convert.ToBase64String而不是ByteToString方法,那里的字符串连接通常被认为是非常不好的做法。

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). 我也不是100%知道Base 64编码是否在逻辑上与将每个字节的byte.ToString("X2")附加到字符串相同(尽管可以)。

Other than that, it shouldn't be too difficult to debug and figure out where the two start to deviate... 除此之外,调试和弄清两者开始偏离的地方应该不会太困难...

Your ByteToString function merely returns a hexadecimal version of the signature. 您的ByteToString函数仅返回签名的十六进制版本。 Your flash version base 64 encodes it. 您的Flash版本基于64。 There's a big difference between converting something to a hexadecimal string, and base 64 encoding. 将某物转换为十六进制字符串与以64位编码之间存在很大的区别。

Use Convert.ToBase64String instead of ByteToString. 使用Convert.ToBase64String而不是ByteToString。

Flash might not be emitting the BOM (Byte Order Mark), where C# (.Net) does. Flash可能不会发出C#(.Net)发出的BOM(字节顺序标记)。

public static readonly Encoding Utf8NoBom = new UTF8Encoding(false);

According to the Flash Documentation you are using the correct encoding on the C# end; 根据Flash文档,您在C#端使用了正确的编码。 so this is the only possible alternative (it didn't have anything to say about the BOM). 因此,这是唯一可能的选择(它没有关于BOM的任何信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM