当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP openssl_error_string()用法及代码示例


openssl_error_string() 函数是 PHP 中的一个内置函数,用于从 openSSL 库中获取最后一个错误。错误消息是排队的,所以应该多次调用这个函数来收集所有的信息。最后一个错误将是最近的错误。

用法:

openssl_error_string():string|false

参数:这个函数没有参数。

返回值:返回错误消息字符串,如果没有更多错误消息要返回,则返回 false。



下面的示例说明了 PHP 中的 openssl_error_string() 函数。

范例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";
  
?>

输出:

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

范例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";
}
  
?>

输出:



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

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




相关用法


注:本文由纯净天空筛选整理自Shivam.Pradhan大神的英文原创作品 PHP openssl_error_string() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。