参考

https://github.com/conzi/php-base64

目的

在原来的基础上,加了一个设置偏移的参数。把设置key放到了__construct
可以设置自定义key和偏移,就可以当做是稍微有强度的编码,可以一种数据编码的方式,也可以把解密函数写成js,作为对抗爬虫的一种方式。

运行结果


原数据:
北京市海淀区北四环西路9号218B3
默认key
5YyX5Lqs5biC5rW35reA5Yy65YyX5Zub546v6KW/6LevOeWPtzIxOEIz
北京市海淀区北四环西路9号218B3

设置自定义key
6ZzY6Mrt6cjD6sX46sfB6Zz76ZzY6avc657w7LXA7MfwPfXQu0JyPFJ0
北京市海淀区北四环西路9号218B3

按照自增番号偏移
a5T4asLNa8DjaM3YaM/ha5Tba5T4a6P8aZbQbr3gbs/Qv/3wOUpSvlpU
北京市海淀区北四环西路9号218B3

完整代码

<?php
class Base64 {
    private $_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    private $_PADCHAR = '=';
    public function __construct($key = '', $number = false){
        if($key && strlen($key) == 64){
            $this->_alpha = $key;
        }
        //按照number偏移
        if(is_numeric($number)){
            $number = $number % 64;
            $this->_alpha = substr($this->_alpha.$this->_alpha,$number,64);
        }
    }
    private function _getbyte64($str, $i){
        $idx = strpos($this->_alpha , $str[$i]);
        if ( $idx === -1 ) {
            trigger_error("Cannot decode base64",E_USER_ERROR);
        }
        return $idx;
    }
    private function  _getbyte( $s, $i ) {
        $x = ord($s[$i]);
        if ( $x > 255 ) {
            trigger_error("INVALID_CHARACTER_ERR: DOM Exception 5",E_USER_ERROR);
        }
        return $x;
    }
    public function encode($s='') {

        $s = (string)$s;
        $x = array();
        $imax = strlen($s) - strlen($s) % 3;
        $b10 =0;
        if ( strlen($s) === 0 ) {
          return $s;
        }
        for ( $i = 0; $i < $imax; $i += 3 ) {
          $b10 = ( $this->_getbyte( $s, $i ) << 16 ) | ( $this->_getbyte( $s, $i + 1 ) << 8 ) | $this->_getbyte( $s, $i + 2 );
          $x[] = ( $this->_alpha[( $b10 >> 18 )] );
          $x[] = ( $this->_alpha[( ( $b10 >> 12 ) & 0x3F )] );
          $x[] = ( $this->_alpha[( ( $b10 >> 6 ) & 0x3f )] );
          $x[] = ( $this->_alpha[( $b10 & 0x3f )] );
        }
        switch ( strlen($s) - $imax ) {
          case 1:
            $b10 = $this->_getbyte( $s, $i ) << 16;
            $x[] = ( $this->_alpha[( $b10 >> 18 )] .$this->_alpha[( ( $b10 >> 12 ) & 0x3F )] . $this->_PADCHAR . $this->_PADCHAR );
            break;
          case 2:
            $b10 = ( $this->_getbyte( $s, $i ) << 16 ) | ( $this->_getbyte( $s, $i + 1 ) << 8 );
            $x[] = ( $this->_alpha[( $b10 >> 18 )] . $this->_alpha[( ( $b10 >> 12 ) & 0x3F )] . $this->_alpha[( ( $b10 >> 6 ) & 0x3f )] . $this->_PADCHAR );
            break;
        }
        return implode('', $x);
  }
    public function decode ($s=''){
        $s = (string)$s;
        $pads = 0;
        $imax = strlen($s);
        $x = array();
        $b10 = 0;
        if ( $imax === 0 ) {
          return $s;
        }
        if ( $imax % 4 !== 0 ) {
          trigger_error("Cannot decode base64",E_USER_ERROR);
        }
        if ( $s[$imax - 1 ] === $this->_PADCHAR ) {
          $pads = 1;
          if ( $s[$imax - 2] === $this->_PADCHAR ) {
            $pads = 2;
          }
          // either way, we want to ignore this last block
          $imax -= 4;
        }
        for ( $i = 0; $i < $imax; $i += 4 ) {
          $b10 = ( $this->_getbyte64( $s, $i ) << 18 ) | ( $this->_getbyte64( $s, $i + 1 ) << 12 ) | ($this->_getbyte64( $s, $i + 2 ) << 6 ) | $this->_getbyte64( $s, $i + 3 );
          $x[] = ( chr( $b10 >> 16) . chr( ( $b10 >> 8 ) & 0xff ) . chr($b10 & 0xff ) );
        }
        switch ( $pads ) {
          case 1:
            $b10 = ( $this->_getbyte64( $s, $i ) << 18 ) | ( $this->_getbyte64( $s, $i + 1 ) << 12 ) | ( $this->_getbyte64( $s, $i + 2 ) << 6 );
            $x[] = ( chr( $b10 >> 16 )  . chr (( $b10 >> 8 ) & 0xff ) );
            break;
          case 2:
            $b10 = ( $this->_getbyte64( $s, $i ) << 18) | ( $this->_getbyte64( $s, $i + 1 ) << 12 );
            $x[] = ( chr( $b10 >> 16 ) );
            break;
        }
        return implode('', $x);
    }
}

$str = '北京市海淀区北四环西路9号218B3';
echo "原数据:\n".$str."\n";

//默认key
echo "默认key\n";
$base64 = new Base64();
$enc = $base64->encode($str);
$dec = $base64->decode($enc);

echo $enc."\n";
echo $dec."\n\n";

//设置自定义key
echo "设置自定义key\n";
$base64 = new Base64('BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/A');
$enc = $base64->encode($str);
$dec = $base64->decode($enc);

echo $enc."\n";
echo $dec."\n\n";

//自定义key
//按照自增番号偏移
echo "按照自增番号偏移\n";
$base64 = new Base64('',7777);
$enc = $base64->encode($str);
$dec = $base64->decode($enc);

echo $enc."\n";
echo $dec."\n\n";
最后修改:2018 年 02 月 28 日 05 : 36 PM