問題描述
我有一個提供應用內計費的 Android 應用程序,我們有我們的應用程序服務器,android 應用程序連接到該應用程序服務器以向用戶提供服務,在應用程序內購買時,我們希望將收據推送到服務器以進行驗證過程.
I have a Android application which provides in-app billing and we have our application server to which android application connects to provide services to the user, on in-app purchase we want to push receipt to the server for verification process.
現在的問題是我不知道如何轉換 Security.java 文件在 dot net(C#) 中,因為我們的服務器是用 dot net 編寫的
Now problem is I don't know how to convert Security.java file in dot net(C#) as our server is written in dot net
注意:此文件與提供消息簽名功能的 android 應用內計費相同的應用程序一起提供,我只需要它們在 dot net 中的等價物.
NOTE: This file comes with android in-app billing same application which provides message signing functions i just need their equivalent in dot net.
有關此問題的更多詳細信息,請訪問http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/66bb5683-fde6-47ca-92d7-de255cc8655a
More Detail regarding this problem is available at http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/66bb5683-fde6-47ca-92d7-de255cc8655a
推薦答案
我找到了解決方案,要實現你首先必須轉換公鑰格式,因為 dot net 使用排序不同的 Key 作為輸入.
I found the solution, to achieve you first have to convert the public key format as dot net uses sort of different Key as an input.
我不知道其他方法,但我們可以使用 java 代碼獲取點網格式密鑰,您只需運行一次即可生成點網友好的 RSA 公鑰.(僅在給定公眾沒有快速變化時才建議這樣做,例如在 Android 市場應用內計費的情況下)
I don't know the other ways but we can get dot net format key using a java Code which you have to run only once to generate the dot net friendly RSA Public Key. (this is only recommended when the given public do not changes rapidly e.g. in case of Android market in-app billing)
以下 Java 代碼對我有用
following Java Code worked for me
public static DotNetRSA GenerateDotNetKey(String base64PubKey)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException {
/*
* String base64PubKey -
* Is a Key retrieved from Google Checkout Merchant Account
*/
BASE64Decoder decoder = new BASE64Decoder();
byte[] publicKeyBytes = decoder.decodeBuffer(base64PubKey);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
RSAPublicKey publicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(publicKeySpec);
byte[] modulusBytes = publicKey.getModulus().toByteArray();
byte[] exponentBytes = publicKey.getPublicExponent().toByteArray();
modulusBytes = stripLeadingZeros(modulusBytes);
BASE64Encoder encoder = new BASE64Encoder();
String modulusB64 = encoder.encode(modulusBytes);
String exponentB64 = encoder.encode(exponentBytes);
return new DotNetRSA(modulusB64, exponentB64);
}
private static byte[] stripLeadingZeros(byte[] a) {
int lastZero = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == 0) {
lastZero = i;
}
else {
break;
}
}
lastZero++;
byte[] result = new byte[a.length - lastZero];
System.arraycopy(a, lastZero, result, 0, result.length);
return result;
}
現在要驗證數字簽名,您可以在您的點網程序(c#)中使用以下代碼,前提是 GCHO_PUB_KEY_EXP 是您的指數,GCHO_PUB_KEY_MOD 是您通過上述 Java 代碼提取的模數
Now to verify the Digital Signature you can use the following code in your dot net program(c#) provided GCHO_PUB_KEY_EXP is your Exponent and GCHO_PUB_KEY_MOD is your Modulus extracted by above Java Code
public static bool VerifyDataSingature(string data, string sign)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
RSAParameters rsaKeyInfo = new RSAParameters()
{
Exponent = Convert.FromBase64String(GCHO_PUB_KEY_EXP),
Modulus = Convert.FromBase64String(GCHO_PUB_KEY_MOD)
};
rsa.ImportParameters(rsaKeyInfo);
return rsa.VerifyData(Encoding.ASCII.GetBytes(data),
"SHA1",
Convert.FromBase64String(sign));
}
}
我希望它對每個人都適用,就像對我一樣.謝謝
I hope it will work for everyone as worked for me. Thanks
歸功于 Code Project Artical
這篇關于點網中的Android應用內計費驗證收據(C#)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!