Php 贝宝如何在加密前构造按钮数据?

Php 贝宝如何在加密前构造按钮数据?,php,encryption,paypal,ssl-certificate,pkcs#7,Php,Encryption,Paypal,Ssl Certificate,Pkcs#7,目前,我已经在Paypal buy now按钮中搜索了很多关于PKCS7加密之前数据的结构,以便正确使用。到目前为止,我找到的关于数据结构的最佳数据是一个Paypal示例代码,它演示了加密按钮如何显示在您的网站上(可以在此处找到:) 乍一看,我的结果似乎与Paypal EaxPle链接中显示的完全一样,只是当我继续单击按钮时,我收到一个Paypal错误,上面写着“我们无法解密证书id”。因此,我认为问题要么在于数据的结构,要么在于实际的加密本身(我也进行了大量调试,并确保所有证书和密钥都已成功注

目前,我已经在Paypal buy now按钮中搜索了很多关于PKCS7加密之前数据的结构,以便正确使用。到目前为止,我找到的关于数据结构的最佳数据是一个Paypal示例代码,它演示了加密按钮如何显示在您的网站上(可以在此处找到:)

乍一看,我的结果似乎与Paypal EaxPle链接中显示的完全一样,只是当我继续单击按钮时,我收到一个Paypal错误,上面写着“我们无法解密证书id”。因此,我认为问题要么在于数据的结构,要么在于实际的加密本身(我也进行了大量调试,并确保所有证书和密钥都已成功注册)。下面的代码演示了我的php代码创建的按钮

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
  <input type="hidden" name="cmd" value="_s-xclick">
  <input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!">
  <input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIH6QYJKM585oH7A[to much data to show]9QRQIpFZzRK6cJu6QQO+q/xfiw==-----END PKCS7-----">
</form>
我目前正在使用上面的格式,这是在互联网上拼凑信息的结果,但它似乎不起作用(当我删除字符串中的所有空格时也不起作用)。因此,

主要问题:是否有人能告诉我或为我指出正确的方向,以便在加密之前了解数据的正确格式

对于任何有兴趣检查是否是代码通过不正确的加密创建了错误的人,可以在下面找到加密数据的功能:

function encryptButton($parameters) {

    if (($this->certificateID == '') ||
    !IsSet($this->certificate) ||
    !IsSet($this->paypalCertificate)) {
      return FALSE;
    }

    $clearText = '';
    $encryptedText = '';

    $clearText = 'cert_id='.$this->certificateID;

    foreach (array_keys($parameters) as $key) {

      $clearText .= "\n{$key}={$parameters[$key]}";
    }

    $clearFile = tempnam($this->tempFileDirectory, 'cle');
    $signedFile = preg_replace('/cle/', 'signed', $clearFile);
    $encryptedFile = preg_replace('/cle/', 'encrypted', $clearFile);

    $out = fopen($clearFile, 'wb');
    fwrite($out, $clearText); 
    fclose($out);

    //openssl_pkcs7_sign occurs here
    if (!openssl_pkcs7_sign($clearFile, $signedFile, $this->certificate, $this->privateKey, array(), PKCS7_BINARY)) {
      return FALSE;
    }

    $signedData = explode("\n\n", file_get_contents($signedFile));

    $out = fopen($signedFile, 'wb');
    fwrite($out, base64_decode($signedData[1]));
    fclose($out);

    //openssl_pkcs7_encrypt occurs here
    if (!openssl_pkcs7_encrypt($signedFile, $encryptedFile, $this->paypalCertificate, array(), PKCS7_BINARY)) {
      return FALSE;
    }

    $encryptedData = explode("\n\n", file_get_contents($encryptedFile));

    $encryptedText = $encryptedData[1];

    @unlink($clearFile);
    @unlink($signedFile);
    @unlink($encryptedFile);

    return $encryptedText;

  }


  function getEncryptedString($params) {
  return "-----BEGIN PKCS7-----".str_replace("\n", "", $this->encryptButton($params))."-----END PKCS7-----"; }

}

您可以使用PayPal提供的两种方法之一正确创建加密字符串:是的,我目前正在尝试使用EWP方法,并且已经创建并上传了必要的公共证书和私钥编辑:我还应该补充,此按钮需要具有动态内容,因为它将从动态购物车(这就是为什么我不使用Paypal button creator工具)为什么不使用Paypal提供的java?部分原因是我不知道如何自动化命令行来自动创建这些按钮。这对我来说很重要,因为一旦用户导航到购物车页面,就必须有一个新的buy now按钮,其中包含最新更新的购物车信息。是否有一种语言可以从php调用,并执行命令行,用php传递的信息创建加密按钮?甚至连php本身都有可能吗?
exec('java…')我自己不想为加密费心。您可以使用PayPal提供的两种方法之一正确创建加密字符串:是的,我目前正在尝试使用EWP方法,并且已经创建并上载了必要的公共证书和私钥编辑:我还应该补充,此按钮需要具有动态内容,因为它将从动态购物车更新(这就是为什么我不只是使用Paypal按钮创建工具)为什么你不使用Paypal提供的java?部分原因是我不知道如何自动化命令行来自动创建这些按钮。这对我来说很重要,因为一旦用户导航到购物车页面,就必须有一个新的buy now按钮,其中包含最新更新的购物车信息。是否有一种语言可以从php调用,并执行命令行,用php传递的信息创建加密按钮?甚至连php本身都有可能吗?
exec('java…')我自己不想麻烦加密。
function encryptButton($parameters) {

    if (($this->certificateID == '') ||
    !IsSet($this->certificate) ||
    !IsSet($this->paypalCertificate)) {
      return FALSE;
    }

    $clearText = '';
    $encryptedText = '';

    $clearText = 'cert_id='.$this->certificateID;

    foreach (array_keys($parameters) as $key) {

      $clearText .= "\n{$key}={$parameters[$key]}";
    }

    $clearFile = tempnam($this->tempFileDirectory, 'cle');
    $signedFile = preg_replace('/cle/', 'signed', $clearFile);
    $encryptedFile = preg_replace('/cle/', 'encrypted', $clearFile);

    $out = fopen($clearFile, 'wb');
    fwrite($out, $clearText); 
    fclose($out);

    //openssl_pkcs7_sign occurs here
    if (!openssl_pkcs7_sign($clearFile, $signedFile, $this->certificate, $this->privateKey, array(), PKCS7_BINARY)) {
      return FALSE;
    }

    $signedData = explode("\n\n", file_get_contents($signedFile));

    $out = fopen($signedFile, 'wb');
    fwrite($out, base64_decode($signedData[1]));
    fclose($out);

    //openssl_pkcs7_encrypt occurs here
    if (!openssl_pkcs7_encrypt($signedFile, $encryptedFile, $this->paypalCertificate, array(), PKCS7_BINARY)) {
      return FALSE;
    }

    $encryptedData = explode("\n\n", file_get_contents($encryptedFile));

    $encryptedText = $encryptedData[1];

    @unlink($clearFile);
    @unlink($signedFile);
    @unlink($encryptedFile);

    return $encryptedText;

  }


  function getEncryptedString($params) {
  return "-----BEGIN PKCS7-----".str_replace("\n", "", $this->encryptButton($params))."-----END PKCS7-----"; }