· 6 years ago · Apr 29, 2019, 08:10 AM
1<?
2/**
3 * Function to encrypt and decrypt a string.
4 * @param $string
5 * @param string $action
6 *
7 * @return bool|string
8 */
9function _custom_encrypt_decrypt($string, $action = 'encrypt') {
10 $secret_key = 'k;Yj>+kZ@CHBJQDs*G=2!uRXE+4;)H(M';
11 $secret_iv = 'F5a:~+EQAj#:>zazNWN6GP3JEWX75h-@';
12 $output = FALSE;
13 $encrypt_method = "AES-256-CBC";
14 $key = hash('sha256', $secret_key);
15 $iv = substr(hash('sha256', $secret_iv), 0, 4);
16 if ($action == 'encrypt') {
17 $output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
18 }
19 elseif ($action == 'decrypt') {
20 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
21 }
22 return $output;
23}