douzai2562 2015-04-15 02:25
浏览 356

是否可以通过仅使用php openssl函数将.cer和.key文件转换为.pem?

I already have converted a public key (.cer) and a private key (.key) to pem format but I had use the command lines to convert them as follows:

$openSSLpath = "c:\OpenSSL-Win64\bin\openssl.exe";   //Path to OpenSSL exe
$path = "c:\appserver\Apache2.2\htdocs\FIEL";        //Path to Project Folder
$rutaCer = $path.'\GAMA600504JP1.cer';
$rutaKey = $path.'\GAMA600504JP1.key';
$passwordKey = '12345678a';

$cmdKey = $openSSLpath." pkcs8 -inform DER -in ".$rutaKey." -passin pass:".$passwordKey." -out ".$rutaKey.".pem";
exec($cmdKey);

$cmdCer = $openSSLpath." x509 -inform DER -outform PEM -in ".$rutaCer." -pubkey -out ".$rutaCer.".pem";
exec($cmdCer);

This generates correctly the .pem files but I need to know if is it possible to do this by any other way using PHP. Note that I'm completely new to openSSL and this is the only way I've found to do it on PHP.

To do this I have to ensure that OpenSSL its properly located and installed on my server and in order to do the conversions to .pem I have to save the .cer and .key files temporarily in the server which its not recommended due to security reasons.

Is there any other way?

  • 写回答

1条回答 默认 最新

  • douyan1903 2015-04-16 14:35
    关注

    You can easily convert your der data to PEM by simple code.

    $rutaCer = file_get_contents($path.'\GAMA600504JP1.cer');
    $rutaKey = file_get_contents($path.'\GAMA600504JP1.key');
    
    echo "-----BEGIN PRIVATE KEY-----
    " . chunk_split(base64_encode($rutaKey), 64, "
    ") . "-----END PRIVATE KEY-----";
    echo "-----BEGIN CERTIFICATE-----
    " . chunk_split(base64_encode($rutaCer), 64, "
    ") . "-----END CERTIFICATE-----";
    
    // Save anywhere
    

    PHP has functions for exporting to PEM but there is problem with loading keys/certificates from binary DER format.

    Another way can be use of phpsec library.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿