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

标签 php ssl encryption paypal certificate

当我查看 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() 返回“Could not sign data: error:0906D06C:PEM routines:PEM_read_bio:no start line openssl_pkcs7_sign>/p>

当我改为使用不带密码的 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

主要问题:

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

  2. 为什么我在使用 openssl_pkcs7_sign

    时出错

请帮忙。

问候语 宁臣

最佳答案

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

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

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

关于php - 支付宝开发。加密交易。 PHP p12,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23907633/

相关文章:

php - mysql - 按字段排序无法正常工作

php - 我如何从另一台电脑访问第二个 laravel 应用程序

c# - 如何通过其他证书签署证书

objective-c - Objective-C 中的 OpenSSL SHA256 等效项

android通过网络加密json数据

php - 将 PHP 脚本输出写入 txt 文件

php - 检测 "overall average"图片颜色

php - SIP2 不支持 php 中的 TLS1.2 协议(protocol)

apache - SSL 证书有效期

java - 这种 vigenere 密码在 Java 中是否可行?