onethink的对称加解密类

本文阅读 1 分钟
首页 PHP笔记 正文
<?php
/**
 * @descript: 对称加解密类
 * @source: onthink
 */

class Encrypt{
    /**
     * 系统加密方法
     * @param string $data 要加密的字符串
     * @param string $key  加密密钥
     * @param int $expire  过期时间 单位 秒
     * @return string
     */
    public static function think_encrypt($data,&nbsp;$key = '', $expire = 0) {
        // $key&nbsp;&nbsp;=&nbsp;md5(empty($key) ? C('DATA_AUTH_KEY') : $key);
        $key&nbsp;&nbsp;=&nbsp;md5($key) ;
        $data&nbsp;=&nbsp;base64_encode($data);
        $x    = 0;
        $len&nbsp;&nbsp;=&nbsp;strlen($data);
        $l&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;strlen($key);
        $char = '';

        for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
            if ($x&nbsp;==&nbsp;$l) $x = 0;
            $char&nbsp;.=&nbsp;substr($key, $x, 1);
            $x++;
        }

        $str&nbsp;=&nbsp;sprintf(&#39;%010d&#39;,&nbsp;$expire ? $expire + time():0);

        for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
            $str&nbsp;.=&nbsp;chr(ord(substr($data, $i,&nbsp;1))&nbsp;+&nbsp;(ord(substr($char, $i, 1)))%256);
        }
        return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
    }

    /**
     * 系统解密方法
     * @param  string $data 要解密的字符串 (必须是think_encrypt方法加密的字符串)
     * @param  string $key  加密密钥
     * @return string
     */
    public static function think_decrypt($data,&nbsp;$key = ''){
        $key&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;md5($key);
        $data&nbsp;&nbsp;&nbsp;=&nbsp;str_replace(array(&#39;-&#39;,&#39;_&#39;),array(&#39;+&#39;,&#39;/&#39;),$data);
        $mod4&nbsp;&nbsp;&nbsp;=&nbsp;strlen($data) % 4;
        if ($mod4) {
        $data&nbsp;.=&nbsp;substr(&#39;====&#39;,&nbsp;$mod4);
        }
        $data&nbsp;&nbsp;&nbsp;=&nbsp;base64_decode($data);
        $expire&nbsp;=&nbsp;substr($data,0,10);
        $data&nbsp;&nbsp;&nbsp;=&nbsp;substr($data,10);

        if($expire&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;$expire < time()) {
            return '';
        }
        $x      = 0;
        $len&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;strlen($data);
        $l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;strlen($key);
        $char&nbsp;&nbsp;&nbsp;=&nbsp;$str = '';

        for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
            if ($x&nbsp;==&nbsp;$l) $x = 0;
            $char&nbsp;.=&nbsp;substr($key, $x, 1);
            $x++;
        }

        for ($i&nbsp;=&nbsp;0;&nbsp;$i < $len;&nbsp;$i++) {
            if (ord(substr($data,&nbsp;$i, 1))<ord(substr($char,&nbsp;$i, 1))) {
                $str&nbsp;.=&nbsp;chr((ord(substr($data, $i,&nbsp;1))&nbsp;+&nbsp;256)&nbsp;-&nbsp;ord(substr($char, $i, 1)));
            }else{
                $str&nbsp;.=&nbsp;chr(ord(substr($data, $i,&nbsp;1))&nbsp;-&nbsp;ord(substr($char, $i, 1)));
            }
        }
        return base64_decode($str);
    }
}
?>
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

html5有哪些新特性、移除了那些元素?
« 上一篇 05-10
基于jQuery的颜色选择器-jQuery MiniColors
下一篇 » 05-15

发表评论