在PHP中使用OpenSSL加密和解密数据可以通过以下步骤实现:
$data = 'Hello World';
$key = 'secret_key';
$method = 'AES-256-CBC';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));
$encrypted = openssl_encrypt($data, $method, $key, 0, $iv);
// Store the encrypted data and IV for later decryption
$encryptedData = base64_encode($encrypted);
$ivData = base64_encode($iv);
$encrypted = base64_decode($encryptedData);
$iv = base64_decode($ivData);
$decrypted = openssl_decrypt($encrypted, $method, $key, 0, $iv);
echo $decrypted; // Output: Hello World
请注意,确保在加密和解密数据时使用相同的加密算法、密钥和初始化向量(IV)。此外,建议在存储和传输加密数据时进行base64编码以确保数据的完整性。