forked from Th3-822/rapidleech
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.pcrypt.php
More file actions
326 lines (285 loc) · 9.92 KB
/
class.pcrypt.php
File metadata and controls
326 lines (285 loc) · 9.92 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
// +----------------------------------------------------------------------+
// | Perfect Scripts class.pcrypt.php |
// | Brazilian Organization |
// +----------------------------------------------------------------------+
// | Viva ao Linux! |
// | Porque nós amamos a liberdade! |
// +----------------------------------------------------------------------+
// | Class Perfect Crypt |
// | Created By Igor Ribeiro de Assis |
// | <igor21@terra.com.br> UIN: 71064682 |
// +----------------------------------------------------------------------+
// | http://ps.wmforce.com |
// +----------------------------------------------------------------------+
// | Under GPL |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published |
// | by the Free Software Foundation; either version 2 of the License, |
// | or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
// | 02111-1307 USA |
// +----------------------------------------------------------------------+
/**
* Encryption Block Mode constants
*
* @const MODE_ECB ecb mode
* @const MODE_CBC cbc mode
*/
define("MODE_ECB",0);
define("MODE_CBC",1);
/**
* An abstract layer class to encrypt data.
*
* This class is class is used to encrypt and decrypt data using different algorithms
* and modes.
*
* @author Igor Ribeiro de Assis <igor21@terra.com.br>
* @version 1.0-beta
* @access public
* @package Perfect Crypt
*/
class pcrypt
{
/** Encryption Block Mode: ECB, CBC actually.
*
* @var int the mode used
* @access private
*/
var $blockmode = MODE_ECB;
/** Key for Encryption
*
* @var string the key used in encryption and decryption
* @access public
*/
var $key = null;
/** IV - Initialization Vector String
*
* @var string initialization vector for some modes (CBC)
* @access public
*/
var $iv = "z4c8e7gh";
/** Methods */
/** Constructor of the class.
*
* The constructor initialize some important vars and include the algorithm
* file.
*
* @access public
* @param int $blockmode the blockmode to use
* @param string $cipher the algorithm used to crypt
* @param string $key the ley used to crypt
*
* @return void
*/
function pcrypt($blockmode = MODE_ECB, $cipher = 'BLOWFISH', $key = null)
{
// Include cipher_class file
$cipher = strtolower($cipher);
if (!file_exists($cipher.".php"))
{
//$this->error();
}
include_once $cipher.".php";
// Load cipher_class
if (!class_exists("pcrypt_".$cipher))
{
$this->error("Class pcrypt_".$cipher." doesn't exists");
}
$class = "pcrypt_".$cipher;
$this->cipher = new $class($key);
// Initialize Vars
$this->blockmode = $blockmode;
$this->key = $key;
}
/** Crypt data using the selected algorithm
*
* This method encrypt data using the selected algorithm and mode:
* Algorithms: Blowfish
* Modes: ECB, CBC
* For a description about algorithms and modes see:
* Applied Cryptography by Bruce Schneier
*
* @access public
* @param string $plain the plain text to be encrypted
* @return string $cipher the plain text encrypted
*/
function encrypt($plain)
{
if (empty($plain))
{
$this->error("Empty Plain Text");
}
// Encrypt using the correct mode
switch($this->blockmode) {
case MODE_ECB:
$cipher = $this->_ecb_encrypt($plain);
break;
case MODE_CBC:
$cipher = $this->_cbc_encrypt($plain);
break;
default:
$this->error("Invalid mode ".$this->blockmode);
}
return $cipher;
}
/** Decrypt using the selected algorithm
*
* This method decrypt data using the selected algorithm and mode.
* TODO: Discover the algorithm and mode auto
*
* @access public
* @param string $cipher the crypted data to be decrypted
* @return string $plain the cipher text decrypted
*/
function decrypt($cipher)
{
if (empty($cipher))
{
$this->error("Invalid Cipher Text");
}
// Decrypt with the correct mode
switch($this->blockmode) {
case MODE_ECB:
$plain = $this->_ecb_decrypt($cipher);
break;
case MODE_CBC:
$plain = $this->_cbc_decrypt($cipher);
break;
default:
$this->error("Invalid mode ".$this->blockmode);
}
return $plain;
}
/** Method to encrypt using ECB mode.
*
* In ECB mode the blocks are encrypted independently
*
* @access private
* @param string $plain the plain text to be encrypted
* @return string $cipher the plain text encrypted
*/
function _ecb_encrypt($plain)
{
$blocksize = $this->cipher->blocksize;
$plainsize = strlen($plain);
$cipher = '';
for($i = 0;$i < $plainsize;$i = $i + $blocksize)
{
$block = substr($plain,$i,$blocksize);
if(strlen($block) < $blocksize)
{
// pad block with '\0'
$block = str_pad($block,$blocksize,"\0",STR_PAD_LEFT);
}
$cipher .= $this->cipher->_encrypt($block);
}
return $cipher;
}
/** Method to decrypt using ECB mode.
*
* @access private
* @param string $cipher the cipher text
* @return string $plain the cipher text decrypted
*/
function _ecb_decrypt($cipher)
{
$blocksize = $this->cipher->blocksize;
$ciphersize = strlen($cipher);
$plain = '';
for($i = 0;$i < $ciphersize;$i = $i + $blocksize)
{
$block = substr($cipher,$i,$blocksize);
$block = $this->cipher->_decrypt($block);
// Remove padded chars
while(substr($block,0,1) == "\0")
{
$block = substr($block,1);
}
$plain .= $block;
}
return $plain;
}
/** This method encrypt using CBC mode.
*
* In CBC mode each block is xored with the last. This function use $iv as
* first block.
*
* @access private
* @param string $plain the plain text to be decrypted
* @return string $cipher the plain text encrypted
*/
function _cbc_encrypt($plain)
{
$blocksize = $this->cipher->blocksize;
$plainsize = strlen($plain);
$cipher = '';
$lcipher = $this->iv;
// encrypt each block
for($i = 0;$i < $plainsize;$i = $i + $blocksize)
{
$block = substr($plain,$i,$blocksize);
if(strlen($block) < $blocksize)
{
// pad block with '\0'
$block = str_pad($block,$blocksize,"\0",STR_PAD_LEFT);
}
// crypt the block xored with the last cipher block
$lcipher = $this->cipher->_encrypt($block ^ $lcipher);
$cipher .= $lcipher;
}
return $cipher;
}
/** This method decrypt using CBC.
*
* @access private
* @param string $cipher the cipher text
* @return string $plain the cipher text decrypted
*/
function _cbc_decrypt($cipher)
{
// get the block size of the cipher
$blocksize = $this->cipher->blocksize;
$ciphersize = strlen($cipher);
$plain = '';
$lcipher = $this->iv;
for($i = 0;$i < $ciphersize;$i = $i + $blocksize)
{
$block = substr($cipher,$i,$blocksize);
// xor the block with the last cipher block
$dblock = $lcipher ^ $this->cipher->_decrypt($block);
$lcipher = $block;
// Remove padded chars
while(substr($dblock,0,1) == "\0")
{
$dblock = substr($dblock,1);
}
$plain .= $dblock;
}
return $plain;
}
/**
* A simple function for error handling.
*
* TODO: Improve the error handling of the class
*
* @access private
* @param string $message erro message
* @return boolean true
*/
function error($message)
{
//echo "Error: ".$message."<br>";
return 1;
}
}
?>