· 5 years ago · Jul 01, 2020, 05:56 AM
1<?php
2function encrypt_decrypt($action, $string) {
3 $output = false;
4 $encrypt_method = "AES-256-CBC";
5 $secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxx';
6 $secret_iv = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
7 $key = hash('sha256', $secret_key);
8 $iv = substr(hash('sha256', $secret_iv), 0, 16);
9 switch ($action) {
10 case "encrypt": $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv); $output = base64_encode($output); break;
11 case "decrypt": $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); break;
12 default:
13 }
14 return $output;
15}
16?>