duanpo2813 2014-10-13 18:35
浏览 60
已采纳

OpenSSL req不适用于PHP Shell

I've been searching for the solution of my problem which is about OpenSSL. I'm using shell_exec() to run shell promts. For example:

shell_exec("openssl genrsa -out myprvkey.pem 1024"); works fine, and generates myprvkey.pem.

However,

shell_exec("openssl req -new -key myprvkey.pem -x509 -days 365 -out mypubcert.pem"); isn't working without giving any errors.

Am I doing something wrong? Thanks. PS: Please remind me if I'm missing something.

OpenSSL Version: 268439647

  • 写回答

1条回答 默认 最新

  • douxuan4556 2014-10-14 13:23
    关注

    Looks like you're trying to create a CSR? If so you can do so using phpseclib, a pure PHP CSR implementation, pretty easily. Here's an example that creates a new RSA key and a new CSR:

    <?php
    include('File/X509.php');
    include('Crypt/RSA.php');
    
    $privKey = new Crypt_RSA();
    extract($privKey->createKey());
    $privKey->loadKey($privatekey);
    
    $x509 = new File_X509();
    $x509->setPrivateKey($privKey);
    $x509->setDNProp('id-at-organizationName', 'phpseclib demo cert');
    
    $csr = $x509->signCSR();
    
    echo $x509->saveCSR($csr);
    ?>
    

    Here's an example that creates a new CSR utilizing an existing RSA key:

    <?php
    include('File/X509.php');
    include('Crypt/RSA.php');
    
    $privKey = new Crypt_RSA();
    $privKey->loadKey('...');
    
    $x509 = new File_X509();
    $x509->setPrivateKey($privKey);
    $x509->setDNProp('id-at-organizationName', 'phpseclib demo cert');
    
    $csr = $x509->signCSR();
    
    echo $x509->saveCSR($csr);
    ?>
    

    (the only diff is that loadKey is being passed a string [which could be populated via file_get_contents or just directly inputted] instead of the output of extract($rsa->createKey()))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵