What is the purpose of using AES 128 encryption in PHP?

AES 128 encryption in PHP is used to securely encrypt sensitive data, such as passwords or personal information, before storing or transmitting it. This encryption method provides a high level of security by using a 128-bit key to encrypt and decrypt the data. By implementing AES 128 encryption in PHP, you can ensure that your data is protected from unauthorized access and maintain the confidentiality of your information.

// Encrypt data using AES 128 encryption
function encryptData($data, $key) {
    $cipher = "aes-128-cbc";
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv);
    return base64_encode($iv . $encrypted);
}

// Decrypt data using AES 128 encryption
function decryptData($data, $key) {
    $cipher = "aes-128-cbc";
    $data = base64_decode($data);
    $ivlen = openssl_cipher_iv_length($cipher);
    $iv = substr($data, 0, $ivlen);
    $encrypted = substr($data, $ivlen);
    return openssl_decrypt($encrypted, $cipher, $key, 0, $iv);
}

// Usage
$data = "Sensitive information";
$key = "YourSecretKey";
$encryptedData = encryptData($data, $key);
echo "Encrypted data: " . $encryptedData . "\n";
$decryptedData = decryptData($encryptedData, $key);
echo "Decrypted data: " . $decryptedData . "\n";