php - PHP 中的 openssl 命令

标签 php openssl x509certificate

我想使用 php 生成 example.com.crtexample.com.pem。获取文件的 Linux 命令如下:

openssl req -newkey rsa:2048 -new -x509 -days 3652 -nodes 
            -out example.com.crt -keyout example.com.pem

我想在php.ini中获取两个字符串中的两个文件内容。与之对应的 php 代码是什么?

更新:请不要要求执行 Linux 命令。

最佳答案

这是另一个命令:

<?php 
// generate 2048-bit RSA key
$pkGenerate = openssl_pkey_new(array(
    'private_key_bits' => 2048,
    'private_key_type' => OPENSSL_KEYTYPE_RSA
));

// get the private key
openssl_pkey_export($pkGenerate,$pkGeneratePrivate); // NOTE: second argument is passed by reference

// get the public key
$pkGenerateDetails = openssl_pkey_get_details($pkGenerate);
$pkGeneratePublic = $pkGenerateDetails['key'];

// free resources
openssl_pkey_free($pkGenerate);

// fetch/import public key from PEM formatted string
// remember $pkGeneratePrivate now is PEM formatted...
// this is an alternative method from the public retrieval in previous
$pkImport = openssl_pkey_get_private($pkGeneratePrivate); // import
$pkImportDetails = openssl_pkey_get_details($pkImport); // same as getting the public key in previous
$pkImportPublic = $pkImportDetails['key'];
openssl_pkey_free($pkImport); // clean up

// let's see 'em
echo "\n".$pkGeneratePrivate
    ."\n".$pkGeneratePublic
    ."\n".$pkImportPublic
    ."\n".'Public keys are '.(strcmp($pkGeneratePublic,$pkImportPublic)?'different':'identical').'.';
?>

关于php - PHP 中的 openssl 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13622630/

相关文章:

php - 如何在 Laravel 中以多对多关系保存自定义列?

ssl - 更改证书 csr 的指纹 SHA

c++ - 如何修复 makefile 以正确包含 -lcrypto 以避免链接器错误 : undefined reference to `EVP_CIPHER_CTX_new'

node.js - 如何使用 node-forge 创建与 iOS 的多点连接兼容的 PKCS12?

ssl - 如何使用私钥生成SSL证书?

PHPMailer问题

php - 在 codeigniter 中通过查询转义订单

java - 使用java加密pdf中的嵌入附件

php - 更改具有相同值的行的顺序

openssl - ECDSA:如何使用 openssl 从解压缩 x 中获取 y 坐标