We now have a youtube channel. Subscribe!

PHP | openssl_pkey_get_private() Function

PHP openssl_pkey_get_private() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP openssl_pkey_get_private() Function.

The openssl_pkey_get_private() function returns the private key.

This PHP function returns the private key from the given public/private key.

syntax

Following below is the syntax to use this function -

openssl_pkey_get_private ( mixed $key [, string $passphrase = "" ] ) : resource


Parameter Details

Sr.NoParameterDescription
1

key

You can take the key from the .pem file or using private key generated from openssl_pkey_new().

2

passphrase

If they key you are using is encrypted , than you will have to specify passphrase.


Return Value

This function returns a resource identifier if there is no error, or it returns false if the key generation fails.

PHP Version

This PHP function works from PHP version greater than 5.0.0.

Example1

The following example illustrates the usage of the openssl_pkey_get_private() function -

<?php
   // Generate a new private (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());
   } else {
      var_dump($testprivatekey);
   }
?>

Output

When the above code is executed, it will produce the following result -

resource(3) of type (OpenSSL key)

Example2

The following example illustrates the usage of the PHP openssl_pkey_get_private() and PHP openssl_pkey_get_details() functions -

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

Output

When the above code is executed, it 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�����]�*Z-�'�|�ܳ ?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��.��
          X�%s��ڕ_�|Za3��z$�\n1�N1�u�j)��z�
          [dmq1] => ��n[������ǣ<��t��^��P��)�F@ni��
          �gHꥴ(��@#s�!+$�D��]����{��I㯽l��[$��>i	q|�$�GØ¡�I'R��^c
          "����e"k
          [iqmp] => �Å–ij�	��a6�W#�:��#bS?�Ó��S�r#�C�×±87])4��Z��`��Ý»��w��.�a�y��@	�Ñ“�p���s�XP.{�o9h�Bq��뵘�EU��n ��&S��tE�
       )
      [type] => 0
   )

Example3

The following example illustrates the usage of the openssl_pkey_get_private() function with passphrase -

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

Output

When the above code is executed, it will produce the following result -

Array
(
   [bits] => 2048
   [key] => -----BEGIN PUBLIC KEY-----
   MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs4HV+NM9dQ1ssuxb9PhM
   64Yn8RHgv7YKK33nZudmk6SCOr9yRo7immp+bkaA0Lt/9ONJP+UF5VCltpdNdHLb
   GDTo+TkK5NdTJDalON3L9EhB4cJeQaQQh59uJGf39Et0BJiYiINXsNdlc/pig1/Z
   XDRyhEtqQ6nZJkOIIWO0gpdj9xj2naq/wy6Oas4p3/A7EvN5nB22xfEVIptKUZzi
   YWV4Bs5y2OM3GRwVv+jLYKQ49S/ZKq7MpdCxcXC6YxyrlEIz4PL9cWRtybK3BINv
   JnCWrfWRhUtlAY/CvfXrq2PhXwHVcebsDOPob6A71TcZMirxFZVyqkC+4rGX+5be
   PwIDAQAB
-----END PUBLIC KEY-----

   [rsa] => Array
      (
         [n] => ��=u
         l��[��L�'�࿶
         +}�f�f��:�rF��j~nF��I?��P��Mtr�4��9
         ��S$6�8��HA��^A���n$g��Kt��W��es�b�_�\4r�KjC��&C�!c��c���.�j�)��;�y���"�JQ��aex�r��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�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

The following example illustrates the usage of the openssl_pkey_get_private() function with .pem file -

<?php
   //creating private key
   $privkey = openssl_pkey_new();
   openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/fortesting.pem');
	
   //using .pem file with 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());
   } else {
      //var_dump($testprivatekey);
      $key_details = openssl_pkey_get_details($testprivatekey);
      print_r($key_details);	
   }
?>

Output

When the above code is executed, it 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] => ����
�p��Dz�#��T�y��v��q��pIWWPl��Q���>1q?� 
            [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
)


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we will be studying about the PHP openssl_pkey_get_public() Function.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain