PHP中的des加密类

2020-08-14 15:22:18 浏览数 (1)

代码语言:javascript复制
<?php
/**
 * DES加密类
 *
 * 本类用于实现des算法的加密及解密
 *
 * 调用方法
 *     $des = new DES('key值');  // key只能是八位
 *     $xx = $des->decrypt('xxxxx');    // 解密
 *     $aa = $des->encrypt('xxxaa');    // 加密
 */
class CI_Des {
    public $key='';
    
    //key长度8例如:1234abcd
    function __construct($params) {
        $key = empty($params[0]) ? '': $params[0];
        $this->key = $key;
    }
    
    function encrypt($encrypt) {
        $encrypt = $this->pkcs5_pad($encrypt);
        $passcrypt = @mcrypt_encrypt(MCRYPT_DES, $this->key, $encrypt, MCRYPT_MODE_CBC);
        return strtoupper(bin2hex($passcrypt));
    }

    function decrypt($decrypt) {
        $decoded = pack("H*", $decrypt);
        $decrypted = @mcrypt_decrypt(MCRYPT_DES, $this->key, $decoded, MCRYPT_MODE_CBC);
        return $this->pkcs5_unpad($decrypted);
    }

    function pkcs5_unpad($text) {
        $pad = ord($text{strlen($text)-1});    
        if ($pad > strlen($text)) {
            return $text;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return $text;
        }
        
        return substr($text, 0, -1 * $pad);
    }

    function pkcs5_pad($text) {
        $len = strlen($text);
        $mod = $len % 8;
        $pad = 8 - $mod;
        return $text.str_repeat(chr($pad),$pad);
    }

    //截中文英文子串
    function msubstr($str, $start, $len) {
        $tmpstr = "";
        $strlen = $start   $len;
        for($i = 0; $i < $strlen; $i  ) {
            if(ord(substr($str, $i, 1)) > 0xa0) {
                $tmpstr .= substr($str, $i, 2);
                $i  ;
            } else
                $tmpstr .= substr($str, $i, 1);
        }
        return $tmpstr;
    }
}

0 人点赞