PHP - Function openssl_pkey_export_to_file()

The openssl_pkey_export_to_file() function will export the key to a file.
The openssl_pkey_export_to_file() function saves the key in a PEM format.PEM stands for Privacy-Enhanced Mail and it mainly stores  cryptographic keys and certificates.
openssl_pkey_export_to_file ( mixed $key , string $outfilename [, string $passphrase [, array $configargs ]] ) : bool

key
The key you want to export to file.
outfilename
The outfilename is the path where you want to save the .pem file.
passphrase
The password you can use to protect the file.
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.
By default openssl_pkey_new() makes use of configuration details present in openssl.cnf. But using configargs you can overwrite them.The config details are as follows:
PHP openssl_pkey_export_to_file() function returns true if there is success and false if failure.
This function will work from PHP Version greater than 5.0.0.
Working of openssl_pkey_export_to_file() −
<?php
   //creating private key
   $privkey = openssl_pkey_new();
   openssl_pkey_export_to_file($privkey, 'C:/xampp/htdocs/modules/openssl/keytest.pem');
?>
The details stored in 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
IeodnVsFG2tEru3YphqAwwdvuBNFblS8q084WzA3wa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hAmzE8/hTD9eaH
-----END PRIVATE KEY-----

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"]);	
   }
?>
The details stored in file test-csr1.pem is as follows −
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnbGAblUCP6sZvbi2JM4G
PcLmKK9flGQq6EE+yvMWrEUnIPGOLJUEB06dE8inIMQTYDmdmjn7HgUAlIDaViw+
aSv3XBZJqBTm7xtW4488oKuCzdBuJbnomtGttFWOk4cjb8kG+Kza6v0AB+P0eUru
cPkwnJF4F1pGY1pszSMsL3/s7OcIzjiKUKu5vHJxUWO7baVovq+gv9+O24Pd0GhB
t6RC/pgK/42YqZ2YW5V+JiuCFFkTVPusviunAOLLpDwoyPIROvXdML2TKopiowOd
nmOkNRrW81duRF8Z4dcn7jnf2sKExpLBgAtcV05OufZIv3VeeTD0ISbCJhn0zmx/
SQIDAQAB
-----END PUBLIC KEY-----


    45 Lectures 
     9 hours 

 Malhar Lathkar

    34 Lectures 
     4 hours 

 Syed Raza

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    17 Lectures 
     1 hours 

 Nivedita Jain

    100 Lectures 
     34 hours 

 Azaz Patel

    43 Lectures 
     5.5 hours 

 Vijay Kumar Parvatha Reddy
 Print
 Add Notes
 Bookmark this page