-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCrypt.php
More file actions
277 lines (243 loc) · 6.9 KB
/
Copy pathCrypt.php
File metadata and controls
277 lines (243 loc) · 6.9 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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// {{{ Crypt class
/**
* The Crypt class provides an easy and secure way to encrypt and
* decrypt data using the Symmetric Cryptography method, that is a
* low cost algorithm that encrypt data based on the shared keys
* conecpt.
*
* @package Crypt Class
* @category Cryptography
* @author Arthur Furlan <arthur.furlan@gmail.com>
* @copyright 2006 (c) - Arthur Furlan
* @license GPL v3.0 {@link http://gnu.org/licenses/gpl.txt}
* @link http://code.google.com/p/cryptclass
* @version 2.2
*
* @todo Improve class documentation
*/
class Crypt {
// {{{ Constants
/* Returning modes */
const MODE_BIN = 0; // same length of the original data
const MODE_B64 = 1; // about 66% bigger
const MODE_HEX = 2; // exactly 100% bigger
// }}}
// {{{ Attributes
/**
* The private key used in the cryptography method.
* NOTE: This value should be as strange as possible.
*
* @name $_key
* @access private
* @var string
*/
private $_key = __CLASS__;
/**
* The returning mode of the cryptography method.
*
* @name $_mode
* @access private
* @var integer
* @see Crypt::{MODE_BIN, MODE_B64, MODE_HEX}
*/
private $_mode = Crypt::MODE_HEX;
// }}}
// {{{ function __construct()
/**
* Configure the new object, setting the mode and key.
*
* @name __construct()
* @access public
* @param [$mode] integer
* @param [$key] integer
* @return void
*/
function __construct($mode = null, $key = null) {
is_null($mode) || ($this->Mode = $mode);
is_null($key) || ($this->Key = $key);
}
// }}}
// {{{ function __toString()
/**
* Overload of the object conversion to string.
*
* @name __toString()
* @access public
* @method void
* @return string
*/
function __toString() {
return __CLASS__ . " object\n"
. "(\n"
. " [Key] => {$this->_key}\n"
. " [Mode] => {$this->_mode}\n"
. ")\n";
}
// }}}
// {{{ function __set()
/**
* Properties write methods.
*
* @name __set()
* @param $property string
* @param $value mixed
* @return void
*/
function __set($property, $value) {
switch ($property) {
case 'Key' : return $this->_setKey($value);
case 'Mode': return $this->_setMode($value);
}
}
// }}}
// {{{ function __get()
/**
* Properties read methods.
*
* @name __get()
* @param $key string
* @return void
*/
function __get($property) {
switch ($property) {
case 'Key' : return $this->_key;
case 'Mode': return $this->_mode;
}
}
// }}}
// {{{ public function encrypt()
/**
* Encrypt the data using the current returning mode.
*
* @name encrypt()
* @access public
* @param $data mixed
* @return string
*/
public function encrypt($data) {
$data = (string) $data;
for ($i=0;$i<strlen($data);$i++)
@$encrypt .= $data[$i] ^
$this->_key[$i % strlen($this->_key)];
if ($this->_mode === Crypt::MODE_B64)
return base64_encode(@$encrypt);
if ($this->_mode === Crypt::MODE_HEX)
return $this->_encodeHexadecimal(@$encrypt);
return @$encrypt;
}
// }}}
// {{{ public function decrypt()
/**
* Decrypt the data using the current returning mode.
* NOTE: You must use the same $_mode of the creation process.
*
* @name decrypt()
* @access public
* @param $crypt string
* @return string
*/
public function decrypt($crypt) {
if ($this->_mode === Crypt::MODE_HEX)
$crypt = $this->_decodeHexadecimal($crypt);
if ($this->_mode === Crypt::MODE_B64)
$crypt = (string)base64_decode($crypt);
for ($i=0;$i<strlen($crypt);$i++)
@$data .= $crypt[$i] ^ $this->_key[$i % strlen($this->_key)];
return @$data;
}
// }}}
// {{{ public static function supportedModes()
/**
* Return the list containing all supported modes.
*
* @name supportedModes()
* @access public
* @param void
* @return void
*/
public static function supportedModes() {
return array(
Crypt::MODE_BIN,
Crypt::MODE_B64,
Crypt::MODE_HEX
);
}
// }}}
// {{{ protected static function _isSupportedMode()
/**
* Checks if $mode is a valid returning mode of the class.
*
* @name _isSupportedMode()
* @access public
* @param $mode integer
* @return void
*/
public static function _isSupportedMode($mode) {
return in_array($mode, Crypt::supportedModes());
}
// }}}
// {{{ protected function _setKey()
/**
* Set the key used in the cryptography method.
*
* @name _setMode()
* @access protected
* @param $key string
* @return void
*/
protected function _setKey($key) {
$this->_key = (string) $key;
}
// }}}
// {{{ protected function _setMode()
/**
* Set the current returning mode of the class.
*
* @name _setMode()
* @access protected
* @param $mode integer
* @return void
*/
protected function _setMode($mode) {
Crypt::_isSupportedMode($mode)
&& ($this->_mode = (int)$mode);
}
// }}}
// {{{ protected function _encodeHexadecimal()
/**
* Encode the data using hexadecimal chars.
*
* @name _encodeHexadecimal()
* @access protected
* @param $data mixed
* @return string
*/
protected function _encodeHexadecimal($data) {
$data = (string) $data;
for ($i=0;$i<strlen($data);$i++)
@$hexcrypt .= str_pad(dechex(ord(
$data[$i])), 2, 0, STR_PAD_LEFT);
return @$hexcrypt;
}
// }}}
// {{{ protected function _decodeHexadecimal()
/**
* Decode hexadecimal strings.
*
* @name _decodeHexadecimal()
* @access protected
* @param $data string
* @return string
*/
protected function _decodeHexadecimal($hexcrypt) {
$hexcrypt = (string) $hexcrypt;
for ($i=0;$i<strlen($hexcrypt);$i+=2)
@$data .= chr(hexdec(substr($hexcrypt, $i, 2)));
return @$data;
}
// }}}
}
// }}}
?>