-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.blowfish.php
More file actions
46 lines (35 loc) · 1010 Bytes
/
class.blowfish.php
File metadata and controls
46 lines (35 loc) · 1010 Bytes
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
<?php
class blowfish {
private $key;
private $iv;
private $mode;
public function __construct($mode = 'ecb', $key = 'm1d@t0?c*M@X2Q_@', $iv = 'a9c_b8$4') {
$this->key = $key;
$this->iv = $iv;
$this->mode = $mode;
return;
}
private function pad($data) {
$padlen = 8 - (strlen($data) % 8);
for ($i = 0; $i < $padlen; $i++)
$data .= chr($padlen);
return $data;
}
function encrypt($data) {
$td = mcrypt_module_open('blowfish', '', $this->mode, $this->iv);
mcrypt_generic_init($td, $this->key, $this->iv);
$encrypted_data = mcrypt_generic($td, $this->pad($data));
$encrypted_data = base64_encode($encrypted_data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encrypted_data;
}
function decrypt($data) {
$decrypted = base64_decode($data, true);
if ($decrypted === false) {
return $data;
}
return mcrypt_decrypt(MCRYPT_BLOWFISH, $this->key, $decrypted, constant('MCRYPT_MODE_' . strtoupper($this->mode)), $this->iv);
}
}
?>