2

我正在制作带有应用内购买的 Android 应用程序。在Android 开发者中心页面上,我看到我必须使用签名验证购买数据(json)。我试图为此使用Google Code 中的 PHP 工具,但验证失败。首先失败的是这个库不是我想要的 json (据我所知),而是一些带有字段的纯文本,用:and连接|。它也拆分了这个纯字符串以获取packageName和验证它。我评论了这部分代码,因为下一部分更有趣:

$result = openssl_verify($responseData, base64_decode($signature),
  $this->_publicKey, self::SIGNATURE_ALGORITHM);

//openssl_verify returns 1 for a valid signature
if (0 === $result) {
    return false;
} else if (1 !== $result) {
    require_once 'RuntimeException.php';
    throw new AndroidMarket_Licensing_RuntimeException('Unknown error verifying the signature in openssl_verify');
}

$responseData我的购买 json在哪里,self::SIGNATURE_ALGORITHMOPENSSL_ALGO_SHA1$this->_publicKey是:

$key = self::KEY_PREFIX . chunk_split($publicKey, 64, "\n") . self::KEY_SUFFIX;
$key = openssl_get_publickey($key);
if (false === $key) {
    require_once 'InvalidArgumentException.php';
    throw new AndroidMarket_Licensing_InvalidArgumentException('Please pass a Base64-encoded public key from the Market portal');
}
$this->_publicKey = $key;

其中公钥是 base64 公钥,如下所述:

Note:To find the public key portion of this key pair, open your application's
details in the Developer Console, then click on Services & APIs, and look at the
field titled Your License Key for This Application.

但是这样的验证是失败的。我读到 API 3 是新的(2012 年 12 月),并且许多其他文章和教程与它不对应。我需要更改什么来更正此验证?

此代码使用 SHA1,但在 Android 开发人员中心页面(第一个链接)上描述了公钥是带有 X.509 的 RSA ......有什么想法吗?

UPD:虽然试图让服务器总是说“购买正常”并将所有购买添加到数据库中,但发现这个错误是我的失败。我将json带到base64中的服务器,因为在服务器上base64_decode它在两个不同的地方,所以我打破了它。该库在使用 openssl 验证 json 的部分代码中工作。据我了解,Previos 版本只是验证包名称;这可能很容易重写以productId从 json 读取。

4

0 回答 0