php服务器中的Android应用内结算签名验证

时间:2013-01-21 01:35:12

标签: php android in-app-billing public-key-encryption

我在Android应用程序中处理IAB v3。 每次成功购买后,我希望我的应用程序将签名数据和签名发送回我的php服务器,以便通过谷歌开发者控制台生成的公钥进行验证。 我找到了以下代码。

<?php
// $data and $signature are assumed to contain the data and the signature

// fetch public key from certificate and ready it
$fp = fopen("/src/openssl-0.9.6/demos/sign/cert.pem", "r");
$cert = fread($fp, 8192);
fclose($fp);
$pubkeyid = openssl_get_publickey($cert);

// state whether signature is okay or not
$ok = openssl_verify($data, $signature, $pubkeyid);
if ($ok == 1) {
    echo "good";
} elseif ($ok == 0) {
    echo "bad";
} else {
    echo "ugly, error checking signature";
}
// free the key from memory
openssl_free_key($pubkeyid);
?>

现在我遇到了问题。谷歌提供的公钥是String Base64编码。我不知道如何将该字符串键转换为“.pem”格式。

如果我在上面的代码中将我的Base64编码密钥放到“$ pubkeyid”上。将发出警告。

Warning: openssl_verify() [function.openssl-verify]: supplied key param cannot be coerced into a public key in myxxx.php.

如何将String Base64编码的公钥转换为php接受格式?

有没有人有上述经验或解决方案?请帮忙。非常感谢。

3 个答案:

答案 0 :(得分:9)

要将您从Google获得的长base64编码公钥转换为可在PHP中使用的公钥,请尝试以下操作:

$base64EncodedPublicKeyFromGoogle = "..."; // This is the public key for your app you get from Google.

$openSslFriendlyKey = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($base64EncodedPublicKeyFromGoogle, 64, "\n") .  "-----END PUBLIC KEY-----";

然后你可以将其传递给openssl_get_publickey()

$publicKeyId = openssl_get_publickey($openSslFriendlyKey);

正如您所看到的,Google的格式几乎是正确的。它只需要分成64个字符的行,并在前面/后面添加正确的页眉/页脚。

您还可以使用OpenSSL命令转换公钥,如下所示:

openssl enc -base64 -d -in publickey.base64 -A | openssl rsa -inform DER -pubin > publickey.pem

然后,您可以使用PHP读取生成的publickey.pem文件,并将其内容传递给openssl_get_publickey()函数。

答案 1 :(得分:1)

答案 2 :(得分:1)

海报问题的完整解决方案:

<?php
// $data and $signature are assumed to contain the data and the signature

// Paste your google public key below:
$base64EncodedPublicKeyFromGoogle  = "###############################"

//Convert the key to the right format for open SSL
$openSslFriendlyKey = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($base64EncodedPublicKeyFromGoogle, 64, "\n") .  "-----END PUBLIC KEY-----";
$publicKeyId = openssl_get_publickey($openSslFriendlyKey);

// free the key from memory
openssl_free_key($publicKeyId);

//Perform signature verification. Don't forget to decode the signature!
$ok = openssl_verify($data, base64_decode($signature), $publicKeyId, OPENSSL_ALGO_SHA1);
if ($ok == 1) {
    echo "good";
} elseif ($ok == 0) {
    echo "bad";
} else {
    echo openssl_error_string();
}

?>