What are some common PHP functions used for encryption and decryption of strings?

When working with sensitive data, it is important to encrypt the data before storing it and decrypt it when retrieving it. PHP provides several functions for encryption and decryption of strings, such as `openssl_encrypt()` and `openssl_decrypt()`. These functions use secure encryption algorithms to protect the data. By using these functions, you can ensure that your data is securely stored and transmitted.

// Encrypt a string
function encryptString($string, $key) {
    $iv = random_bytes(openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = openssl_encrypt($string, 'aes-256-cbc', $key, 0, $iv);
    return base64_encode($iv . $encrypted);
}

// Decrypt a string
function decryptString($string, $key) {
    $data = base64_decode($string);
    $iv = substr($data, 0, openssl_cipher_iv_length('aes-256-cbc'));
    $encrypted = substr($data, openssl_cipher_iv_length('aes-256-cbc'));
    return openssl_decrypt($encrypted, 'aes-256-cbc', $key, 0, $iv);
}

// Usage
$key = 'secret_key';
$originalString = 'Hello, world!';
$encryptedString = encryptString($originalString, $key);
$decryptedString = decryptString($encryptedString, $key);

echo 'Original String: ' . $originalString . PHP_EOL;
echo 'Encrypted String: ' . $encryptedString . PHP_EOL;
echo 'Decrypted String: ' . $decryptedString . PHP_EOL;