在PHP中,OpenSSL庫提供了一組函數來執行加密和解密操作、生成數字證書以及驗證簽名等功能。以下是一些常見的OpenSSL函數用法示例:
$keypair = openssl_pkey_new(array(
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
));
openssl_pkey_export($keypair, $privateKey);
$publicKey = openssl_pkey_get_details($keypair)['key'];
$data = 'Hello, World!';
$encrypted = '';
openssl_public_encrypt($data, $encrypted, $publicKey);
$decrypted = '';
openssl_private_decrypt($encrypted, $decrypted, $privateKey);
echo $decrypted; // Output: Hello, World!
$data = 'Hello, World!';
$signature = '';
openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256);
$verified = openssl_verify($data, $signature, $publicKey, OPENSSL_ALGO_SHA256);
echo $verified; // Output: 1 (verified)
$dn = array(
'countryName' => 'US',
'organizationName' => 'Example Company',
'commonName' => 'example.com',
);
$csr = openssl_csr_new($dn, $privateKey);
$cert = openssl_csr_sign($csr, null, $privateKey, 365);
openssl_x509_export($cert, $certout);
echo $certout;
這些示例展示了一些常見的OpenSSL函數用法,您可以根據具體的需求選擇合適的函數來實現您的加密、簽名和證書生成等功能。