PHP openssl_pkey_new() Function Last Updated : 19 May, 2023 Comments Improve Suggest changes Like Article Like Report The openssl_pkey_new() function is an inbuilt function in PHP cryptography extension that is used to generate a new private/public key pair using the OpenSSL library. Syntax: openssl_pkey_new(?array $options = null): OpenSSLAsymmetricKey|false Parameters: This function accepts one parameter which is described below. $options: An optional array of configuration options that can be used to customize the key generation process. This can include options such as the key type, key length, and digest algorithm. Return Values: The return value of openssl_pkey_new() is a new OpenSSL key pair resource that can be used with other OpenSSL functions. This resource represents both the private and public key components of the generated key pair. If it fails, it will return false. Example 1: The following program demonstrates the openssl_pkey_new() function. PHP <?php $config = [ "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, ]; $keypair = openssl_pkey_new($config); openssl_pkey_export($keypair, $private_key); $public_key = openssl_pkey_get_details($keypair); $public_key = $public_key["key"]; echo "Private key: " . $private_key . "\n"; echo "Public key: " . $public_key . "\n"; ?> Output: Private key: -----BEGIN PRIVATE KEY----- MIIEvAIBADANBgkqhkiG9w0... ... ...o9NsYUeNhM2eTDd5KnyQA== -----END PRIVATE KEY----- Public key: -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQE... ... ...JmQQIDAQABDGSFdgKHJhgER -----END PUBLIC KEY----- Example 2: The following program demonstrates the openssl_pkey_new() function. PHP <?php $config = [ "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA, ]; $keypair = openssl_pkey_new($config); if ($keypair) { openssl_pkey_export($keypair, $private_key); if ($private_key) { echo "Private key successfully exported\n"; } else { echo "Error exporting private key\n"; } } else { echo "Error generating key pair\n"; } ?> Output: Private key successfully exported Reference: https://www.php.net/manual/en/function.openssl-pkey-new.php Comment More infoAdvertise with us Next Article PHP openssl_pkey_new() Function neeraj3304 Follow Improve Article Tags : PHP PHP-function PHP-OpenSSL Similar Reads PHP openssl_pkey_export() Function The openssl_pkey_export() function is an inbuilt function in PHP which is used to view and manage private keys and public keys. It gets an exportable representation of a key into a string. It exports the key as a PEM encoded string and stores to pass by reference. Syntax: bool openssl_pkey_export( m 2 min read PHP openssl_spki_verify() Function The openssl_spki_verify() function is a built-in function in PHP and is used to validate the supplied signed public key and challenge. This should be the public key corresponding to the private key used for the signature. It verifies a signed public key and challenge. Syntax: string openssl_spki_ver 2 min read PHP openssl_verify() Function The openssl_verify() function is an inbuilt function in PHP which is used to verifies that if the signature is correct for the specified data using the public key associated with public_key or not. This must be the public key corresponding to the private key used for signing. Syntax:Â openssl_verify 2 min read PHP openssl_pkcs12_read() Function The openssl_pkcs12_read() function is a built-in function in PHP and is used by the PKCS # 12 certificate store to convert it into an array provided by pkcs12. A PKCS #12 file may be encrypted and signed. Syntax: bool openssl_pkcs12_read( string $pkcs12, array &$certs, string $pass ) Parameters: 2 min read PHP openssl_pkcs12_export() Function The opensl_pkcs12_export() function is a built-in function in PHP which is used to store in a string called x509 in a PKCS # 12 file format. Certificate store file corresponding to PKCS # 12 variable. Syntax: bool openssl_pkcs12_export( mixed $x509, string &$out, mixed $priv_key, string $pass [, 2 min read PHP openssl_pbkdf2() Function The openssl_pbkdf2() function is an inbuilt function in PHP that implements the Password-Based Key Derivation Function 2 (PBKDF2) algorithm provided by the OpenSSL library. The algorithm is designed to be slow and computationally intensive, making it resistant to brute-force attacks. Syntax: openssl 2 min read PHP openssl_digest() Function The openssl_digest() function is an inbuilt function in PHP that is used to compute a digest hash value for the given data using a given method and returns a raw or binary hex-encoded string. Syntax: openssl_digest( string $data, string $digest_algo, bool $binary = false): string|false Parameters: T 2 min read PHP openssl_get_curve_names() Function The openssl_get_curve_names() function is an inbuilt function in PHP which is used to curve names in Elliptic curve cryptography. The two most widely standardized or supported curves are prime256v1 (NIST P-256) and secp384r1 (NIST P-384). The curve names usually contain a number which is the number 2 min read PHP | inet_ntop() Function The inet_ntop() function is an inbuilt function in PHP which converts a 32bit IPv4 or 128bit IPv6 address into a readable format. Syntax: string inet_ntop( string $ip_address ) Parameter: This function accepts one parameter as mentioned above and described below: $ip_address: It is required paramete 1 min read PHP openssl_get_md_methods() Function The openssl_get_md_methods() function is an inbuilt function in PHP that is used to retrieve a list of available digest (message digest) methods supported by OpenSSL. Syntax: openssl_get_md_methods(bool $aliases = false): array Parameters: This function accepts one parameter which is described below 2 min read Like