· 6 years ago · Feb 12, 2019, 08:02 PM
1class AesCipher
2{
3 const CIPHER = 'AES-128-CBC';
4 const INIT_VECTOR_LENGTH = 16;
5 protected $data;
6 protected $initVector;
7 protected $errorMessage;
8 public function __construct()
9 {
10 }
11 public static function encrypt($secretKey, $plainText)
12 {
13 try {
14 if (!static::isKeyLengthValid($secretKey)) {
15 throw new \InvalidArgumentException("Secret key's length must be 128, 192 or 256 bits");
16 }
17 $initVector = bin2hex(openssl_random_pseudo_bytes(static::INIT_VECTOR_LENGTH / 2));
18 $raw = openssl_encrypt(
19 $plainText,
20 static::CIPHER,
21 $secretKey,
22 OPENSSL_RAW_DATA,
23 $initVector
24 );
25 $result = base64_encode($initVector . $raw);
26 if ($result === false) {
27 return openssl_error_string();
28 }
29 return $result;
30 } catch (\Exception $e) {
31 return new static(isset($initVector), null, $e->getMessage());
32 }
33 }
34 public static function decrypt($secretKey, $cipherText)
35 {
36 try {
37 if (!static::isKeyLengthValid($secretKey)) {
38 throw new \InvalidArgumentException("Secret key's length must be 128, 192 or 256 bits");
39 }
40 $encoded = base64_decode($cipherText);
41 $initVector = substr($encoded, 0, static::INIT_VECTOR_LENGTH);
42 $data = substr($encoded, static::INIT_VECTOR_LENGTH);
43 $decoded = openssl_decrypt(
44 $data,
45 static::CIPHER,
46 $secretKey,
47 OPENSSL_RAW_DATA,
48 $initVector
49 );
50 if ($decoded === false) {
51 return openssl_error_string();
52 }
53 return $decoded;
54 } catch (\Exception $e) {
55 return new static(isset($initVector), null, $e->getMessage());
56 }
57 }
58 public static function isKeyLengthValid($secretKey)
59 {
60 $length = strlen($secretKey);
61 return $length == 16 || $length == 24 || $length == 32;
62 }
63 public function getData()
64 {
65 return $this->data;
66 }
67 public function getInitVector()
68 {
69 return $this->initVector;
70 }
71 public function getErrorMessage()
72 {
73 return $this->errorMessage;
74 }
75 public function hasError()
76 {
77 return $this->errorMessage !== null;
78 }
79}
80
81function generateRandomString($length = 10) {
82 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
83 $charactersLength = strlen($characters);
84 $randomString = '';
85 for ($i = 0; $i < $length; $i++) {
86 $randomString .= $characters[rand(0, $charactersLength - 1)];
87 }
88 return $randomString;
89}
90
91function custom_encrypt($string)
92{
93 $encrypted = base64_encode(strrev(base64_encode($string)));
94 $encrypted = AesCipher::encrypt(private_key1,$encrypted);
95 $array1 = array();
96 $max_count = rand(5,10);
97 $data_location = rand(1,$max_count);
98 for($i = 1;$i <= $max_count;$i++)
99 {
100 if($i == $data_location)
101 {
102 $array1[$i] = $encrypted;
103 }else{
104 $array1[$i] = generateRandomString(strlen($encrypted)-1);
105 }
106 }
107 return AesCipher::encrypt(private_key2,json_encode($array1));
108}
109
110function custom_decrypt($string)
111{
112 $decrypted = AesCipher::decrypt(private_key2,$string);
113 $array1 = json_decode($decrypted,true);
114 if(is_array($array1))
115 {
116 $data = "";
117 foreach($array1 as $a_column)
118 {
119 if(strlen($data) < strlen($a_column))
120 {
121 $data = $a_column;
122 }
123 }
124 $decrypted2 = AesCipher::decrypt(private_key1,$data);
125 return base64_decode(strrev(base64_decode($decrypted2)));
126 }
127 return false;
128}