· 9 years ago · Jan 10, 2017, 04:22 PM
1function sif($action, $string) {
2 $output = false;
3
4 $encrypt_method = "AES-256-CBC";
5 $secret_key = 'oIp2cWHhlXK6OYMI0CfBCFRT';
6 $secret_iv = 'TGjiCJdRj6UW161qtqkjFGTZ';
7
8 // hash
9 $key = hash('sha256', $secret_key);
10
11 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
12 $iv = substr(hash('sha256', $secret_iv), 0, 16);
13
14 if( $action == 'encrypt' ) {
15 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
16 $output = base64_encode($output);
17 }
18 else if( $action == 'decrypt' ){
19 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
20 }
21
22 return $output;
23}