gpt4 book ai didi

PHP 服务器端 IAB 验证 openssl_verify 总是返回 0

转载 作者:行者123 更新时间:2023-11-29 01:47:57 29 4
gpt4 key购买 nike

我正在使用以下函数(服务器端 php)来验证 IAB v3 交易:

我正在从 android 应用程序传递过来:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

String signed_data=data.getStringExtra(IabHelper.RESPONSE_INAPP_PURCHASE_DATA);
String signature=data.getStringExtra(IabHelper.RESPONSE_INAPP_SIGNATURE);

我感觉这可能与我传递的签名有关。我正在使用以下 Android 方法对其进行编码,因为没有编码我会收到错误消息:

public String URLsafe(String text){
try {
return URLEncoder.encode(text, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();;
}
return null;
}

我正在传递 url

http://www.example.com/handlepayment.php?signature=....&data=....


public String getXmlFromUrl(String url) {

String xml = null;

try {
// defaultHttpClient
DefaultHttpClient httpClient = new MyHttpClient_ALKS(myContext.getApplicationContext());
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}

到服务器:

function verify_play($signed_data, $signature) 
{
global $public_key_base64;
$pkey = "-----BEGIN PUBLIC KEY-----\n".
chunk_split($public_key_base64, 64,"\n").
'-----END PUBLIC KEY-----';
//using PHP to create an RSA key
$pkey = openssl_get_publickey($pkey);
//$signature should be in binary format, but it comes as BASE64.
//So, I'll convert it.
$signature = base64_decode($signature);
//using PHP's native support to verify the signature
$result = openssl_verify(
$signed_data,
$signature,
$pkey,
OPENSSL_ALGO_SHA1);

if (0 === $result)
{
return false;
}
else if (1 !== $result)
{
return false;
}
else
{
return true;
}
} ;

它似乎总是返回 false ($result=0),有人知道为什么吗?如何传递未编码的签名,或者我应该使用哪种编码?

最佳答案

根据我的经验,openssl_get_publickey() 只有在 X.509 证书中有公钥时才会创建公钥资源。

我的建议是使用 phpseclib, a pure PHP RSA implementation .例如。

function verify_play($signed_data, $signature) 
{
global $public_key_base64;
$rsa = new Crypt_RSA();
$rsa->loadKey($public_key_base64);
$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
$signature = base64_decode($signature);
return $rsa->verify($signed_data, $signature);
}

关于PHP 服务器端 IAB 验证 openssl_verify 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20261977/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com