Last active
March 2, 2018 04:04
-
-
Save jimmy18dev/2f05066072a73518302f03fc48b2501d to your computer and use it in GitHub Desktop.
php openssl encrypt and decrypt
This file contains hidden or 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
private function Encrypt($data){ | |
$key = $this->key; | |
$password = $this->cookie_salt; | |
$encryption_key = base64_decode($key.$password); | |
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); | |
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv); | |
return base64_encode($encrypted . '::' . $iv); | |
} | |
private function Decrypt($data){ | |
$key = $this->key; | |
$password = $this->cookie_salt; | |
$encryption_key = base64_decode($key.$password); | |
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2); | |
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment