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