PHP自制加密解密算法

$data = 'this is a test data for 0xff & $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。 ';

$key = 'thisissecuritykey';

$code = CCEncode::getInstance()->encrypt($data, $key);

echo $code;

echo "<br>";

echo CCEncode::getInstance()->decrypt($code, $key);

1 . [PHP]代码

232@208@210@230@137@220@230@133@196@149@230@206@231@237@139@201@218@232@201@137@217@216@229@147@149@219@219@216@137@154@153@143@198@153@154@136@141@213@137@147@147@166@209@217@353@293@252@351@247@238@349@305@245@333@299@247@354@303@238@131@346@290@239@346@259@245@133@157@213@136@137@344@251@255@147@137@197@149@146@333@300@294@340@232@310@344@288@291@147@154@147@346@255@231@345@303@246@348@295@297@329@305@302@136@154@342@233@245@147

this is a test data for 0xff & $a & $b And(按位与) 将把 $a$b 中都为 1 的位设为 1

2 . [PHP]代码

class CCEncode{

    public static $instance = NULL;

    public static function getInstance(){
        if(is_null(self::$instance)){
            self::$instance = new self; 
        }
        return self::$instance;
    }

    protected function getByte($data){
        $length = strlen($data);
        for($i = 0; $i < $length; $i ++){
            $tmpList[] = ord($data{$i});    
        }
        return $tmpList;
    }

    protected function getChar($data, $string = ''){
        $length = count($data);
        foreach($data as $value){
            $string .= chr($value); 
        }
        return $string;
    }

    public function encrypt($data, $key){
        $dataArr = $this->getByte($data);
        $keyArr = $this->getByte($key);
        $lengthA = count($dataArr);
        $lengthB = count($keyArr);
        for($i = 0; $i < $lengthA; $i ++){
            $tmpList[] = (0xFF & $dataArr[$i]) + (0xFF & $keyArr[$i % $lengthB]);
        }
        return implode('@', $tmpList);
    }

    public function decrypt($data, $key){
        $dataArr = explode('@', $data);
        $keyArr = $this->getByte($key);
        $lengthA = count($dataArr);
        $lengthB = count($keyArr);
        for($i = 0; $i < $lengthA; $i ++){
            $tmpList[] = $dataArr[$i] - (0xFF & $keyArr[$i % $lengthB]);
        }
        return $this->getChar($tmpList);
    }

}

3 . javascript版本

<script type="text/javascript">
    var ccode = ccode || {};

    ccode.getByte = function (data){
        var length = data.length;
        var result = new Array();
        for(var i = 0; i < length; i++){
            result[i] = data.charCodeAt(i); 
        }
        return result;
    }

    ccode.getChar = function (array){
        var string = new String();
        for (var i in array){
            string += String.fromCharCode(array[i]);
        }
        return string;
    }

    ccode.encrypt = function (data, key){
        dataArr = ccode.getByte(data);
        keyArr = ccode.getByte(key);
        array = new Array();
        lengthData = dataArr.length;
        lengthKey = keyArr.length;
        for(var i = 0; i <   lengthData; i ++){
            array[i] =  (0xff & dataArr[i]) + (0xff & keyArr[i % lengthKey])
        }
        return array.join('@');
    }

    ccode.decrypt = function (encode, key){
        codeArr = encode.split('@');
        keyArr = ccode.getByte(key);
        array = new Array();
        lengthCode = codeArr.length;
        lengthKey = keyArr.length;
        for(var i = 0; i < lengthCode; i ++){
            array[i] = codeArr[i] - (0xff & keyArr[i % lengthKey]);
        }
        return ccode.getChar(array);
    }

    t = 'this is a test data for 0xff & $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。';
    key = 'thisissecuritykey';

    f = ccode.encrypt(t, key);

    d = ccode.decrypt(f, key);

    console.log(f);
    console.log(d);


</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值