android应用程序内计费安全验证方法始终返回false

时间:2017-01-31 14:04:27

标签: android in-app-purchase in-app-billing

我已经构建了一个Android应用程序,它为用户提供了购买项目的能力,并且谷歌购买对话框成功返回付款,但是在活动结果中,应用程序在Security.java中失败验证方法总是返回false&# 34; sig.verify(Based64.decode(签名))"

任何帮助,为什么它发生以及我必须做什么。 我已经将应用程序发布到了Play商店,我没有使用android.test。* sku我有自己的skus

1 个答案:

答案 0 :(得分:2)

您是否正确初始化了它?

获取公钥:

public PublicKey getAPKKey(String keyFactoryAlgorithm) throws Exception{
    byte[] decodedKey = Base64.decode("...your google play public key...", Base64.DEFAULT);
    KeyFactory keyFactory = KeyFactory.getInstance(keyFactoryAlgorithm);
    return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
}

您可以在Google Play开发者控制台中找到您的公钥。

然后验证签名:

// get purchase data
Bundle ownedItems = ... query purchases
String purchaseData = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST").get(0); // just index 0 for demonstration
String signature = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE_LIST").get(0); // just index 0 for demonstration

PublicKey pkey = getAPKKey("RSA");
Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(pkey);
sig.update(purchaseData.getBytes());

if(sig.verify(Base64.decode(signature, Base64.DEFAULT))) {
    // ok
} else {
    // not ok
}