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_export_to_file() Function.
The built-in openssl_pkey_export_to_file() function exports the key to a file.
This PHP function saves the key in a PEM format. PEM is an acronym which stands for Privacy-Enhanced Mail and it is mainly used for storing cryptographic keys and certificates.
The built-in openssl_pkey_export_to_file() function exports the key to a file.
This PHP function saves the key in a PEM format. PEM is an acronym which stands for Privacy-Enhanced Mail and it is mainly used for storing cryptographic keys and certificates.
syntax
Following below is the syntax to use this function -
openssl_pkey_export_to_file ( mixed $key , string $outfilename [, string $passphrase [, array $configargs ]] ) : bool
READ: PHP | openssl_pkey_get_public() Function
Parameter Details
Sr.No | Parameter | Description |
---|---|---|
1 | key | The key you want to export to file. |
2 | outfilename | The outfilename is the path where you want to save the .pem file. |
3 | passphrase | The password you can use to protect the file. |
4 | configargs | The parameter configargs is the configuration details that needs to be given to the function to generate the private/public key pair.The details are described below. |
configargs
By default the openssl_pkey_export_to_file() function makes use of configuration details present in openssl.cnf. But using configargs you can overwrite them. The config details are as follows -
key in configargs | type | key used in openssl.conf | description |
---|---|---|---|
digest_alg | string | default_md | Digest methods that you get from openssl_get_md_methods(). |
x509_extensions | string | x509_extensions | Extensions used when creating an x509 certificate. |
req_extensions | string | req_extensions | Extensions used when creating a CSR. |
private_key_bits | integer | default_bits | Specifies how many bits to be used while generating a private key. |
private_key_type | integer | none | The type of private key to create. 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 | Whether the exported key be encrypted? |
encrypt_key_cipher | integer | none | Cipher constants like OPENSSL_CIPHER_RC2_40 (integer),OPENSSL_CIPHER_RC2_128 (integer),OPENSSL_CIPHER_RC2_64 (integer),OPENSSL_CIPHER_DES (integer), OPENSSL_CIPHER_3DES (integer) etc. |
curve_name | string | none | One of the curve name returned by this function openssl_get_curve_names(). |
config | string | N/A | You can change the configuration in openssl.conf as per your requirement and give the path of it here. |
Return Value
This PHP function returns true on success, else false on failure.
PHP Version
This PHP function works from PHP version greater than 5.0.0.
Example1
The following example illustrates the usage of the built-in openssl_pkey_export_to_file() function -
php
//creating private key
$privkey = openssl_pkey_new();
openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/keytest.pem');
?>
Output
When the above code is executed, it will produce the following result -
-----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-----
Example2
To fetch the key from .pem file -
php
//creating private key
$privkey = openssl_pkey_new();
openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/keytest.pem');
//using .pem file with 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"]);
}
?>
Output
When the above code is executed, it will produce the following result -
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnbGAblUCP6sZvbi2JM4G
PcLmKK9flGQq6EE+yvMWrEUnIPGOLJUEB06dE8inIMQTYDmdmjn7HgUAlIDaViw+
aSv3XBZJqBTm7xtW4488oKuCzdBuJbnomtGttFWOk4cjb8kG+Kza6v0AB+P0eUru
cPkwnJF4F1pGY1pszSMsL3/s7OcIzjiKUKu5vHJxUWO7baVovq+gv9+O24Pd0GhB
t6RC/pgK/42YqZ2YW5V+JiuCFFkTVPusviunAOLLpDwoyPIROvXdML2TKopiowOd
nmOkNRrW81duRF8Z4dcn7jnf2sKExpLBgAtcV05OufZIv3VeeTD0ISbCJhn0zmx/
SQIDAQAB
-----END PUBLIC KEY-----
READ: PHP | openssl_pkey_get_private() Function
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_private_encrypt() 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.
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.