• php常用函数——字符串加密以及解密函数【转】


     1 /**
     2      * 字符串加密以及解密函数
     3      *
     4      * @param string $string 原文或者密文
     5      * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
     6      * @param string $key 密钥
     7      * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
     8      * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
     9      *
    10      * @example
    11      *
    12      *     $a = authcode('abc', 'ENCODE', 'key');
    13      *     $b = authcode($a, 'DECODE', 'key');  // $b(abc)
    14      *
    15      *     $a = authcode('abc', 'ENCODE', 'key', 3600);
    16      *     $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
    17      */
    18     function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
    19 
    20         $ckey_length = 4;    // 随机密钥长度 取值 0-32;
    21                     // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
    22                     // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
    23                     // 当此值为 0 时,则不产生随机密钥
    24 
    25         $key = md5($key ? $key : UC_KEY);
    26         $keya = md5(substr($key, 0, 16));
    27         $keyb = md5(substr($key, 16, 16));
    28         $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
    29 
    30         $cryptkey = $keya.md5($keya.$keyc);
    31         $key_length = strlen($cryptkey);
    32 
    33         $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    34         $string_length = strlen($string);
    35 
    36         $result = '';
    37         $box = range(0, 255);
    38 
    39         $rndkey = array();
    40         for($i = 0$i <= 255$i++) {
    41             $rndkey[$i= ord($cryptkey[$i % $key_length]);
    42         }
    43 
    44         for($j = $i = 0$i < 256$i++) {
    45             $j = ($j + $box[$i+ $rndkey[$i]) % 256;
    46             $tmp = $box[$i];
    47             $box[$i= $box[$j];
    48             $box[$j= $tmp;
    49         }
    50 
    51         for($a = $j = $i = 0$i < $string_length$i++) {
    52             $a = ($a + 1% 256;
    53             $j = ($j + $box[$a]) % 256;
    54             $tmp = $box[$a];
    55             $box[$a= $box[$j];
    56             $box[$j= $tmp;
    57             $result .= chr(ord($string[$i]) ^ ($box[($box[$a+ $box[$j]) % 256]));
    58         }
    59 
    60         if($operation == 'DECODE') {
    61             if((substr($result, 0, 10== 0 || substr($result, 0, 10- time() > 0&& substr($result, 10, 16== substr(md5(substr($result, 26).$keyb), 0, 16)) {
    62                 return substr($result, 26);
    63             } else {
    64                 return '';
    65             }
    66         } else {
    67             return $keyc.str_replace('=', '', base64_encode($result));
    68         }
    69 
    70     }
  • 相关阅读:
    百度地图代码API
    3层下拉列表
    stl+数论——1247D
    数论+乱搞——cf181B
    思维+multiset优化——cf1249E
    线性基思想+贪心——cf1249C
    tarjan求强连通+缩点——cf1248E
    排序+模拟+优先队列——cf1248E
    栈+括号序列+暴力枚举——cf1248D1
    二分+贪心——cf1251D
  • 原文地址:https://www.cnblogs.com/zack/p/1497410.html
Copyright © 2020-2023  润新知