· 7 years ago · Mar 02, 2018, 05:24 AM
1<?php
2
3function dec_enc($action, $string) {
4 $output = false;
5
6 $encrypt_method = "AES-256-CBC";
7 $secret_key = 'This is my secret key';
8 $secret_iv = 'This is my secret iv';
9
10 // hash
11 $key = hash('sha256', $secret_key);
12
13 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
14 $iv = substr(hash('sha256', $secret_iv), 0, 16);
15
16 if( $action == 'encrypt' ) {
17 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
18 $output = base64_encode($output);
19 }
20 else if( $action == 'decrypt' ){
21 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
22 }
23
24 return $output;
25}