Skip to content
Related Articles

Related Articles

Improve Article

PHP openssl_pkey_export() Function

  • Last Updated : 13 Sep, 2020

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( mixed $key, string $out [, string $passphrase [, array $configargs ]])

Parameters: This function accepts four parameters as mentioned above and described below.

  • $key: This key is passed as a PEM encoded string.
  • $out: This variable is passed by reference which holds PKCS#12 on successful execution of the above function.
  • $passphrase: This parameter is used to control access to a computer system, program or data.
  • $configargs: The “configargs” parameter is used to initialize the request. The developer can also mention an alternative openssl configuration file by setting the configuration key value to the file path that will be used.

Return Values: This function returns TRUE on success or FALSE on failure.

Below program illustrate the openssl_pkey_export() function in PHP.



Program:

PHP




<?php 
  
// Create the keypair
$res = openssl_pkey_new();
       
// Get private key
openssl_pkey_export($res, $privkey
                    "PassPhrase number 1"); 
{
        
    // Get details of public key
    $pubkey = openssl_pkey_get_details($res);
    $pubkey = $pubkey["key"];
    var_dump($privkey);
    var_dump($pubkey);
}
  
?>  

Output:

string(1854) "-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIxb/qYev3FEkCAggA
MAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECL2Qz46mZsMgBIIEyL0r4vpwOpt9
BtBmsrL8aXNsy8Da8FdwN5Ll3ZtuYnKxUWOyRByuQNxHWiVkkZhZ5R9zS+6yKN1q
DxGqkUo6PBRmIYEHfYJ/oN0FQAPTEEQ2GtUimt2HxFIHL1WfSYM7UfYFUSD5AImM
BwTtnIcSwXv3IfZHu5Bq8mOrRt6UmVXYwLT3jpeW1xX5Y3HjyyepPV13PXbxjGPL
OgtH6J+CaCPvmmUgj8FJAaqJsTPed6vjHftSwp+vZRpeS9teuPXLwK4GVjsPuhDo
nK7CQL+BEtPH1oZ0T0/uG1aRkkms0ul29L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3b+Qg5LWLhU1ZFo5512IaHgpLWsAHcOwrqwZ1TTdwjJCoz5gf8o28
VCxC/EoFLv8nTItebqV9lkrd9NWraXPkfPPqrIi1cPmHQSAwisgfdfSrJaIqX9Sm
aQQ0dxFaVzhXHYqu4MOQa3fQO/Jfr8Q0ZFnX7Eg/FfgatCFQYYMKg2+tWmD/qGgQ
31dKhjo3VgtEubC/o9D+TJq17NH4FdRhhgv6JG6MM1MGY2UPLjkJfgYXfmu5/t65
BnG9JjwWVy3g7skKFqTKpw==
-----END ENCRYPTED PRIVATE KEY-----
"
string(451) "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtrfq8MIME9BbbmcLlPiQ
Xdsd4tIAaWU6pJZQdUpipRSdgbBRAxTh/z7yXAeIooSt2w9PdwYEPMYmNNkH596D
bHgezBq0mR7AUGIth2mVSspntfehu+Yyp7GiLm4ZK7Bv7rKqKMo8KSGXrJGSly6l
yOlgVDMRgxBNPxcCqHmU2lIr3RmFb5gAxyxk2cRA3WIok58DltlHn7InX1KQMbGb
QqRttSyuZNgFWA8r8O7Ug15abSs71tAboJZWS/NYQDeY6jxSVxgj3HFX7hQ7VRZp
3hXyAArqjcvcvcWtpWcc3CQ/BjZOTsDXyjuokxGyiTjD2p7VSHFGXwanzE69bwTA
kQIDAQAB
-----END PUBLIC KEY-----
"

Reference: https://www.php.net/manual/en/function.openssl-pkey-export.php

My Personal Notes arrow_drop_up
Recommended Articles
Page :