English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The openssl_pkey_export_to_file() function is used to export the key to a file.
openssl_pkey_export_to_file()The function exports the key to a file.
The openssl_pkey_export_to_file() function saves the key in PEM format, where PEM stands for Privacy Enhanced Mail.-Enhanced Mail, mainly used for storing encrypted keys and certificates.
openssl_pkey_export_to_file ( mixed $key , string $outfilename [, string $passphrase [, array $configargs ] ] ) : bool
Serial number | Parameters | Description |
---|---|---|
1 | key | The key to be exported to the file. |
2 | outfilename | outfilename is the path to save the .pem file. |
3 | passphrase | Password that can be used to protect the file. |
4 | configargs | The parameter configargs is required to be provided to the function to generate the private key/The detailed configuration information for the public key pair, as described below. |
By default, openssl_pkey_new() uses the detailed configuration information that exists in openssl.cnf. However, configargs can override them. The detailed configuration information is as follows:
configargs | Type | Keys used in openssl.conf | Description |
---|---|---|---|
digest_alg | String | default_md | The digest method you obtain 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_DES (integer), OPENSSL_CIPHER_3DES (integer) and others. |
curve_name | String | none | One of the curve names returned by the openssl_get_curve_names() function. |
config | String | N/A | You can change the configuration in openssl.conf as needed and provide its path here. |
If the PHP openssl_pkey_export_to_file() function succeeds, it returns true; otherwise, it returns false.
This function will start from versions greater than5Starting from PHP version .0.0
The function of openssl_pkey_export_to_file()-
<?php //Create private key $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/keytest.pem'); ?>
The detailed information stored in the file keytest.pem is as follows-
-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDaqNK97A+mL9Xu IDt3rz9yfFUvrLcDEvsDa9JsjQByJVbdRtaNl6nfg91/LfKO8zAeG8srd292jcYk 9MgBhkpMCHvF/QhWjA4IdPLdWHCbYfjF/LHmo/z022/FqTnjQtFws992/ClhZdo6 kpDlU/H2lmbnCwrsqHlqcQ7bzBgC5U5SW0t3A03PSqxQTIFPOHi1Yx1Il5jH/H11 6UXDKogAWsseRpdwVdsCy6Wj3rkybr1pr7CDkHSS49MAvJ4e6xhs+je12lrtyChR ZTIYLICzEG7a1n0BPGAI1bQcivHXNipUkAYFn221gKRuB+9SQvC3VKbNXy8Oc7N9 HEahD8S3AgMBAAECggEBAKzEU68og7zlcvzxjsskNtd4kb5Xk0rkhlzPprWKO131 TssLm57IxLoMcMh6P3rff5dqkn9HoVRk9LhiiF1cA/xLf7CSGzJ2+ueHsBVgOaks IeodnVsFG2tEru3YphqAwwdvuBNFblS8q084WzA3waj6cVgAi6MuArEtn3XfruEp Yryc4Y1I1SB92x4y85tZ/PcomumPH0djKQeuhzy7f7GloJRfdshNENRbkdLc65N5 j8hy7WxMSa0dpJ3ZJMmgNfek9nALntSZfOsHGMZ/Wog8eV6+HzCwqqrMkR15pZI1 HqvVszU1iwoUJvlGoxInJOqJ2c6lBSBOBBR8DuuQixECgYEA+8RKXcw0U3VU8zJO NTFzSDEtFYKZ5Bg4IPaYSTSo/ojiL3VrLeocRq3/2zdeCw8wx9eNZbcBW93lWVxK q2G0X4XgonorEEONBvL9aE/D7wBCMYPWDXd/KQVZW8CPwcy10g2oIi3SqbcTQ/gT fcmcHAQD2wVgo9XBlg24ESAP01MCgYEA3lYGasOvDweca5GCiP4m1oOH605haIUU f5CDWXbZ6QjcoUQQB0CoDtTl3QpBd3KGbd+PbqU8xb44+LhrVIsjUyZs6k+eLACe Dufzq00mIRSl/TZ0R3q17lAMmxId9QramDScpmqqqXonpOpdEoonThynhLyANgX3 eYGLXeqaII0CgYBPVi/JFwx2MEcwy+1xPcACQ9zdJmawRiGJ4atjhkCq1R/RrMK1 mUyHyVUTE4ODIKpSj05zexPmiyo22qp9DzDz2RBMowrm+SJ7yh6ovFoV+pLhX5YY cEuV9aWPEEM84vF42+zbuGzmJlbf2FDsFpgnC+zbG/q0Jiv2ySPz4ZKbGQKBgQDM ek9ih1+LshNAts1Xkm5DoSoy1Z4uUx48B7tVX0If2N+YjRE0qlklctWIiXMWGMTb bdzrBJq0vjKFRI6pbWFqio9mmxy8GUFEMjzekZB8ohHao+cjCg8iAorlXy8f+wB5 NQHQ547XWRn2yPgaIebuJtpF8Fr11Fz6aZK0KBvhzQKBgGRwuxq6IhIROupoDRpU RHuqICeQQYcf7Cfk7+ZyYJnA1fbOowj4Q5zvbWa6N2Ygyq2KIl0P5YL4Atb7aRKS e6ol8lIKZM9ysbS+wR0OhhTJs/9CqpgvDbYNQFiaVZtGRpSNCxHkhn0cAR7lzK4P ROQC7p9zXJhAmzE8/hTD9eaH -----END PRIVATE KEY-----
Obtain the key from the .pem file-
<?php //Create private key $privkey = openssl_pkey_new(); openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/keytest.pem'); //Use the .pem file with the private key. $testprivatekey = openssl_get_privatekey(file_get_contents('C:/xampp/htdocs/modules/openssl/keytest.pem')); if ($testprivatekey === false) { var_dump(openssl_error_string()); } else { //var_dump($testprivatekey); $key_details = openssl_pkey_get_details($testprivatekey); print_r($key_details["key"]); } ?>
stored in file test-csr1Detailed information in .pem as follows-
-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnbGAblUCP6sZvbi2JM4G PcLmKK9flGQq6EE+yvMWrEUnIPGOLJUEB06dE8inIMQTYDmdmjn7HgUAlIDaViw+ aSv3XBZJqBTm7xtW4488oKuCzdBuJbnomtGttFWOk4cjb8kG+Kza6v0AB+P0eUru cPkwnJF4F1pGY1pszSMsL3/s7OcIzjiKUKu5vHJxUWO7baVovq+gv9+O24Pd0GhB t6RC/pgK/42YqZ2YW5V+JiuCFFkTVPusviunAOLLpDwoyPIROvXdML2TKopiowOd nmOkNRrW81duRF8Z4dcn7jnf2sKExpLBgAtcV05OufZIv3VeeTD0ISbCJhn0zmx/ SQIDAQAB -----END PUBLIC KEY-----