-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypt.php
171 lines (141 loc) · 3.72 KB
/
crypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* Quick 'n Dirty Laravel 5.1 decrypter.
*
* This is a slightly modified Laravel Crypt encryptor/decryptor script,
* original by:
* https://gist.github.com/leonjza/ce27aa7435f8d131d93f
* Modifications done by Nikolay Dandanov
*
* Based directly off the source code at:
* https://github.com/laravel/framework/blob/5.1/src/Illuminate/Encryption/Encrypter.php
*
* Have access to an application key from a .env?
* Have some encrypted data you want to decrypt?
* Well: (new Crypt($key))->decrypt($payload); should have you sorted
*
* Have access to an application key from a .env?
* Want to replace an encrypted value?
* Well: (new Crypt($key))->encrypt($payload); should have you sorted
*/
//$payload = 'eyJpdiI6Im55MXhqZUkwdW5PYTVycmFFbUVLTnc9PSIsInZhbHVlIjoiQXptOGxcLzVvZVFXUlwvaCtuXC9ad3BqZz09IiwibWFjIjoiMjE2MGU3NjliODVmYWNmNzc1ZjNjY2FjODBkMmIxOWFkZWUxMDE0OWIzNzVjZGIwZDM1NzdiMmY5MmY2NjllNSJ9';
//$key = 'AvDs2rPepOLpWgypXPs04JCFWs4wbDTe';
$payload = $argv[1];
$key = $argv[2];
$newkey = $argv[3];
/*
echo $payload;
echo "
";
echo $key;
echo "
";
echo $newkey;
echo "
";
*/
$decrypted_data = (new Crypt($key))->decrypt($payload);
$reencrypted_data = (new Crypt($newkey))->encrypt($decrypted_data);
// Output
$arr = array('decrypted_data' => $decrypted_data, 'reencrypted_data' => $reencrypted_data);
echo json_encode($arr);
/**
* Class Crypt
*/
class Crypt
{
/**
* The key used to decrypt.
*
* @var string
*/
public $key;
/**
* The original encrypted payload
*
* @var string
*/
public $payload;
/**
* The cipher types to try for decryption.
*
* @var array
*/
protected $ciphers = [
'AES-128-CBC',
'AES-256-CBC'
];
/**
* Construct a new Crypt
*
* @param null $key
*/
public function __construct($key = null)
{
if (!is_null($key))
$this->key = $key;
}
/**
* Attempt to decrypt a payload.
*
* @param $payload
*
* @return mixed|string
* @throws \Exception
*/
public function decrypt($payload)
{
$payload = json_decode(base64_decode($payload), true);
if ($this->invalidPayload($payload))
throw new Exception('Invalid Payload Format. Want {iv, value, mac} json.');
$iv = base64_decode($payload['iv']);
foreach ($this->ciphers as $cipher) {
$decrypted = openssl_decrypt($payload['value'],
$cipher, $this->key, 0, $iv);
if ($decrypted)
return unserialize($decrypted);
}
return 'Failed to decrypt the payload.';
}
/**
* Encrypt a payload.
*
* @param $payload
* @param string $cipher
* @param int $length
*
* @return string
*/
public function encrypt($payload, $cipher = 'AES-256-CBC', $length = 16)
{
$iv = $bytes = openssl_random_pseudo_bytes($length, $strong);
$value = openssl_encrypt(serialize($payload), $cipher, $this->key, 0, $iv);
$mac = $this->hmac($iv = base64_encode($iv), $value, $this->key);
return base64_encode(json_encode(compact('iv', 'value', 'mac')));
}
/**
* Generate a sha256 keyed hash value.
*
* @param $iv
* @param $value
* @param $key
*
* @return string
*/
public function hmac($iv, $value, $key)
{
return hash_hmac('sha256', $iv . $value, $key);
}
/**
* Check if a payload is in the correct format.
*
* @param $data
*
* @return bool
*/
public function invalidPayload($data)
{
return !is_array($data) || !isset($data['iv']) ||
!isset($data['value']) || !isset($data['mac']);
}
}