• PHP加密解密函数(带有效期,过了有效期也解不了)


    转的,原来应该是discuz中弄的

     1 <?php
     2 //加解密函数
     3 //此函数的厉害之处在于可以在指定时间内加密还原字符串,超时无法还原。
     4 //这样我们就可以拿此函数来做很多用途了,比如:单点登录的token加密传输啦,临时密码啦等等。
     5 
     6 /**
     7 * @param string $string 原文或者密文
     8 * @param string $operation 操作(ENCODE加密 | DECODE解密), 默认为 DECODE
     9 * @param string $key 密钥
    10 * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
    11 * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
    12 *
    13 * @example
    14 *
    15 *
    16 * echo $encode = authcode('abc','ENCODE','key',3600);
    17 * echo '<br />';
    18 * 解密
    19 * echo $decode = authcode($encode,'DECODE','key'); // 在一个小时内有效(abc),否则 $b 为空
    20 */
    21 
    22 
    23 ?>
    24 
    25 
    26 <?php
    27 function authcode($string, $operation = 'DECODE', $key = '', $expiry = 3600)
    28 {
    29   $ckey_length = 4;   
    30     // 随机密钥长度 取值 0-32;
    31     // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
    32     // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
    33     // 当此值为 0 时,则不产生随机密钥
    34     $key = md5($key);
    35     $keya = md5(substr($key, 0, 16));
    36     $keyb = md5(substr($key, 16, 16));
    37     $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
    38 
    39     $cryptkey = $keya.md5($keya.$keyc);
    40     $key_length = strlen($cryptkey);
    41 
    42     $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    43     $string_length = strlen($string);
    44 
    45     $result = '';
    46     $box = range(0, 255);
    47 
    48     $rndkey = array();
    49     for($i = 0; $i <= 255; $i++) 
    50     {
    51         $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    52     }
    53 
    54     for($j = $i = 0; $i < 256; $i++) 
    55     {
    56         $j = ($j + $box[$i] + $rndkey[$i]) % 256;
    57         $tmp = $box[$i];
    58         $box[$i] = $box[$j];
    59         $box[$j] = $tmp;
    60     }
    61 
    62     for($a = $j = $i = 0; $i < $string_length; $i++) 
    63     {
    64         $a = ($a + 1) % 256;
    65         $j = ($j + $box[$a]) % 256;
    66         $tmp = $box[$a];
    67         $box[$a] = $box[$j];
    68         $box[$j] = $tmp;
    69         $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    70     }
    71 
    72     if($operation == 'DECODE') 
    73     {
    74         if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) 
    75         {
    76             return substr($result, 26);
    77         } 
    78         else 
    79         {
    80             return '';
    81         }
    82     } 
    83     else 
    84     {
    85         return $keyc.str_replace('=', '', base64_encode($result));
    86     }
    87 }
    88 ?>
  • 相关阅读:
    poj 1113 wall(凸包裸题)(记住求线段距离的时候是点积,点积是cos)
    Atcoder(134)E
    poj 1696 极角排序(解题报告)
    poj 1410 (没做出来,记得闲着没事看看这道题)
    poj1066 线段相交简单应用(解题报告)
    poj 2653 线段相交裸题(解题报告)
    poj 1269
    要习惯用vector代替数组
    在 Angularjs 中$state.go 如何传递参数
    CSS实现内容超过长度后以省略号显示
  • 原文地址:https://www.cnblogs.com/html55/p/10298531.html
Copyright © 2020-2023  润新知