Instantly share code, notes, and snippets.

Embed
What would you like to do?
An AES encryption implementation in PHP with OpenSSL module.
<?php
// Set cipher method
$cipherMethod = 'AES-256-CBC';
// Set encryption key
// The key length must be 256 bits long
$key = (empty($key)) ? openssl_random_pseudo_bytes(32) : '...';
// Set initialization vector
$iv = (empty($iv)) ? openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod)) : '...';
// Encryption
// openssl_encrypt uses PKCS#7 padding by default if the options parameter was set to zero
$encryptedString = openssl_encrypt($rawString, $cipherMethod, $key, 0, $iv);
// Decryption
$decryptedString = openssl_decrypt($encryptedString, $cipherMethod, $key, 0, $iv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment