Last active
June 1, 2016 17:34
-
-
Save gundamew/3462de49997eeb1fe34753d6c27f7646 to your computer and use it in GitHub Desktop.
An AES encryption implementation in PHP with OpenSSL module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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