1function encrypt_decrypt($string, $action = 'encrypt')
2{
3 $encrypt_method = "AES-256-CBC";
4 $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
5 $secret_iv = '5fgf5HJ5g27'; // user define secret key
6 $key = hash('sha256', $secret_key);
7 $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
8 if ($action == 'encrypt') {
9 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
10 $output = base64_encode($output);
11 } else if ($action == 'decrypt') {
12 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
13 }
14 return $output;
15}
16
17echo "Your Encrypted password is = ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
18echo "Your Decrypted password is = ". encrypt_decrypt($pwd, 'decrypt');
19
1function encryptor($action, $string) {
2 $output = FALSE;
3 $encrypt_method = "AES-256-CBC";
4 $secret_key = 'SecretKeyWord';
5 $secret_iv = 'SecretIV@123GKrQp';
6 // hash
7 $key = hash('sha256', $secret_key);
8 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
9 $iv = substr(hash('sha256', $secret_iv), 0, 16);
10 //do the encryption given text/string/number
11 if ($action == 'encrypt') {
12 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
13 $output = base64_encode($output);
14 } elseif ($action == 'decrypt') {
15 //decrypt the given text/string/number
16 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
17 }
18 return $output;
19}
20
21function encrypt($data) {
22 return urlencode(self::encryptor('encrypt', self::sanitize($data)));
23}
24
25function decrypt($data) {
26 return self::encryptor('decrypt', urldecode(self::sanitize($data)));
27}
28// Now you can just call encrypt($string) or decrypt($string)
29?>