Android in-app billing signature verification in php server

混江龙づ霸主 提交于 2019-12-06 01:40:53

To convert the long base64-encoded public key you get from Google into one that you can use in PHP, try this:

$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-----";

Then you can pass that into openssl_get_publickey().

$publicKeyId = openssl_get_publickey($openSslFriendlyKey);

As you can see, the format from Google is almost the right kind. It just needs to be broken up into 64-character lines, and prepended/appended with the right header/footer.

You can also use the OpenSSL command to convert the public key like this:

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

Then you can read in the generated publickey.pem file with PHP and pass its contents to the openssl_get_publickey() function.

A complete solution to the poster's question:

<?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();
}

?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!