PHP使用现有私钥创建CSR [英] PHP Creating CSR using existing private key

查看:259
本文介绍了PHP使用现有私钥创建CSR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php 
$c = $_POST['c'];
$city = $_POST['city'];
$cn = $_POST['cn'];
$mail = $_POST['email'];
$p = $_POST['pass'];
echo "$c<BR>$city<br>$cn<br>$mail";
$key = $_POST['key'];
$dn = array(
"countryName" => "$c",
"stateOrProvinceName" => ".",
"localityName" => "$city",
"organizationName" => ".",
"organizationalUnitName" => ".",
"commonName" => "$cn",
"emailAddress" => "$key"
//"passphase" => "$p"
);
// Generate a certificate signing request
$csr = openssl_csr_new($dn, $key);
var_dump($csr);
while ($msg = openssl_error_string())
echo $msg . "<br />\n";
?>

它显示了一个错误:

Warning: openssl_csr_new(): dn: add_entry_by_NID 48 -&gt; -----BEGIN RSA PRIVATE KEY----- M[...]= -----END RSA PRIVATE KEY----- (failed; check error queue and value of string_mask OpenSSL option if illegal characters are reported) in D:\wamp\www\csr.php on line 21

boolean false
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0D07A097:asn1 encoding routines:ASN1_mbstring_ncopy:string too long

这是我在php.net上找到的

This is what I found on php.net

mixed openssl_csr_new ( array $dn , resource &$privkey [, array $configargs [, array $extraattribs ]] )  

$pivkey应该是资源类型,如果我已经有一个私钥作为字符串怎么办?

$pivkey should be a resource type, what if I already have got a private key as string?

推荐答案

我个人将使用 phpseclib,这是一个纯PHP CSR实施.例如.

<?php
include('File/X509.php');
include('Crypt/RSA.php');

$key = new Crypt_RSA();
$key->loadKey($_POST['key']);

$x509 = new File_X509();
$x509->setPrivateKey($key);
$x509->setDN(array(
    'countryName' => $_POST['c'],
    'stateOrProvinceName' => '.',
    'localityName' => $_POST['city'],
    'organizationName' => '.',
    'organizationalUnitName' => '.',
    'commonName' => $_POST['cn'],
    'emailAddress' => $_POST['email']
));

$csr = $x509->signCSR();

echo $x509->saveCSR($csr);
?>

这篇关于PHP使用现有私钥创建CSR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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