简体   繁体   English

Android In-App Billing Error InvalidKeySpecException

[英]Android In-App Billing Error InvalidKeySpecException

I am trying to implement the In-App Purchase using the wrapper of Trivial Gas tutorial. 我正在尝试使用Trivial Gas教程的包装器来实现应用程序内购买。 After completion of successful purchase The app crashed, since then the app is getting crashed at the beginning. 成功购买完成后应用程序崩溃,从那时起应用程序开始崩溃。

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

I have used the following code to call it: 我使用以下代码来调用它:

    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
            }


        }
    };

Can anyone help me to solve it? 任何人都可以帮我解决吗? What I guessed from the log is that something problem with the Public Key I provide. 我从日志中猜到的是我提供的公钥存在问题。 But the first time it worked (It was working till I initiated a successful purchase). 但它第一次起作用(直到我成功购买才开始工作)。 And I was also getting the price list of all my in-app products. 我还得到了所有应用内商品的价目表。

I tried to test the purchase with a testing account. 我试图用测试帐户测试购买。 On successful completion of the purchase this error came and since then the app is not running for that account. 成功完成购买后出现此错误,从那时起该应用程序未针对该帐户运行。

Don't use String.valueOf(R.string.myPubKey) , it will give you a wrong value. 不要使用String.valueOf(R.string.myPubKey) ,它会给你一个错误的值。 Instead use getResources().getString(R.string.myPubKey_data)) to get your public key string. 而是使用getResources().getString(R.string.myPubKey_data))来获取您的公钥字符串。

Although the question is old, but i write answer for anyone will have this problem. 虽然问题很老,但我写的答案对任何人都会有这个问题。 The problem is from public key length. 问题来自公钥长度。 It's not valid and is longer than it should be. 它无效且比它应该更长。 Make sure you entered it correctly. 确保您输入正确。

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

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