250 |
kaklik |
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* $Id: mcrypt.lib.php,v 2.3 2005/02/07 16:46:02 rabus Exp $ */ |
|
|
4 |
// vim: expandtab sw=4 ts=4 sts=4: |
|
|
5 |
|
|
|
6 |
/** |
|
|
7 |
* Initialization |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
// Store the initialization vector because it will be needed for |
|
|
11 |
// further decryption. I don't think necessary to have one iv |
|
|
12 |
// per server so I don't put the server number in the cookie name. |
|
|
13 |
|
|
|
14 |
if (!isset($_COOKIE['pma_mcrypt_iv'])) { |
|
|
15 |
srand((double) microtime() * 1000000); |
|
|
16 |
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC), MCRYPT_RAND); |
|
|
17 |
setcookie('pma_mcrypt_iv', |
|
|
18 |
base64_encode($iv), |
|
|
19 |
time() + (60 * 60 * 24 * 30), |
|
|
20 |
$GLOBALS['cookie_path'], '', |
|
|
21 |
$GLOBALS['is_https']); |
|
|
22 |
} else { |
|
|
23 |
$iv = base64_decode($_COOKIE['pma_mcrypt_iv']); |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* String padding |
|
|
28 |
* |
|
|
29 |
* @param string input string |
|
|
30 |
* @param integer length of the result |
|
|
31 |
* @param string the filling string |
|
|
32 |
* @param integer padding mode |
|
|
33 |
* |
|
|
34 |
* @return string the padded string |
|
|
35 |
* |
|
|
36 |
* @access public |
|
|
37 |
*/ |
|
|
38 |
function full_str_pad($input, $pad_length, $pad_string = '', $pad_type = 0) { |
|
|
39 |
$str = ''; |
|
|
40 |
$length = $pad_length - strlen($input); |
|
|
41 |
if ($length > 0) { // str_repeat doesn't like negatives |
|
|
42 |
if ($pad_type == STR_PAD_RIGHT) { // STR_PAD_RIGHT == 1 |
|
|
43 |
$str = $input.str_repeat($pad_string, $length); |
|
|
44 |
} elseif ($pad_type == STR_PAD_BOTH) { // STR_PAD_BOTH == 2 |
|
|
45 |
$str = str_repeat($pad_string, floor($length/2)); |
|
|
46 |
$str .= $input; |
|
|
47 |
$str .= str_repeat($pad_string, ceil($length/2)); |
|
|
48 |
} else { // defaults to STR_PAD_LEFT == 0 |
|
|
49 |
$str = str_repeat($pad_string, $length).$input; |
|
|
50 |
} |
|
|
51 |
} else { // if $length is negative or zero we don't need to do anything |
|
|
52 |
$str = $input; |
|
|
53 |
} |
|
|
54 |
return $str; |
|
|
55 |
} |
|
|
56 |
/** |
|
|
57 |
* Encryption using blowfish algorithm (mcrypt) |
|
|
58 |
* |
|
|
59 |
* @param string original data |
|
|
60 |
* @param string the secret |
|
|
61 |
* |
|
|
62 |
* @return string the encrypted result |
|
|
63 |
* |
|
|
64 |
* @access public |
|
|
65 |
* |
|
|
66 |
* @author lem9 |
|
|
67 |
*/ |
|
|
68 |
function PMA_blowfish_encrypt($data, $secret) { |
|
|
69 |
global $iv; |
|
|
70 |
// Seems we don't need the padding. Anyway if we need it, |
|
|
71 |
// we would have to replace 8 by the next 8-byte boundary. |
|
|
72 |
//$data = full_str_pad($data, 8, "\0", STR_PAD_RIGHT); |
|
|
73 |
return base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $secret, $data, MCRYPT_MODE_CBC, $iv)); |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
/** |
|
|
77 |
* Decryption using blowfish algorithm (mcrypt) |
|
|
78 |
* |
|
|
79 |
* @param string encrypted data |
|
|
80 |
* @param string the secret |
|
|
81 |
* |
|
|
82 |
* @return string original data |
|
|
83 |
* |
|
|
84 |
* @access public |
|
|
85 |
* |
|
|
86 |
* @author lem9 |
|
|
87 |
*/ |
|
|
88 |
function PMA_blowfish_decrypt($encdata, $secret) { |
|
|
89 |
global $iv; |
|
|
90 |
return trim(mcrypt_decrypt(MCRYPT_BLOWFISH, $secret, base64_decode($encdata), MCRYPT_MODE_CBC, $iv)); |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
?> |