微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在PHP中创建数字证书并导出到.p12文件?

如何在PHP中创建数字证书并导出到.p12文件

我希望.p12文件包含私钥.并且还想检查密钥对是否已经发布(登录数据库).

我找到了一个名为’openssl_pkcs12_export_to_file’的函数,但不知道从哪里开始.似乎我首先需要X509证书和私钥.

解决方法:

<?PHP
error_reporting(-1);

function dump($Var) {
  echo "<hr/><pre>";
  var_dump($Var);
  echo "</pre><hr/>";
}

function check_errors() {
  echo "<hr/><pre>";
  $Count = 0;
  while (($e=openssl_error_string())!==false) {
    echo $e."<br>";
    $Count++;
  }
  if ($Count==0)
    echo "No error";
  echo "</pre><hr/>";
}

$Configs = array(
  "config" => "e:/progetti/PHP/openssl/openssl.cfg",
  "digest_alg" => "sha1",
  "x509_extensions" => "v3_ca",
  "req_extensions" => "v3_req",
  "private_key_bits" => 1024,
  "private_key_type" => OPENSSL_KEYTYPE_RSA,
  "encrypt_key" => true,
  "encrypt_key_cipher" => OPENSSL_CIPHER_3DES 
);
$Info = array(
  "countryName" => "VN",
  "stateOrProvinceName" => "Hanoi",
  "localityName" => "Long Bien",
  "organizationName" => "Test Company",
  "organizationalUnitName" => "Test Department",
  "commonName" => "Tester",
  "emailAddress" => "test@gmail.com"
);

$Private_Key = null;
$Unsigned_Cert = openssl_csr_new($Info,$Private_Key,$Configs);
check_errors();
dump($Private_Key);
dump($Unsigned_Cert);

$Signed_Cert = openssl_csr_sign($Unsigned_Cert,null,$Private_Key,365,$Configs);
check_errors();
dump($Signed_Cert);

openssl_pkcs12_export_to_file($Signed_Cert,"test.p12",$Private_Key,"123456");
check_errors();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐