· 7 years ago · Jul 13, 2018, 07:54 AM
1<?php if (!defined("BASEPATH")) exit("No direct script access allowed");
2
3function encrypt_url($string) {
4
5 $output = false;
6 /*
7
8 * read security.ini file & get encryption_key | iv | encryption_mechanism value for generating encryption code
9
10 */
11 $security = parse_ini_file("security.ini");
12 $secret_key = $security["encryption_key"];
13 $secret_iv = $security["iv"];
14 $encrypt_method = $security["encryption_mechanism"];
15
16 // hash
17 $key = hash("sha256", $secret_key);
18
19 // iv – encrypt method AES-256-CBC expects 16 bytes – else you will get a warning
20 $iv = substr(hash("sha256", $secret_iv), 0, 16);
21
22 //do the encryption given text/string/number
23 $result = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
24 $output = base64_encode($result);
25 return $output;
26}
27
28
29
30function decrypt_url($string) {
31
32 $output = false;
33 /*
34 * read security.ini file & get encryption_key | iv | encryption_mechanism value for generating encryption code
35 */
36
37 $security = parse_ini_file("security.ini");
38 $secret_key = $security["encryption_key"];
39 $secret_iv = $security["iv"];
40 $encrypt_method = $security["encryption_mechanism"];
41
42 // hash
43 $key = hash("sha256", $secret_key);
44
45 // iv – encrypt method AES-256-CBC expects 16 bytes – else you will get a warning
46 $iv = substr(hash("sha256", $secret_iv), 0, 16);
47
48 //do the decryption given text/string/number
49
50 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
51 return $output;
52}