English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The openssl_pkey_new() function is used to generate a new private key.
openssl_pkey_new() generates a new private key and public key pair. You can obtain the public component of the key through the openssl_pkey_get_public() function.
The function openssl_pkey_new() returns a resource identifier. You can use the openssl_pkey_get_details() function to get all the details of the generated key. The key (public)/The private key pair) is used later together with other openssl functions (such as openssl_sign(), openssl_csr_new())-Obtain a CSR certificate that helps create encrypted digital signatures.
openssl_pkey_new ([ array $configargs ] ) : resource
Number | Parameters | Description |
---|---|---|
1 | configargs | You can use the configargs parameter to fine-tune the key generation (such as specifying the number of digits). Check openssl_csr_new() for more information about configargs. |
By default, openssl_pkey_new() uses the configuration details that exist in openssl.cnf. However, configargs can override them. The configuration details are as follows:
configargs | Type | The key types used in openssl.conf | Description |
---|---|---|---|
digest_alg | String | default_md | The digest method you get from openssl_get_md_methods(). |
x509_extensions | String | x509_extensions | Create x509The extension name used when creating a certificate. |
req_extensions | String | req_extensions | The extension name used when creating CSR. |
private_key_bits | integer | default_bits | Specify the number of bits to be used when generating the private key. |
private_key_type | integer | none | The type of private key to be created. It can be one of OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA, or OPENSSL_KEYTYPE_EC. The default value is OPENSSL_KEYTYPE_RSA. |
encrypt_key | Boolean | encrypt_key | Is the exported key encrypted? |
encrypt_key_cipher | integer | none | Password constants, such as OPENSSL_CIPHER_RC2_40 (integer), OPENSSL_CIPHER_RC2_128(integer), OPENSSL_CIPHER_RC2_64_ (integer), OPENSSL_CIPHER_RC3DES (integer), OPENSSL_CIPHER_DES (integer), OPENSSL_CIPHER_ |
curve_name | String | none | One of the curve names returned by the function openssl_get_curve_names(). |
config | String | N/A | You can change the configuration in openssl.conf as needed and provide its path here. |
If there is no error, the PHP openssl_pkey_new() function will return a resource identifier. If the key generation fails, it will return false.
This function will return a resource identifier if there is no error. If the key generation fails, it will return false.5Starting from PHP version .0.0.
The work of openssl_pkey_new():
<?php //Generate a new private key (and public key) pair $privkey = openssl_pkey_new(array( "digest_alg" => md5', "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); var_dump($privkey); ?>
This will produce the following results:
resource(2) of type (OpenSSL key)
The work of openssl_pkey_new() and openssl_pkey_get_details:
<?php //Generate a new private key (and public key) pair $privkey = openssl_pkey_new(array( "digest_alg" => md5', "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); $key_details = openssl_pkey_get_details($privkey); print_r($key_details); ?>
This will produce the following results:
Array ( [bits] => 2048 [key] => -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1IGWxnWzICSkjrOVrYkw 9EKpIhcAYbhaymiwQY/ii9d7hnuGhpjjitDxzFftGafL6XOFKOhgbO2yrcpFxRKu AY3t5wyUcqUJV6CNwV2Hipk90psUcTmK3+pcCzcqCKr7sLWlHI48lse92xane8Sf CATNNbr9vmqUaTZ9FQqWihm3o/rNGuZTwMSKvcKsVguFpwrEDJaSLP1nt7RSHGc+ PixQSXp3PtQCH+S0CM9jt1jD9NkYXuuAlNbrsPm1fl2zAGR5Vh15evz5765lZ2mH LIZScfsO/qgai3R6foaBlJM5tiSeiVZgnnQDKFBi5XK2GhzDnKggJe4tdY7awTFm CQIDAQAB -----END PUBLIC KEY----- [rsa] => Array ( [n] => ��u� $������0�B�"a�Z�h�A���{�{�������W����s�(�`l����E������r� W���]���=қq9���\7*������<�ǽ��{ğ�5���j�i6} �������S�Ċ�¬V�� ���,�g��Rg>>,PIzw>���c�X���^뀔����~]�dyVyz���egi�,�Rq����tz~����9�$��V`�t(Pb�r�Ü %�-u���1f [e] => [d] => F"�3����4��!K�yF��R�lY�9�]Ξ��A��@�f��~�<�YPєG��=���TkTP�rl�Mԓ{�lŮ^��!����q�˂�7�|�f����lE+#�@�.�GIH�tS�R.mbc�U~����7&w�ݥ�P.Fe�ϨU�@��m��X�ECsemjI$@/�$l����)�8�@���c�w�NRo���+!'�.����fvT7�=n")LQ�sd�%zZ���G��{�{�1A^�v��E [p] => =>u)�1The current K�r���pHE��k���LBE����y�'�3-�/��Pd���jeFV1����?�.O�����:�bl2�z�U�{��{��*��A���<�}m4��J�Y�'���f@����� [q] => ߦz\��96�=��V]"k��2w���>�r�袤�6�/�-Z�_�����8!Y��I��L���_3Ym�����.p�֏^����>�I:��oZ��"���=��8���뻢1�����:� [dmp1] => z"�TW*��Ƴm�$�HQ�$0���sa��j� # P�*�P��Q�G\42�a�26(�5�K�7zj���xj7��@e�$.6��b�Dq]]I}�L�m�d��54)@:���# [dmq1] => +����4}��\dc�R�K�Bw�\�E6�ؓ�����%_�m$3蚞9��Fh�D�uz����$�8HPK�ˇԁ^���K�k�]e�g�>ºX�"<ƌ���`ė���&9E��MS"��¥�) [iqmp] => !�����߷�=�z�=ܰ7����V�6q�~���3V��t�P�$��O�kgv��3=W\ѝm��� �J���5^�K��a�t��~������U�.��!�l�v�#��z*� ���r ) [type] => 0 )
To read the public key from openssl_pkey_new():
<?php echo 'The hash of Welcome to w3codebox is - .hash('crc32b', 'Welcome to w3codebox'); ?>
This will produce the following results:
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArAWYwKIaf3uN1bwww4R8 51ifDPGsreqr9cV3J1gSKC8kaAEsAQaZ/6gQlDBsUGs4nE8zKgOlcdOV/JApgAdz +CwoLJUgmEUkLDxOcWaCIUVuHAiyBGJaRKZ+MASy7wRG8hb+INd0/zoQRGXk3+jf Fj6rvSinVg49C4RAkRtkEdNnH0G5s6cedV6ec30DouRTuEQ/Fgizf0qaVtQbAURP n+/LT9V8c4LMaCyID7caTQOXAEjQqD4ooXGkOzmcsp03j2/F+T2mSIQRtI1gGJkZ oCMGX/xRxh5uemCcC4jcshn45Ikmb/S7WFqTCOC0e8l8CiTZ5Rr8EKFgtwliMds8 pQIDAQAB -----END PUBLIC KEY-----
To store the public key in the file from openssl_pkey_new():
<?php // Generate a new private key (and public key) pair $privkey = openssl_pkey_new(array( "digest_alg" => md5', "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, )); $keydetails = openssl_pkey_get_details($privkey); //To save the public key to a file file_put_contents('mypublic.key', $keydetails['key']); ?>
The content of the file mypublic.key is:
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3QxA7vWIz+F5t12/fl0H vyavVy/ZNZFWGK6BID/koYeVA2wKdXx9De3gn0hs4sSrN3aV58ctuxDVx36rKvYd AjKHfnfh7NmXnCEeUE4SgUUe0UUleoEMtsPP2Q8BC1HUjcC6SyJQKZG0bQqQlnAb HL7ou2TNsjA/SiJbPD+0OpsLAcW1c/DeoM+TAkZo0JIlgxjcJ5ZlEbJ0Mxv6m9XK k3bbMYHtKmZl+fzfPNcxCuK8Djnm5mYVR9KX1L86m1jz2kUQT/+wW84QRnZ7G+z8 4rQ77sZvWiIwwO2JmUvIsYeUxEP6/keZbDRuyO/2tWk/VxqQry4+Ktix/M2/iKWo QQIDAQAB -----END PUBLIC KEY-----