Android In-App结算错误InvalidKeySpecException

时间:2015-09-06 22:36:22

标签: android runtime-error in-app-billing

我正在尝试使用Trivial Gas教程的包装器来实现应用程序内购买。成功购买完成后应用程序崩溃,从那时起应用程序开始崩溃。

java.lang.IllegalArgumentException: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D07209B:asn1 encoding routines:ASN1_get_object:too long
at com.nightowl.memory.Security.generatePublicKey(Security.java:85)
at com.nightowl.memory.Security.verifyPurchase(Security.java:65)
at com.nightowl.memory.IabHelper.queryPurchases(IabHelper.java:875)
at com.nightowl.memory.IabHelper.queryInventory(IabHelper.java:550)
at com.nightowl.memory.IabHelper.queryInventory(IabHelper.java:528)
at com.nightowl.memory.IabHelper$2.run(IabHelper.java:623)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D07209B:asn1 encoding routines:ASN1_get_object:too long
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:101)
at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
at com.nightowl.memory.Security.generatePublicKey(Security.java:80)
... 6 more
Caused by: java.lang.RuntimeException: error:0D07209B:asn1 encoding routines:ASN1_get_object:too long
at com.android.org.conscrypt.NativeCrypto.d2i_PUBKEY(Native Method)
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:99)
... 9 more

我使用以下代码来调用它:

    String base64EncodedPublicKey;
            base64EncodedPublicKey= String.valueOf(R.string.myPubKey);
            additionalSkuList = new ArrayList<String>();
            for(int i=0;i<3;i++)
            {
                for(int j=0;j<4;j++)
                {
                    if(i==0&&j>1)
                        break;
                    additionalSkuList.add(id[i][j]);
                }
            }
            mHelper = new IabHelper(this, base64EncodedPublicKey);
            mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
                public void onIabSetupFinished(IabResult result) {
                    Log.d("main", "m here");
                    if (!result.isSuccess()) {
                        // Oh noes, there was a problem.
                        Log.d("main", "Problem setting up In-app Billing: " + result);
                    }
                    // Hooray, IAB is fully set up!
                    isIAB = true;

                    mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
                    Log.d("main", "" + isIAB + " " + isLoad);
                }
            });


IabHelper.QueryInventoryFinishedListener
            mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result, Inventory inventory)
        {
            Log.d("main", "m here too");
            if (result.isFailure()) {
                // handle error
                return;
            }
            for(int i=0;i<3;i++)
            {
                for(int j=0;j<4;j++)
                {
                    if(i==0&&j>1)
                        break;
                    price[i][j]=inventory.getSkuDetails(id[i][j]).getPrice();
                }
            }
            isLoad=true;
            data.setPrice(price);
            data.setDataLoad(true);
            // update the UI
        }
    };

    public void onPurchaseCall()
    {
        int loc[] = data.getItem();
        mHelper.launchPurchaseFlow(this, id[loc[0]][loc[1]], 100*loc[0]+loc[1],mPurchaseFinishedListener, id[loc[0]][loc[1]]);
    }
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
            = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase)
        {
            if (result.isFailure()) {
                Log.d("main", "Error purchasing: " + result);
                return;
            }
            else if (purchase.getSku().equals(id[0][0])) {
                data.setNoads(1);
                mAdView.setVisibility(View.INVISIBLE);
                data.setNotificationState(1);
                data.setNoOfnotifications(2);
                data.setNotificationMsg(0, "Purchase Complete");
                data.setNotificationMsg(1, PurchaseMsg[0]);
                // consume the gas and update the UI
            }


        }
    };

任何人都可以帮我解决吗?我从日志中猜到的是我提供的公钥存在问题。但它第一次起作用(直到我成功购买才开始工作)。我还得到了所有应用内商品的价目表。

我尝试用测试帐户测试购买。成功完成购买后,出现此错误,从那时起该应用程序未针对该帐户运行。

2 个答案:

答案 0 :(得分:0)

不要使用getResources().getString(R.string.myPubKey_data)),它会给你一个错误的价值。而是使用<input type="number" /> 来获取您的公钥字符串。

答案 1 :(得分:0)

虽然问题很老,但我写的答案对任何人都会有这个问题。 问题来自公钥长度。它无效且比它应该更长。确保你输入正确。