· 6 years ago · Jun 15, 2019, 04:44 AM
1function my_simple_crypt( $string, $action = 'e' ) {
2 // you may change these values to your own
3 $secret_key = 'abcdefgh';
4 $secret_iv = 'xyz12345';
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 }
14 else if( $action == 'd' ){
15 $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
16 }
17 return $output;
18}