· 5 years ago · Jul 04, 2020, 01:52 PM
1function my_crypt($string, $action = 'E')
2{
3 $secret_key = 'my_simple_secret_key';
4 $secret_iv = 'my_simple_secret_iv';
5
6 $output = false;
7 $encrypt_method = "AES-256-CBC";
8 $key = hash('sha256', $secret_key);
9 $iv = substr(hash('sha256', $secret_iv), 0, 16);
10
11 if ($action == 'E') {
12 $output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
13 } else if ($action == 'D') {
14 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
15 }
16
17 return $output;
18}