Related Questions :

How to convert a (complete) p12/pfx certificate to receive it in web service and redirects it to PJP page which I download it in there to browser. I used xxd , hexdump and od.

Yet couldn't optimize the output of these commands to make it's appropriate input for hex2bin function in PHP to get the binary back to download the certificate.

  • I'm a little unsure what you're trying to accomplish. Are you trying to pass the certificate through exactly how it's stored on the filesystem? If that's the case fpassthru is usually considered binary safe. Not sure what good that does you, though, unless the goal is to just distribute the p12 certs through a web interface (which is why I was unsure if this is what you wanted). – Bratchley Jun 23 '13 at 20:46

solution is to remove spaces of the output of bash in bash :

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

in 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;
  • Ah, I wrote my comment before seeing that this answer was your follow up. It looks like you pretty much figured it out, except fpassthru makes the bash and hex2bin stuff irrelevant. – Bratchley Jun 23 '13 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.