· 7 years ago · Apr 19, 2018, 07:50 AM
1<?php
2function encrypt_decrypt($string, $user_id, $action = 'encrypt') {
3 $secret_key = $user_id;
4 $secret_iv = $user_id;
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 == 'encrypt' ) {
12 $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
13 } else if( $action == 'decrypt' ){
14 $output = stripslashes(openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv ));
15 }
16 return $output;
17}