PHP加密解密类(密文只有大小写字母和数字),支持中文加密

星期三 下午 13:08 狂奔的蜗牛 PHP开发 10584 views
2
Comments
<?php
/**
 * MrPmvc加密解密类
 * @author 狂奔的蜗牛
 * @email  672308444@163.com
 * @version alpha
 * Usage:
 *      $key = new KEY('adfasdfs', 30);
        $e_txt = $key->encode('中文哦..aaaa');
        echo $e_txt.'<br/>';
        echo $key->decode($e_txt);
 */
class KEY {
    private $expire = 0, $key = 'secret';

    public function __construct($key_str = null, $time_to_live = null) {
        if ($key_str) {
            self::setKey($key_str);
        }
        if ($time_to_live) {
            self::setExpire($time_to_live);
        }
    }

    public  function setExpire($time_to_live) {
        $this->expire = floatval($time_to_live);
    }

    public  function setKey($key_str) {
        $this->key = (string) $key_str;
    }

    public  function encode($tex, $key = null, $expire = 0) {
        $key = $key ? $key : $this->key;
        $expire = $expire ? $expire : $this->expire;
        $chrArr = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        $tex.="~#~" . sprintf('%010d', $expire ? $expire + time() : 0) . "~#~";
        $key_b = $chrArr[rand() % 62] . $chrArr[rand() % 62] . $chrArr[rand() % 62] . $chrArr[rand() % 62] . $chrArr[rand() % 62] . $chrArr[rand() % 62];
        $rand_key = $key_b . $key;
        $rand_key = md5($rand_key);
        $texlen = strlen($tex);
        $reslutstr = "";
        for ($i = 0; $i < $texlen; $i++) {
            $reslutstr.=$tex{$i} ^ $rand_key{$i % 32};
        }
        $reslutstr = trim($key_b . base64_encode($reslutstr), "==");
        $reslutstr = substr(md5($reslutstr), 0, 8) . $reslutstr;
        return $reslutstr;
    }

    public  function decode($tex, $key = null) {
        $key = $key ? $key : $this->key;
        if (strlen($tex) < 14)
            return false;
        $verity_str = substr($tex, 0, 8);
        $tex = substr($tex, 8);
        if ($verity_str != substr(md5($tex), 0, 8)) {
            //完整性验证失败
            return false;
        }
        $key_b = substr($tex, 0, 6);
        $rand_key = $key_b . $key;
        $rand_key = md5($rand_key);
        $tex = base64_decode(substr($tex, 6));
        $texlen = strlen($tex);
        $reslutstr = "";
        for ($i = 0; $i < $texlen; $i++) {
            $reslutstr.=$tex{$i} ^ $rand_key{$i % 32};
        }
        $expiry_arr = array();
        preg_match('/^(.*)~#~(\d{10})~#~$/', $reslutstr, $expiry_arr);
        if (count($expiry_arr) != 3) {
            //过期时间完整性验证失败
            return false;
        } else {
            $tex_time = $expiry_arr[2];
            if ($tex_time > 0 && $tex_time - time() <= 0) {
                //验证码过期
                return false;
            } else {
                $reslutstr = $expiry_arr[1];
            }
        }
        return $reslutstr;
    }

}
2012/09/18 13:11:53
评论(2)
2楼 王伟 评论时间:2014-10-22 17:25:09 引用
能加我QQ么?研究一下
1楼 3a技术网 评论时间:2013-10-04 22:52:29 引用
php简单验证码类(字母+数字)http://3aj.cn/php/27.html
共 2 个评论 1/1 页

发表评论

引用成功取消引用×

昵称*: 邮箱*: 网址:

最多可输入300字符 验证码*:


Process: 0.053s | DB :18 queries | UseMem:2,349 kb