Skip to content

Instantly share code, notes, and snippets.

@gundamew
Last active June 1, 2016 17:34
Show Gist options
  • Save gundamew/3462de49997eeb1fe34753d6c27f7646 to your computer and use it in GitHub Desktop.
Save gundamew/3462de49997eeb1fe34753d6c27f7646 to your computer and use it in GitHub Desktop.
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