English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

Object function

PHP OpenSSL Function Manual

Usage and example of PHP openssl_pkey_get_private() function

The openssl_pkey_get_private() function is used to obtain the private key.

Definition and usageopenssl_pkey_get_private()

The function will return your private key./The function openssl_pkey_get_private() from the given public key

Returns the private key. For example, the content in the .pem file.

   Syntax

Serial number

openssl_pkey_get_private ( mixed $key [, string $passphrase = "" ] ) : resourceSerial numberParameter
1

Description

key

2

You can obtain the key from the .pem file or use the private key generated by openssl_pkey_new().

 passphrase

Return value

If the specified key has been encrypted (protected by a password), the optional parameter passphrase is required

PHP version

This function will return a resource identifier if there is no error.5Starting from PHP version .0.0

Example1

The work of openssl_pkey_get_private():

<?php
       //Generate a new private key (and public key) pair
	$privkey = openssl_pkey_new();
	openssl_pkey_export($privkey, $yourprivatekey);
	$testprivatekey = openssl_pkey_get_private($yourprivatekey);
	if ($testprivatekey === false) {
		var_dump(openssl_error_string());
	}
		var_dump($testprivatekey);
	}
?>

This will produce the following result:

resource(3) of type (OpenSSL key)

Example2

The work of openssl_pkey_get_private() and openssl_pkey_get_details()-

<?php
   $privkey = openssl_pkey_new();
	openssl_pkey_export($privkey, $yourprivatekey);
	$testprivatekey = openssl_pkey_get_private($yourprivatekey);
	if ($testprivatekey === false) {
      var_dump(openssl_error_string());
	}
		//var_dump($testprivatekey);
		$key_details = openssl_pkey_get_details($testprivatekey);
		print_r($key_details);	
	}
?>

This will produce the following result:

Array
(
    [bits] => 2048
    [key] => -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnO1F0Gt03mgxLcDpRxlm
VEh8sfIfan5o11EcoLaEwaTyKgMNm7A4Rs9LcG3fwkNjXo8XNRQv6OSrhl8y00AX
+cPUb78Qp/K3INmyXr8UUVWy0BT+Rxq5kez1TmZhpITxUYLScEL8DPcghFyfstRa
5hP8hc0cwYM6N4ieOlXIxrdmbzTn92MeyiTstTvHxt8aGKbSdpmksWyNeqke22kM
9pBfEsf98XFh5HoQBQaQF6GXL5y00PWIdki7DTzYtXXPRGcQl/53M5HrGGdP0kGS
uD4YOFXRWYtQR1ZYLa4Ej+BP0eOpfxXiME0aaH1/2iWqyi+bsvkdgFbu92j5ptsr
yQIDAQAB
-----END PUBLIC KEY-----
    [rsa] => Array
        (
            [n] => ��E�kt�h1-��GfTH|��j~h�Q������*
��8F�Kpm��Cc^�5/�䫆_2�@���o��� ٲ^�QU���G����Nfa���Q��pB�� �\���Z������:7��:U�Ʒfo4��c�$�;�����v���l�z��i��_���qa�z���/�����vH�
<صu�Dg��w3��gO�A��>8U�Y�PGVX-���O���0Mh}�%��/����V��h���+�
            [e] => 
            [d] => MK��C|��Y5��5}Z�R0;�S�I�V������M��0�Lw�r�����R��|��C���d�W�}�#����v�[9iZI��1��&!���A�;8K�%}��`@
����������}��n	b�]K�L#�~Sg�' 81!��2R]������`�vl�&�Z�@-q���7u�#����.���d������]�*This is a thirty-seventh sample text.-�'�|�ܳ ?M
j������Å
            [p] => �*&5IXM�U�$u�'����d,�$x��iR�1�ᙯ���A�Cxi`%�FR�5�
��!�a��C��֧t�^����f�$���V�+�ү��]v"�+=��I�w��RN��<�P�e��
3-�"���^����|_�-�l��lD,���=�26�ۗXGg5�@}q��T.�j����A��#��m,>#YN}��,pk}g
            [dmp1] => ��
�l5o�5L������ޅh)I�+��D�n�J!��ycP���1
=u>&��h-HT����K��j;�um
�oO\�e��.����
This is a seventy-ninth sample text.3This is a seventy-eighth sample text.1This is a seventy-seventh sample text.1This is a seventy-sixth sample text.
            [dmq1This is a seventy-fifth sample text.
This is a seventy-fourth sample text.+This is a seventy-third sample text.
This is a seventy-second sample text.
            This is a seventy-first sample text.6This is a seventieth sample text.87This is a sixty-ninth sample text.4This is a sixty-eighth sample text.9This is a sixty-seventh sample text.
        )
    [type] => 0
)

Example3

This is a sixty-sixth sample text.

<?php
   $privkey = openssl_pkey_new();
	This is a sixty-fifth sample text.
	This is a sixty-fourth sample text.
	if ($testprivatekey === false) {
		var_dump(openssl_error_string());
	}
		//var_dump($testprivatekey);
		$key_details = openssl_pkey_get_details($testprivatekey);
		print_r($key_details);	
	}
?>

This will produce the following result:

Array
(
    [bits] => 2048
    [key] => -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8This is a sixty-third sample text.4This is a sixty-second sample text.+This is a sixty-first sample text.9This is a sixtieth sample text.1This is a fifty-ninth sample text.9This is a fifty-eighth sample text.
64This is a fifty-seventh sample text.8This is a fifty-sixth sample text.7This is a fifty-fifth sample text.33This is a fifty-fourth sample text.6This is a fifty-third sample text.9This is a fifty-second sample text.7This is a fifty-first sample text.+This is a fiftieth sample text./9This is a forty-ninth sample text.+This is a forty-eighth sample text.5This is a forty-seventh sample text.
This is a forty-sixth sample text.+This is a forty-fifth sample text.5This is a forty-fourth sample text.3This is a forty-third sample text.9This is a forty-second sample text.4This is a forty-first sample text.59This is a fortieth sample text.39This is a thirty-ninth sample text./This is a thirty-eighth sample text.1/This is a thirty-seventh sample text.
This is a thirty-sixth sample text.6This is a thirty-fifth sample text.9This is a thirty-fourth sample text.2This is a thirty-third sample text./This is a thirty-second sample text.6This is a thirty-first sample text.4This is a thirtieth sample text.3/A7This is a twenty-ninth sample text.5This is a twenty-eighth sample text.22This is a twenty-seventh sample text.
This is a twenty-sixth sample text.4This is a twenty-fifth sample text.5This is a twenty-fourth sample text.2This is a twenty-third sample text.3This is a twenty-second sample text.+This is a twenty-first sample text.49This is a twentieth sample text./This is a nineteenth sample text.7This is an eighteenth sample text.6This is a seventeenth sample text.4This is a sixteenth sample text.9This is a fifteenth sample text.3This is a fourteenth sample text.
This is a thirteenth sample text./This is a twelfth sample text.2This is an eleventh sample text.6A71This is a tenth sample text.+4This is a ninth sample text.+5This is an eighth sample text.
This is a seventh sample text.
-----END PUBLIC KEY-----
    [rsa] => Array
        (
            This is a sixth sample text.
This is a fifth sample text.
+This is a fourth sample text.4��9
This is a third sample text.6�8This is another sample text.4This is a sample text.7���`�8�/�*�̥бqp�c��B3���qdmɲ��o&p�����Ke�½��c�_�q����o�;�72*��r�@�ⱗ���?
            [e] => 
            [d] => ��e���e�$%
sဩQ��EUA�D��Bu��34$�v�#����P�\��uD��(�z�w+�z����aͲ5���X��������q�Pŵڡ��%O+3��]+o�3�FM�*�Z^L��k�/�Ty��/��;�)�sB�Np�5�@�x�,8�z2�#�SyM�o�Xċ.���;�����K��� ���ڊ��qsLLD
^���"j��*=Io{����7`a��{�l�ᛅ�B������#���\�
            [p] => ׳��à�tY�@���[N��n�p�Ƌ��f�	��?7/\���͡�y<ox�,�(損�i�w�X�P�5�@��s BD�C���⎓�| �*���JH��/��|`�����+�3��]��oc?�fI�Ze�`�C�>U��|�"��ڊ:U���^��L����g�	�u|@z����F%6�X/�
��Ú��+�_�Yh:���59}�D�&כ��Gc
            [dmp1] => i�*/
�U�̯�6��
���|�>3�HP�hN��.�G�������9���cxj*�!��0�ؼ��R��:�9E(�Z��$�&�&7>�9.Ih��E�z/�,�QR�"�'�;���^���xq
            [dmq1] => |�w��`���䛌���
��3�����b�����AZ�t���.8���Q]��F�iI��2)�{<��G^ܵt��u�Mb��4I�YL�E�S�8a!�6��&Q�(�^��������É���
            [iqmp] => \p5��N��
$WC�j�� �|�{>d�������Y
f�����9�3�$rF(ON��>��� ����%��`(�
&�op� �}=C+����b�˝�#�G"�0T.���G`t�܈��^���\7�
        )
    [type] => 0
)

Example4

Handling openssl_pkey_get_private() with .pem file:

<?php
        //Create private key
	$privkey = openssl_pkey_new();
	openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/fortesting.pem');
	//Use the .pem file with the private key.
	$testprivatekey = openssl_pkey_get_private(file_get_contents('C:/xampp/htdocs/modules/openssl/fortesting.pem));
	if ($testprivatekey === false) {
		var_dump(openssl_error_string());
	}
		//var_dump($testprivatekey);
		$key_details = openssl_pkey_get_details($testprivatekey);
		print_r($key_details);	
	}
?>

This will produce the following result:

Array
(
    [bits] => 2048
    [key] => -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqaka1+sKh3D4HgIDzER6
vr/DI5u6h1QF6Xm1q/nOduTn0vlx4bLv+QEbcElXV1Bss4W7wOZRkMIOwj4xcT+e
PGKaN95JUyxC/NQ13+F6K5yUk0ish36BVusrHt0wdZj28f63gHm824D0yDnn+aJr
s+vSuMppErUD/i0QUFnO86ypHi/zeb+QBEif4a82RtfwRIVUtE/Sxy08ct+1ogA9
pdBd47elLmcekz/dtSUqpUjLj5SNojS7AJCZ5LNxnLOzN3ryCQXGaAn8KHQ284Xs
jlYBjSjXFLY/1fLDYDpQGOApoBj2vK9Io8MxFU3uss79Ezb6LwKZG6CmzlbldBrJ
YQIDAQAB
-----END PUBLIC KEY-----
    [rsa] => Array
        (
            [n] => [n]
�p��Dz���#���T�y����v����q���pIWWPl�����Q���>1q?�<b�7�is,b��5��z+���h��~�v�+�0u������y�ۀ��9���k��Ҹ�i��-py��  �y��h��6f��d�t�o��- 
            [d] => �9:������Y����"���*xu�&��gt����  &"���{��!Px�Ÿr�hn#��!c%�u
�ʻ�꛷x��7z����&��|��ǔv�Ĩѳ�XT?΅��[w"�=e��m�1R_JH�/�hX��.�E����&&'�:�/:������.I�zdx@�6��)��i��1L�z��"X��>�����]�t����Rh��g��!8�Y9�G�rŜ��9z�����z��~�����jo_���
            [p] => ٥���+�iy��Ѷm:#`BF���������Bj>�����f�2W%�eIAn� ����۪��.o�	v�`tgg��a�L�%ٱ�
L[��\6��`��sx]�~�bU�fF�/Oy�6�+~v��7u�
            [q] => ǎ�Ͱ�u(�.L���l��o�Gc�x�������@4��r� `i�i�X��v��'�33X��c��_׏��m8��M���5#t"��^�{�j	vSLi�X��mhԷI܀8�
1t�
            [dmp1] => ���6�$5�Eg�Q�1F"��8y��V`��\��A�{����0�
��ym�6�dUI<�8�f���\��"K�Khja5�z
�w�'�m�F9�nv�rϑFP]��i�%��߿L�1�m'�Y��4
@lth7�
            [dmq1] => il5w*�eG�~��z�2�{��*�"Ӟ������`�����y�r��������%@�lq���De��U�z���
�s.��[
����G�l_äh�����8¢H4N�7$�25�?�L�
            [iqmp] => 9"l@Ǚ�O�[I)}��K�6A/f5S�	����\�u���>��C��׏�� �z��]�B�-ry/����D��w��H|�g:����8���s̙��◻(�B�Eo�`0
)

PHP OpenSSL Function Manual