當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。