Windows Phone的ANID在C#ANID2转换? [英] Windows Phone ANID to ANID2 conversion on C#?

查看:194
本文介绍了Windows Phone的ANID在C#ANID2转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows Phone 7的呼吁ANID一个匿名用户ID属性。的Windows Phone 8已经取代与ANID2。不同的是,ANID2依赖于应用程序的发布者ID。

Windows Phone 7 had a anonymous user ID property called ANID. Windows Phone 8 has replaced that with ANID2. The difference is that ANID2 is dependent on the app's publisher ID.

这是可能的ANID转换为ANID2的为MSDN上显示下面的代码示例。只有你所需要的东西是原来的WP7 ANID和发布者ID(GUID)。问题是,该实施例是在C ++中。我一直在试图将它移植到C#,但没有成功。

It's possible to convert ANID to ANID2 as the following code sample on MSDN shows. Only things you need are the original WP7 ANID and the publisher ID (guid). The problem is that the example is in C++. I've been trying to port it to C# but with no success.

该算法本身是很容易的:

The algorithm itself is quite easy:

var data = HMAC(ANID, publisherId) // Uses SHA-256
var result = ToBase64(data)

问题是,我不能够得到的结果相匹配。我确信++创建两个应用程序(WP7和WP8),在同一设备上运行它们,然后使用GUID出版商从WP7应用程序转换ANID正常工作的温度。上的C ++所转换ANID2和从装置匹配ANID2。 。在C#转换的ANID2是别的东西。

The problem is that I'm not able to get the results match. I've made sure that the C++ works correctly by creating two apps (WP7 and WP8), running them on same devices and then converting the ANID from WP7 app using the publisher GUID. On C++ the converted ANID2 and the ANID2 from the device match. On C# the converted ANID2 is something else.

C#代码非常简单:

        var anidBytes = System.Text.Encoding.UTF8.GetBytes(this.anidBox.Text);
        var publisherGuid = Guid.Parse(this.publisherBox.Text.ToUpper());

        var macObject = new HMACSHA256(anidBytes);
        var hashed = macObject.ComputeHash(publisherGuid.ToByteArray());

        var result = Convert.ToBase64String(hashed);



C ++版本使用了一种叫CNG(加密API:下一代)。下面是它的一些代码:

The C++ version uses something called CNG (Cryptography API: Next Generation). Here's some code from it:

BCryptOpenAlgorithmProvider(&Algorithm, BCRYPT_SHA256_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
BCryptGetProperty(Algorithm,BCRYPT_OBJECT_LENGTH,reinterpret_cast<BYTE*>(&HashObjectLength),PropertyLength,&PropertyLength,0);
BCryptCreateHash(Algorithm, &Hash, HashObject, HashObjectLength, pAnidId, dwAnidLength, 0);
BCryptHashData(Hash, const_cast<BYTE*>(pPublisherId), dwPublisherIdLength, 0);
BCryptFinishHash(Hash, pUniqueId, GETDEVICEUNIQUEID_V1_OUTPUT, 0);



这之后,pUniqueId是转换器使用的Base64一些定制功能。

After which the "pUniqueId" is converter to Base64 using some custom built function.

任何帮助表示赞赏。

更新:
Visual Studio的报道说,anidBytes( C#)和pAnidId(C ++)(这是转换为字节)都有44.此长度是C#调试器如何报告的字节数组的ANID字符串:

Update: Visual Studio reports that the anidBytes (C#) and pAnidId (C++) (this is the ANID string converted to bytes) both have length of 44. This is how C# debugger reports the byte array:

和这里的C ++调试:

And here's the C++ debugger:

我不知道C ++,所以我不知道,如果这两个是相同的。他们是,但C ++每个之后这些\0字符,我不知道这是确定。我想是的,在这两种情况下的长度被报告为44。

I don't know C++ so I'm not sure if these two are identical. They are, but the C++ has these '\0' chars after each and I'm not sure if that's OK. I think it is, as the length in both cases is reported as 44.

另一个字节数组的比较是publisherGuid(C#)VS pPublisherId(C ++)(发布者ID为GUID)。我想,他们再次匹配。 C#中:

Another byte array comparison is the publisherGuid (C#) vs pPublisherId (C++) (publisher id as GUID). I think they again match. C#:

C ++:

如果我再看看输出值已被加密的之后,我可以看到一个区别:

If I then look at the output after the value has been crypted, I can see a difference:

在C#我得到这个代码的输出:

On C# I get the output from this code:

        var macObject = new HMACSHA256(anidBytes);
        var hashed = macObject.ComputeHash(publisherBytes);

和字节数组看起来像这样:

And the byte array looks like this:

在C ++代码,如果我检查pUniqueId(BCryptFinishHash的结果),我看到:

On C++ code, if I check the pUniqueId (result of BCryptFinishHash), I see this:

C

长度似乎是在这两种情况下相同,但结果并非如此。

The length seems to be the same in both of these cases but the outcome isn't.

在C#中,如果我从UTF8更改编码类型为Unicode的anidBytes字节数组改变这一点:

On C#, if I change the encoding type from UTF8 to Unicode, the anidBytes byte array changes to this:

所以它等同于什么C ++调试器显示。也是结果的变化,但它仍然从C ++不同。这里是新的C#的结果:

So it's identical to what C++ debugger shows. Also the result changes but it still differs from C++. Here's the new C# outcome:

这是从C ++正确的结果:

This is the correct result from C++:

推荐答案

请确保你切开ANID字节数组一半您使用它作为HMACSHA256密钥之前。转换算法只需要前44个字节,而不是整个88不要问我为什么,但据我所知,如果你看一下示例代码在的 http://code.msdn.microsoft.com/wpapps/ANID-to- ANID2 - 转换 - cc428038 - 他们必须为CHAR误WCHAR

Make sure you cut the ANID byte array into half before you use it as the secret key for the HMACSHA256. The conversion algorithm only needs the first 44 bytes and not the entire 88. Don't ask me why, but AFAIK, it's a bug in the C++ code they use when converting ANID to ANID2 if you look at the sample code at http://code.msdn.microsoft.com/wpapps/ANID-to-ANID2-Converter-cc428038 - they must have mistaken wchar for char!

这篇关于Windows Phone的ANID在C#ANID2转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆