SecurityKey.class.php
1.51 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
<?php
class Util_Utils_SecurityKey
{
/**
* 加密
*
* @param string $value
* @param string $privacyKey
* @return string |false
*/
public static function encrypt($value, $privacyKey = '')
{
if(empty($privacyKey))
{
return false ;
}
srand((double)microtime()*1000000);
$encryptKey = md5(rand(0,32000));
$ctr = 0;
$tmp = "";
for ($i=0;$i<strlen($value);$i++)
{
if ($ctr==strlen($encryptKey)) $ctr=0;
$tmp.= substr($encryptKey,$ctr,1) . (substr($value,$i,1) ^ substr($encryptKey,$ctr,1));
$ctr++;
}
return SecurityKey::keyED($tmp, $privacyKey);
}
/**
* 解密
*
* @param string $value
* @param string $privacyKey
* @return string|false
*/
public static function decrypt($value, $privacyKey = '')
{
if(empty($privacyKey))
{
return false;
}
$value = SecurityKey::keyED($value, $privacyKey);
$tmp = '';
for ($i=0;$i<strlen($value);$i++)
{
$md5 = substr($value,$i,1);
$i++;
$tmp.= (substr($value,$i,1) ^ $md5);
}
return $tmp;
}
/**
* 密钥加解密
*
* @param string $value
* @param string $encryptKey
* @return string
*/
public static function keyED($value, $encryptKey)
{
$encryptKey = md5($encryptKey);
$ctr = 0;
$tmp = '';
for ($i=0;$i<strlen($value);$i++)
{
if ($ctr == strlen($encryptKey))
{
$ctr = 0;
}
$tmp.= substr($value,$i,1) ^ substr($encryptKey,$ctr,1);
$ctr++;
}
return $tmp;
}
}