· 7 years ago · Mar 12, 2018, 02:32 PM
1<?php
2/**
3 *
4 * Name AES Encryption
5 *
6 *
7 * @param $appKey random_bytes( 16 )
8 * @param $secretKey random_bytes( 32 )
9 *
10 * @author RapidMod.com
11 * @author 813.330.0522
12 *
13 */
14
15/**
16 *
17 * Name encryptData
18 * @param $data
19 *
20 * @param $appKey
21 * @param $secretKey
22 *
23 * @return string
24 *
25 * @author RapidMod.com
26 * @author 813.330.0522
27 *
28 */
29function encryptData($data,$appKey,$secretKey) {
30
31
32 $method = "aes-256-cbc";
33 $iv_length = openssl_cipher_iv_length($method);
34 $iv = openssl_random_pseudo_bytes($iv_length);
35
36 $first_encrypted = openssl_encrypt($data,$method,$appKey, OPENSSL_RAW_DATA ,$iv);
37 $second_encrypted = hash_hmac('sha3-512', $first_encrypted, $secretKey, TRUE);
38
39 $output = base64_encode($iv.$second_encrypted.$first_encrypted);
40 return $output;
41}
42
43/**
44 *
45 * Name decryptData
46 * @param $data
47 *
48 * @param $appKey
49 * @param $secretKey
50 *
51 * @return bool|string
52 *
53 * @author RapidMod.com
54 * @author 813.330.0522
55 *
56 */
57function decryptData($data,$appKey,$secretKey) {
58
59 $method = "aes-256-cbc";
60 $iv_length = openssl_cipher_iv_length($method);
61 $data = base64_decode($data);
62 $iv = substr($data,0,$iv_length);
63
64 $second_encrypted = substr($data,$iv_length,64);
65 $first_encrypted = substr($data,$iv_length+64);
66
67 $data = openssl_decrypt($first_encrypted,$method,$appKey,OPENSSL_RAW_DATA,$iv);
68 $second_encrypted_new = hash_hmac('sha3-512', $first_encrypted, $secretKey, TRUE);
69 if (hash_equals($second_encrypted,$second_encrypted_new)){return $data;}
70 return false;
71}
72?>