PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

openssl_pkey_new> <openssl_pkey_get_private
Last updated: Fri, 30 Jan 2009

view this page in

openssl_pkey_get_public

(PHP 4 >= 4.2.0, PHP 5)

openssl_pkey_get_public証明書から公開鍵を抽出し、使用できるようにする

説明

resource openssl_pkey_get_public ( mixed $certificate )

openssl_get_publickey() は公開鍵を certificate から抽出し、 他の関数で使用できるよう準備します。

パラメータ

certificate

certificate は以下のいずれかです。

  1. an X.509 証明書リソース
  2. file://path/to/file.pem 形式の文字列。 このファイルは、PEM エンコードされた証明書/秘密鍵である必要が あります(両方を含むことも可能です)。
  3. PEM フォーマットの秘密鍵。

返り値

成功した場合に正のキーリソース ID、エラー時に FALSE を返します。



openssl_pkey_new> <openssl_pkey_get_private
Last updated: Fri, 30 Jan 2009
 
add a note add a note User Contributed Notes
openssl_pkey_get_public
thelen dot shar at gmail dot com
25-Jan-2009 10:08
Found it difficult to get my head around this due to lack of documentation.

But the process I followed for all this was:
Generate private key:
openssl genrsa -des3 -out private.pem 1024

Generate public key:
openssl rsa -in private.pem -out public.pem -outform PEM -pubout

Then in PHP:
$passphrase = 'somestring';
$key_private = openssl_get_privatekey(file_get_contents('private.pem'), $passphrase);
$key_public = openssl_get_publickey(file_get_contents('public.pem'));

Probably not the best way of doing it, but a lot simpler than the other examples on the site. I was having trouble getting the pubkey, it wasn't exactly specified very well, and I had made a mistake in generating it so it wasn't working for that reason as well.
VaD
06-Jun-2008 08:36
Small error in this code:

$pub_key = openssl_pkey_get_public(file_get_contents('./cert.crt'));
$keyData = openssl_pkey_get_details($pub_key);
file_put_contents('./key.pub', $keyData['key']);
07-May-2007 10:40
you can get (and save to file) public key using openssl_pkey_get_details(resource $key ) function:

<?php
$pub_key
= openssl_pkey_get_public(file_get_contents('./cert.crt'));
$keyData = openssl_pkey_get_details($pub_key);
fule_put_contents('./key.pub', $keyData['key']);
?>
dankybastard at hotmail
09-Feb-2005 06:52
You must also use the string representation of the certificate to get the public key resource:

$dn = array();  // use defaults
$res_privkey = openssl_pkey_new();
$res_csr = openssl_csr_new($dn, $res_privkey);
$res_cert = openssl_csr_sign($res_csr, null, $res_privkey, $ndays);

openssl_x509_export($res_cert, $str_cert);

$res_pubkey = openssl_pkey_get_public($str_cert);
09-Aug-2004 09:44
This documentation notes it can take a PEM-formatted private key, but as per bug #25614, this is not possible in any form. The function simply returns a FALSE.

The only thing you can get public keys out of are X.509 certificates.

Furthermore, there is NO way to export a public key into a PEM-encoded form.

openssl_pkey_new> <openssl_pkey_get_private
Last updated: Fri, 30 Jan 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites