| 1 |
<?php |
|---|
| 2 |
if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); } |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
class Crypt { |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
* Gets a random value for encryption |
|---|
| 11 |
* - From php.net docs |
|---|
| 12 |
* |
|---|
| 13 |
* @param $iv_len |
|---|
| 14 |
* length of random variable |
|---|
| 15 |
*/ |
|---|
| 16 |
function getRndIV($iv_len) { |
|---|
| 17 |
|
|---|
| 18 |
$iv = ''; |
|---|
| 19 |
while ($iv_len-- > 0) { |
|---|
| 20 |
$iv .= chr(mt_rand() & 0xff); |
|---|
| 21 |
} |
|---|
| 22 |
return $iv; |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
* Encrypts string |
|---|
| 27 |
* - From php.net docs |
|---|
| 28 |
* |
|---|
| 29 |
* @param $str |
|---|
| 30 |
* string to encrypt |
|---|
| 31 |
* @param $salt |
|---|
| 32 |
* password to use for encryption |
|---|
| 33 |
* @param $iv_len |
|---|
| 34 |
* length of random number |
|---|
| 35 |
*/ |
|---|
| 36 |
function encrypt($str, $salt, $iv_len = 16) { |
|---|
| 37 |
|
|---|
| 38 |
$str .= "\x13"; |
|---|
| 39 |
$n = strlen($str); |
|---|
| 40 |
if ($n % 16) $str .= str_repeat("\0", 16 - ($n % 16)); |
|---|
| 41 |
$i = 0; |
|---|
| 42 |
$enc_text = $this->getRndIV($iv_len); |
|---|
| 43 |
$iv = substr($salt ^ $enc_text, 0, 512); |
|---|
| 44 |
while ($i < $n) { |
|---|
| 45 |
$block = substr($str, $i, 16) ^ pack('H*', md5($iv)); |
|---|
| 46 |
$enc_text .= $block; |
|---|
| 47 |
$iv = substr($block . $iv, 0, 512) ^ $salt; |
|---|
| 48 |
$i += 16; |
|---|
| 49 |
} |
|---|
| 50 |
return base64_encode($enc_text); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* Decrypts string |
|---|
| 55 |
* - From php.net docs |
|---|
| 56 |
* |
|---|
| 57 |
* @param $enc |
|---|
| 58 |
* encrypted string to decrypt |
|---|
| 59 |
* @param $salt |
|---|
| 60 |
* password to use for encryption |
|---|
| 61 |
* @param $iv_len |
|---|
| 62 |
* length of random number |
|---|
| 63 |
*/ |
|---|
| 64 |
function decrypt($enc, $salt, $iv_len = 16) { |
|---|
| 65 |
|
|---|
| 66 |
$enc = base64_decode($enc); |
|---|
| 67 |
$n = strlen($enc); |
|---|
| 68 |
$i = $iv_len; |
|---|
| 69 |
$str = ''; |
|---|
| 70 |
$iv = substr($salt ^ substr($enc, 0, $iv_len), 0, 512); |
|---|
| 71 |
while ($i < $n) { |
|---|
| 72 |
$block = substr($enc, $i, 16); |
|---|
| 73 |
$str .= $block ^ pack('H*', md5($iv)); |
|---|
| 74 |
$iv = substr($block . $iv, 0, 512) ^ $salt; |
|---|
| 75 |
$i += 16; |
|---|
| 76 |
} |
|---|
| 77 |
return preg_replace('/\\x13\\x00*$/', '', $str); |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
?> |
|---|
| 83 |
|
|---|