- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP Operators
- PHP - Operators
- PHP - Arithmatic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Useful Resources
- PHP - Discussion
PHP - Function openssl_pkey_export_to_file()
Definition and Usage
The openssl_pkey_export_to_file() function will export the key to a file.
Description
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.
Syntax
openssl_pkey_export_to_file ( mixed $key , string $outfilename [, string $passphrase [, array $configargs ]] ) : bool
Parameters
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 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:
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 Values
PHP openssl_pkey_export_to_file() function returns true if there is success and false if failure.
PHP Version
This function will work from PHP Version greater than 5.0.0.
Example 1
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 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-----
Example 2
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-----