Skip to content

Instantly share code, notes, and snippets.

@jimmy18dev
Last active March 2, 2018 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmy18dev/2f05066072a73518302f03fc48b2501d to your computer and use it in GitHub Desktop.
Save jimmy18dev/2f05066072a73518302f03fc48b2501d to your computer and use it in GitHub Desktop.
php openssl encrypt and decrypt
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