How do you encrypt data in PHP

In PHP, you can encrypt data using various methods. One common approach is using the OpenSSL functions provided by PHP. Below is a simple example of how to encrypt and decrypt a string using OpenSSL:

<?php // Encryption $data = "Sensitive Information"; $key = "your-encryption-key"; $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $encrypted = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv); $encodedData = base64_encode($encrypted . '::' . $iv); echo "Encrypted Data: " . $encodedData . "<br>"; // Decryption list($encryptedData, $iv) = explode('::', base64_decode($encodedData), 2); $decrypted = openssl_decrypt($encryptedData, 'aes-256-cbc', $key, 0, $iv); echo "Decrypted Data: " . $decrypted; ?>

PHP encryption OpenSSL AES secure data