我已实施IAP结算。但我得到的问题是在购买产品后,应用程序崩溃了。说错误无效的密钥规范。但付款是成功的。当我浏览网页时,我发现密钥有问题。这是我通过注销跟踪出现错误的代码。
这里我提供了我从市场页面获得的 base64EncodedPublicKey(MIIBI **************等)
PublicKey key = BillingSecurity.generatePublicKey(base64EncodedPublicKey);
verified = BillingSecurity.verify(key, signedData, signature);
if (!verified) {
Log.w(TAG, "signature does not match data.");
return null;
}
try {
byte[] decodedKey = Base64.decode(encodedPublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
Log.e(TAG, "Invalid key specification.");
throw new IllegalArgumentException(e);
} catch (Base64DecoderException e) {
Log.e(TAG, "Base64DecoderException.", e);
return null;
} catch (Exception g)
{
Log.e(TAG, "Exception generate Public key.", g);
return null;
}
注意:我能够收到产品购买详情和应用程序崩溃后。
请在这里帮助我。提前致谢。
答案 0 :(得分:0)
/**
* Generates a PublicKey instance from a string containing the
* Base64-encoded public key.
*
* @param encodedPublicKey Base64-encoded public key
* @throws IllegalArgumentException if encodedPublicKey is invalid
*/
public static PublicKey generatePublicKey(String encodedPublicKey) {
try {
String str = new String(Base64.decode(encodedPublicKey));
byte[] decodedKey = Base64.decode(str);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
Log.e(TAG, "Invalid key specification.");
throw new IllegalArgumentException(e);
} catch (Base64DecoderException e) {
Log.e(TAG, "Base64 decoding failed.");
throw new IllegalArgumentException(e);
}
}