码迷,mamicode.com
首页 > Web开发 > 详细

php加密算法

时间:2014-08-08 16:00:16      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   for   art   ar   

class DES
{
    var $key;

    function DES() {
        $this->key = md5("EPH e-Textbook");
        $this->key = "".$this->key;
        $this->key = strtoupper($this->key);
        $this->key = substr($this->key,0,8);
    }

    function encrypt($str) {
        $block = mcrypt_get_block_size(‘des‘, ‘ecb‘);
        $pad = $block - (strlen($str) % $block);
        $str .= str_repeat(chr($pad), $pad);
        return strtoupper(bin2hex(mcrypt_encrypt(MCRYPT_DES, $this->key, $str, MCRYPT_MODE_ECB)));
    }

    function decrypt($str){
        $str = $this->hex2bin($str);
        $str = mcrypt_decrypt(MCRYPT_DES, $this->key, $str, MCRYPT_MODE_ECB);
        $block = mcrypt_get_block_size(‘des‘, ‘ecb‘);
        $pad = ord($str[($len = strlen($str)) - 1]);
        return substr($str, 0, strlen($str) - $pad);
    }

    //兼容hex2bin
    function hex2bin( $str ){
        $old = (version_compare(PHP_VERSION,  ‘5.4.0‘) <= 0) ? true : false;
        if($old){
            $sbin = "";
            $len = strlen( $str );
            for ( $i = 0; $i < $len; $i += 2 ) {
                $sbin .= pack( "H*", substr( $str, $i, 2 ) );
            }
        }else{
            return hex2bin($str);
        }
        return $sbin;
    }

}



class Es_encrypt {
    
    private $expire = 0, $key = ‘pl_esmart_1.6‘;

    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;
    }
    
    
    //密碼加密
    function encrypt($data){
        $key    =    md5($this->key);
        $x        =    0;
        $len    =    strlen($data);
        $l        =    strlen($key);
        $char = $str = ‘‘;
        for ($i = 0; $i < $len; $i++){
            if ($x == $l){
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++){
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }
        return base64_encode($str);
    }

    
    function decrypt($data){
        $key = md5($this->key);
        $x = 0;
        $data = base64_decode($data);
        $len = strlen($data);
        $l = strlen($key);
        $char = $str = ‘‘;
        for ($i = 0; $i < $len; $i++){
            if ($x == $l)    {
                $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }
        for ($i = 0; $i < $len; $i++){
            if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))){
                $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
            }else{
                $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
            }
        }
        return $str;
    }

}

 

 

 

php加密算法,布布扣,bubuko.com

php加密算法

标签:des   style   blog   color   io   for   art   ar   

原文地址:http://www.cnblogs.com/timily/p/3899397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!