php 2 way encryption with salt code example
Example: php two way encrypt decrypt
function encryptor($action, $string) {
$output = FALSE;
$encrypt_method = "AES-256-CBC";
$secret_key = 'SecretKeyWord';
$secret_iv = 'SecretIV@123GKrQp';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if ($action == 'encrypt') {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
} elseif ($action == 'decrypt') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
function encrypt($data) {
return urlencode(self::encryptor('encrypt', self::sanitize($data)));
}
function decrypt($data) {
return self::encryptor('decrypt', urldecode(self::sanitize($data)));
}
?>