【问题标题】:Android in-app billing signature verification in php serverphp服务器中的Android应用内计费签名验证
【发布时间】:2013-01-04 01:54:01
【问题描述】:

我正在我的 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);
?>

现在我有一个问题。 google 给出的公钥是 String Base64 Encoded。我不知道如何将该字符串键转换为“.pem”格式。

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

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

如何将我的字符串 Base64 编码公钥转换为 php 接受格式?

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

【问题讨论】:

  • base64_decode(...) 是你的朋友....
  • 虽然 key 可以通过 base64_decode() 解码,但是 openssl_verify() 仍然会警告我的 $pubkeyid 格式错误。我该怎么办? openssl_verify() [&lt;a href='function.openssl-verify'&gt;function.openssl-verify&lt;/a&gt;]: supplied key param cannot be coerced into a public key

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


【解决方案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();
}

?>

【讨论】:

    【解决方案2】:

    要将您从 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() 函数。

    【讨论】:

    • 这应该是 Google 验证签名指南的一部分。花费数小时搜索要使用的公钥的正确结构...谢谢!
    • 我得到这个:error:0906D06C:PEMroutines:PEM_read_bio:no start line,我该如何解决?
    【解决方案3】:

    【讨论】:

    • 上面的 php 类不再起作用了,还有其他建议吗?
    猜你喜欢
    • 2011-08-04
    • 2011-10-27
    • 2012-01-13
    • 2019-09-23
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多