Authcode

@Ta 2015-11-22 592点击
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0){
02
  
03
 if($operation == 'DECODE') {
04
  $string = str_replace('[a]','+',$string);
05
  $string = str_replace('[b]','&',$string);
06
  $string = str_replace('[c]','/',$string);
07
 }
08
    $ckey_length = 4;
09
    $key = md5($key ? $key : 'livcmsencryption ');
10
    $keya = md5(substr($key, 0, 16));
11
    $keyb = md5(substr($key, 16, 16));
12
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
13
    $cryptkey = $keya.md5($keya.$keyc);
14
    $key_length = strlen($cryptkey);
15
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
16
    $string_length = strlen($string);
17
    $result = '';
18
    $box = range(0, 255);
19
    $rndkey = array();
20
    for($i = 0; $i <= 255; $i++) {
21
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
22
    }
23
    for($j = $i = 0; $i < 256; $i++) {
24
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
25
        $tmp = $box[$i];
26
        $box[$i] = $box[$j];
27
        $box[$j] = $tmp;
28
    }
29
    for($a = $j = $i = 0; $i < $string_length; $i++) {
30
        $a = ($a + 1) % 256;
31
        $j = ($j + $box[$a]) % 256;
32
        $tmp = $box[$a];
33
        $box[$a] = $box[$j];
34
        $box[$j] = $tmp;
35
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
36
    }
37
    if($operation == 'DECODE') {
38
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
39
    
40
            return substr($result, 26);
41
        } else {
42
            return '';
43
        }
44
    } else {
45
  $ustr = $keyc.str_replace('=', '', base64_encode($result));
46
  $ustr = str_replace('+','[a]',$ustr);
47
  $ustr = str_replace('&','[b]',$ustr);
48
  $ustr = str_replace('/','[c]',$ustr);
49
        return $ustr;
50
    }
51
}
dz
回复列表(0|隐藏机器人聊天)
帖子没有回复
添加新回复
回复需要登录