Encrypter.php
1.55 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
<?php
include 'config.php';
/**
* 加解密帮助类
*/
class Encrypter
{
/**
* @desc加密
* @param string $str 待加密字符串
* @param string $key 密钥
* @return string
*/
public function encrypt($str){
$config = include('Common/config.php');
$key = $config['key'];
$mixStr = md5(date('Y-m-d H:i:s').rand(1000,9999));
$tmp = '';
$strLen = strlen($str);
for($i=0, $j=0; $i<$strLen; $i++, $j++){
$j = $j == 32 ? 0 : $j;
$tmp .= $mixStr[$j].($str[$i] ^ $mixStr[$j]);
}
return base64_encode($this->bind_key($tmp, $key));
}
/**
* @desc解密
* @param string $str 待解密字符串
* @param string $key 密钥
* @return string
*/
public function decrypt($str){
$config = include('Common/config.php');
$key = $config['key'];
$str = $this->bind_key(base64_decode($str), $key);
$strLen = strlen($str);
$tmp = '';
for($i=0; $i<$strLen; $i++){
$tmp .= $str[$i] ^ $str[++$i];
}
return $tmp;
}
/**
* @desc辅助方法 用密钥对随机化操作后的字符串进行处理
* @param $str
* @param $key
* @return string
*/
private function bind_key($str, $key){
$encrypt_key = md5($key);
$tmp = '';
$strLen = strlen($str);
for($i=0, $j=0; $i<$strLen; $i++, $j++){
$j = $j == 32 ? 0 : $j;
$tmp .= $str[$i] ^ $encrypt_key[$j];
}
return $tmp;
}
}