PHP中的openssl命令 [英] openssl command in php

查看:221
本文介绍了PHP中的openssl命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用php生成example.com.crtexample.com.pem.给出了获取文件的Linux命令:

I want to generate example.com.crt and example.com.pem using php. The Linux command to get the files is given:

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

我想在php中以两个字符串获取两个文件内容. php的等效代码是什么?

I want to get the two file contents in two string in php. What is the php equivalent code for this?

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

UPDATE: Please don't ask to execute the Linux command.

推荐答案

此处是另一个命令:

<?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中的openssl命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆