支付宝开发。加密交易。 php p12

Posted

技术标签:

【中文标题】支付宝开发。加密交易。 php p12【英文标题】:Paypal development. encrypt transactions. php p12 【发布时间】:2014-07-17 10:35:36 【问题描述】:

当我查看 paypal 文档时,他们说“请注意,用于 php 的 PayPal SDK 不需要 SSL 加密”。 https://developer.paypal.com/docs/classic/api/apiCredentials/#encrypting-your-certificate

这句话的意思是说我在使用php时不必创建p12证书,而是使用public_key.pempaypal_public_key.pem

如果是: 在没有 p12 证书的情况下创建加密的表单输入元素是否足够安全?

如果没有: 他们的意思是什么? :-)

在提出这个问题之前,我已经测试了这个小程序。 http://www.softarea51.com/blog/how-to-integrate-your-custom-shopping-cart-with-paypal-website-payments-standard-using-php/

有一个配置文件 paypal-wps-config.inc.php 我可以在其中定义我的证书的路径。

  // tryed to use // 'paypal_cert.p12 ';
  $config['private_key_path'] = '/home/folder/.cert/pp/prvkey.pem'; 

  // must match the one you set when you created the private key
  $config['private_key_password'] = ''; //'my_password'; 

当我尝试使用 p12 证书时,openssl_error_string() 返回“无法签署数据:错误:0906D06C:PEM 例程:PEM_read_bio:no start line openssl_pkcs7_sign

当我改为使用没有密码的 prvkey.pem 时,一切正常。

这是对数据进行签名和加密的函数。

    function signAndEncrypt($dataStr_, $ewpCertPath_, $ewpPrivateKeyPath_, $ewpPrivateKeyPwd_, $paypalCertPath_)
    

        $dataStrFile  = realpath(tempnam('/tmp', 'pp_'));
        $fd = fopen($dataStrFile, 'w');
        if(!$fd) 
            $error = "Could not open temporary file $dataStrFile.";
            return array("status" => false, "error_msg" => $error, "error_no" => 0);
        
        fwrite($fd, $dataStr_);
        fclose($fd);

        $signedDataFile = realpath(tempnam('/tmp', 'pp_'));
        **// here the error came from**
        if(!@openssl_pkcs7_sign(    $dataStrFile,
                                    $signedDataFile,
                                    "file://$ewpCertPath_",
                                    array("file://$ewpPrivateKeyPath_", $ewpPrivateKeyPwd_),
                                    array(),
                                    PKCS7_BINARY)) 
            unlink($dataStrFile);
            unlink($signedDataFile);
            $error = "Could not sign data: ".openssl_error_string();
            return array("status" => false, "error_msg" => $error, "error_no" => 0);
        

        unlink($dataStrFile);

        $signedData = file_get_contents($signedDataFile);
        $signedDataArray = explode("\n\n", $signedData);
        $signedData = $signedDataArray[1];
        $signedData = base64_decode($signedData);

        unlink($signedDataFile);

        $decodedSignedDataFile = realpath(tempnam('/tmp', 'pp_'));
        $fd = fopen($decodedSignedDataFile, 'w');
        if(!$fd) 
            $error = "Could not open temporary file $decodedSignedDataFile.";
            return array("status" => false, "error_msg" => $error, "error_no" => 0);
        
        fwrite($fd, $signedData);
        fclose($fd);

        $encryptedDataFile = realpath(tempnam('/tmp', 'pp_'));
        if(!@openssl_pkcs7_encrypt( $decodedSignedDataFile,
                                    $encryptedDataFile,
                                    file_get_contents($paypalCertPath_),
                                    array(),
                                    PKCS7_BINARY)) 
            unlink($decodedSignedDataFile);
            unlink($encryptedDataFile);
            $error = "Could not encrypt data: ".openssl_error_string();
            return array("status" => false, "error_msg" => $error, "error_no" => 0);
        

        unlink($decodedSignedDataFile);

        $encryptedData = file_get_contents($encryptedDataFile);
        if(!$encryptedData) 
            $error = "Encryption and signature of data failed.";
            return array("status" => false, "error_msg" => $error, "error_no" => 0);
        

        unlink($encryptedDataFile);

        $encryptedDataArray = explode("\n\n", $encryptedData);
        $encryptedData = trim(str_replace("\n", '', $encryptedDataArray[1]));

        return array("status" => true, "encryptedData" => $encryptedData);
     // signAndEncrypt
 // PPCrypto

主要问题:

    是否可以将 p12 证书与 php 一起使用,或者没有它是否足够安全?

    为什么我在使用openssl_pkcs7_sign时会报错

请帮忙。

问候 宁辰

【问题讨论】:

【参考方案1】:

您不应将“使用 SSL”与“使用带有预定义客户端证书的 SSL”混淆。您链接到的文档描述了后者。只需调用https URL 即可启用 SSL 并提供与浏览器等效的安全性。这是由 SDK 自动完成的。

预定义的客户端证书可防止老练的攻击者执行中间人攻击。这两种方法都可以阻止不成熟的攻击者直接读取您的网络流量。

客户端证书还用于向 PayPal 验证您的身份,作为用户/密码/签名的替代。

【讨论】:

嘿汤姆。谢谢你的建议。 但我还有一些问题here ---

以上是关于支付宝开发。加密交易。 php p12的主要内容,如果未能解决你的问题,请参考以下文章

PHP - 贝宝支付交易

支付宝统一收单交易创建接口,在PHP中怎么传递notify_url,具体的代码,不知道的别乱回答,谢谢

php开发常用的实用方法(第一节)(电商开发微信支付宝开发常用)

php开发常用的实用方法(第一节)(电商开发微信支付宝开发常用)

支付宝即时到账接口开发 - DEMO讲解

支付宝接口 notify_url.php和return_url.php这两个页面应该怎么写