在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函数用法,您可以根据具体的需求选择合适的函数来实现您的加密、签名和证书生成等功能。