PHP openssl_error_string() Function
Last Updated :
05 Nov, 2021
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 = "AES-256-CBC" ;
$secret_key = "KEY" ;
$secret_iv = "KEY" ;
$string = "geeksforgeeks" ;
$key = hash( 'sha256' , $secret_key );
$iv = substr (hash( 'sha256' , $secret_iv ) , 0, 16);
$output = openssl_encrypt(
$string , $encrypt_method , $key , 0, $iv );
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
$dn = array (
"countryName" => "IN" ,
"stateOrProvinceName" => "Delhi" ,
"localityName" => "Delhi" ,
"organizationName" => "Geeks for geeks" ,
"organizationalUnitName" => "PHP Documentation Team" ,
"commonName" => "Geeks" ,
"emailAddress" => "wez@example.com"
);
$privkey = openssl_pkey_new( array (
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));
$csr = openssl_csr_new( $dn , $privkey , array (
'digest_alg' => 'sha256'
));
$x509 = openssl_csr_sign( $csr ,
null, $privkey , $days = 365, array (
'digest_alg' => 'sha256'
));
dopenssl_pkey_export( $privkey , $pkeyout , "mypassword" )
and var_dump( $pkeyout );
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
Share your thoughts in the comments
Please Login to comment...