Open In App

PHP openssl_error_string() Function

Last Updated : 05 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The openssl_error_string() function is an inbuilt function in PHP which is used to get  the last error from the openSSL library. Error messages are queued, so this function should be called multiple times to collect all of the information. The last error will be the most recent one.

Syntax:

openssl_error_string(): string|false

Parameters: This function has no parameters.

Return Values: Returns an error message string, or false if there are no more error messages to return.

Below examples illustrate the openssl_error_string() function in PHP.

Example 1:

PHP




<?php
    
// Encrypt method to use
$encrypt_method = "AES-256-CBC";
  
$secret_key = "KEY";
  
$secret_iv = "KEY";
  
$string = "geeksforgeeks";
  
// Generate hash key using sha 256
$key = hash('sha256', $secret_key);
  
// iv - encrypt method AES-256-CBC
$iv = substr(hash('sha256', $secret_iv) , 0, 16);
  
// Generate the encrypted text
$output = openssl_encrypt(
      $string, $encrypt_method, $key, 0, $iv);
  
// Print encrypted output string
echo $output;
  
echo "\n\n:::: Listed are open ssl "error while encryption:::\n\n";
while ($msg = openssl_error_string()) echo $msg . "\n";
  
?>


Output:

SXuR6GPO5Kk6WJcS0zuG4A==
:::: Listed are open ssl error while encryption:::
error:0607A082:digital envelope routines:EVP_CIPHER_CTX_set_key_length:invalid key length

Example 2:

PHP




<?php
    
// For SSL server certificates the commonName
// is the domain name to be secured
// For S/MIME email certificates the commonName
// is the owner of the email address
// Location and identification fields refer to
// the owner of domain or email subject to be
// secured
$dn = array(
    "countryName" => "IN",
    "stateOrProvinceName" => "Delhi",
    "localityName" => "Delhi",
    "organizationName" => "Geeks for geeks",
    "organizationalUnitName" => "PHP Documentation Team",
    "commonName" => "Geeks",
    "emailAddress" => "wez@example.com"
);
  
// Generate a new private (and public) key pair
$privkey = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));
  
// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey, array(
    'digest_alg' => 'sha256'
));
  
// Generate a self-signed cert,
// valid for 365 days
$x509 = openssl_csr_sign($csr,
    null, $privkey, $days = 365, array(
    'digest_alg' => 'sha256'
));
  
// Save your private key, CSR and 
// self-signed cert for later use
dopenssl_pkey_export($privkey, $pkeyout, "mypassword")
              and var_dump($pkeyout);
  
// Show any errors that occurred here
while (($e = openssl_error_string()) !== false) {
    echo $e . "\n";
}
  
?>


Output:

string(1854) “—–BEGIN ENCRYPTED PRIVATE KEY—–

MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIPbMk8XYJMNECAggA

MAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECFEhHuj2fzInBIIEyNt/L/HxdXnw

VJdlJUaG8He7uIjIa5b8/JVAjd1r5jZqLrSztNG5KiPUljGJxYzFTx/eyD6mmVp4

VrOnOrFyRwHBYQAJyTdEkYQ8J6ogBUdHY724uIIqHcbMo3hKFn0te8xDVWhn0klV

fdTbhJfGvYlU+HmJRmmR65Ser38CLsSxR55/TUx8PPUzY4683VdWS40AIQeCoeVe

yn68eN4ylbBWg0N9Zor3D8k8+qmOGTZp3RK69ddavIscEObDtw6iGDbxXa6Q9QC+

B7/u8wYRBTz0XMLapm4+JFi1F4cQZCFP7pC9VJwiGvfo4hYAT0P3S2eEzCZllF9N

mFNDjU1AIHpTSD87ZQr”…

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

error:0E06D06C:configuration file routines:NCONF_get_string:no value

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads