Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 下载证书二进制数据时,结果为无效证书_Php_Download_Binary_Certificate_Xxd - Fatal编程技术网

Php 下载证书二进制数据时,结果为无效证书

Php 下载证书二进制数据时,结果为无效证书,php,download,binary,certificate,xxd,Php,Download,Binary,Certificate,Xxd,我正在接收php中的二进制数据作为证书内容,我想将其下载到浏览器中。 使用证书查看器打开时,我总是收到以下消息: 这个二进制数据是正确的,我立即将它放在服务器上的一个文件中,它生成了一个有效的证书 我认为问题在于两个方面: 命令输出 exec('sshroot@192.168.0.137“echo”“$bindata”“| xxd-p-r”“tr-d\'\n\'”,$output) 而$bindata来自于在bash中使用xxd-p将证书pfx文件转换成文件后的转换 或者在标题中,有丢失或

我正在接收php中的二进制数据作为证书内容,我想将其下载到浏览器中。 使用证书查看器打开时,我总是收到以下消息:

这个二进制数据是正确的,我立即将它放在服务器上的一个文件中,它生成了一个有效的证书

我认为问题在于两个方面:

  • 命令输出

    exec('sshroot@192.168.0.137“echo”“$bindata”“| xxd-p-r”“tr-d\'\n\'”,$output)

$bindata
来自于在bash中使用
xxd-p将证书pfx文件转换成文件后的转换

  • 或者在标题中,有丢失或添加:

    header("Content-Description: File Transfer");
    header("Content-Type: application/octet-stream");
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment; filename=hex.pfx");
    header('Content-Length: '.  strlen($output[0]));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    echo $output[0];
    exit();
    
    怎么了


    • 解决方案是删除xxd输出的空格

      在bash中:

      res=`xxd -p $exportedkey`
      echo "${res//[[:space:]]/}"
      
      在php中:

      $hex = hex2bin($result);
      header("Content-Description: File Transfer");
      header("Content-Type: application/octet-stream");
      header("Content-Transfer-Encoding: binary");
      header("Content-Disposition: attachment; filename=hex.pfx");
      header('Content-Length: '.  strlen($hex));
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      
      echo $hex;
      exit(); 
      

      希望它至少能帮助某些人

      你想用exec命令实现什么?我在问题中写道,我想将我的证书从十六进制转换回bin,然后再交给浏览器。这样做似乎需要大量(昂贵的)重定向。可能是一个简单的
      包('H*',$hexstr)也会这样做。你是从哪里得到十六进制字符串的?它的真正含义是什么?一个pem/der格式的x509证书,一个(完整的)p12/pfx,…?实际上我在bash中使用
      xxd
      将其从bin转换为hex,所以我在php中重新使用它将其转换为bash并将其提供给浏览器。因为p12/pfx是二进制的,如果不将其转换为十六进制,就不能作为bash的输出
      $hex = hex2bin($result);
      header("Content-Description: File Transfer");
      header("Content-Type: application/octet-stream");
      header("Content-Transfer-Encoding: binary");
      header("Content-Disposition: attachment; filename=hex.pfx");
      header('Content-Length: '.  strlen($hex));
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      
      echo $hex;
      exit();