zoukankan      html  css  js  c++  java
  • PHP数据压缩、加解密(pack, unpack)

     

    PHP数据压缩、加解密(pack, unpack)

     
    网络通信、文件存储中经常需要交换数据,为了减少网络通信流量、文件存储大小以及加密通信规则,经常需要对数据进行双向加解密以保证数据的安全。
    PHP中实现此功能主要需要使用的函数主要是pack及unpack函数
     
    pack

    压缩资料到位字符串之中。

    语法: string pack(string format, mixed [args]...);

    返回值: 字符串
    本函数用来将资料压缩打包到位的字符串之中。
     
    a - NUL- 字符串填满[padded string] 将字符串空白以 NULL 字符填满
    A - SPACE- 字符串填满[padded string]
    h – 十六进制字符串,低“四位元”[low nibble first] (低位在前)
    H - 十六进制字符串,高“四位元”[high nibble first](高位在前)
    c – 带有符号的字符
    C – 不带有符号的字符
    s – 带有符号的短模式[short](通常是16位,按机器字节顺序)
    S – 不带有符号的短模式[short](通常是16位,按机器字节排序)
    n -不带有符号的短模式[short](通常是16位,按大endian字节排序)
    v -不带有符号的短模式[short](通常是16位,按小endian字节排序)
    i – 带有符号的整数(由大小和字节顺序决定)
    I – 不带有符号的整数(由大小和字节顺序决定)
    l– 带有符号的长模式[long](通常是32位,按机器字节顺序)
    L – 不带有符号的长模式[long](通常是32位,按机器字节顺序)
    N – 不带有符号的长模式[long](通常是32位,按大edian字节顺序)
    V– 不带有符号的长模式[long](通常是32位,按小edian字节顺序)
    f –浮点(由大小和字节顺序决定)
    d – 双精度(由大小和字节顺序决定)
    x – 空字节[NUL byte]
    X- 后面一个字节[Back up one byte](倒回一位)
     
     

    unpack

    解压缩位字符串资料。

    语法: string pack(string format, mixed [args]...);

    返回值: 数组
    本函数用来将位的字符串的资料解压缩。本函数和 Perl 的同名函数功能用法完全相同。
     
    案例一、pack实现缩减文件数据存储大小
    [php] view plaincopy
     
     
    1. <?php  
    2. //存储整数1234567890  
    3. file_put_contents("test.txt", 1234567890);  
    此时test.txt的文件大小是10byte。注意此时文件大小是10字节,实际占用空间大小是1KB。
     
    上面存储的整数实际是以字符串形式存储于文件test.txt中。
    但如果以整数的二进制字符串存jy储,将会缩减至4byte。
    [php] view plaincopy
     
     
    1. <?php  
    2. print_r(unpack("i", file_get_contents("test.txt")));  
     
     
     
    案例二、数据加密
    以字符串形式存储一段有意义数据,7-110-abcdefg-117。
    字符"-"分割后,第一位表示字符串长度,第二位表示存储位置,第三位表示实际存储的字符串,第四位表示结尾位置。
    [php] view plaincopy
     
     
    1. <?php  
    2. file_put_contents("test.txt", "7-110-abcdefg-117");  
    上述方法缺点:
    一、数据存储大小
    二、数据以明文方式存储,如果是任何敏感信息,都可能造成不安全访问。
    三、文件存储大小,以不规则方式递增。
     
    加密:
    [php] view plaincopy
     
     
    1. <?php  
    2. file_put_contents("test.txt", pack("i2a7i1", 7, 110, "abcdefg", 117));  
    存储一段数据,加密格式为:整数2位长度字符串10位长度整数1位长度。
    优点:
    一、数据大小最优化
    二、在不知道"i2a7i1"这样的压缩格式时,即使拿到文件,也无法正确读出二进制文件转化为明文。
    三、数据增加时,文件存储大小是等量递增。每次都是以19byte递增。
     
    案例三、key-value型文件存储
    存储生成的文件为两个:索引文件,数据文件
    文件中数据存储的格式如下图:
    代码实现:
    [php] view plaincopy
     
     
    1. <?php  
    2. error_reporting(E_ALL);  
    3.   
    4. class fileCacheException extends Exception{  
    5.   
    6. }  
    7.   
    8. //Key-Value型文件存储  
    9. class fileCache{  
    10.      private $_file_header_size = 14;  
    11.      private $_file_index_name;  
    12.      private $_file_data_name;  
    13.      private $_file_index;//索引文件句柄  
    14.      private $_file_data;//数据文件句柄  
    15.      private $_node_struct;//索引结点结构体  
    16.      private $_inx_node_size = 36;//索引结点大小  
    17.   
    18.      public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){  
    19.           $this->_node_struct = array(  
    20.                'next'=>array(1, 'V'),  
    21.                'prev'=>array(1, 'V'),  
    22.               'data_offset'=>array(1,'V'),//数据存储起始位置  
    23.               'data_size'=>array(1,'V'),//数据长度  
    24.               'ref_count'=>array(1,'V'),//引用此处,模仿PHP的引用计数销毁模式  
    25.               'key'=>array(16,'H*'),//存储KEY  
    26.           );  
    27.   
    28.           $this->_file_index_name = $file_index;  
    29.           $this->_file_data_name = $file_data;  
    30.   
    31.           if(!file_exists($this->_file_index_name)){  
    32.                $this->_create_index();  
    33.           }else{  
    34.                $this->_file_index = fopen($this->_file_index_name, "rb+");  
    35.           }  
    36.   
    37.           if(!file_exists($this->_file_data_name)){  
    38.                $this->_create_data();  
    39.           }else{  
    40.                $this->_file_data = fopen($this->_file_data_name, "rb+");//二进制存储需要使用b  
    41.           }  
    42.      }  
    43.   
    44.      //创建索引文件  
    45.      private function _create_index(){  
    46.           $this->_file_index = fopen($this->_file_index_name, "wb+");//二进制存储需要使用b  
    47.           if(!$this->_file_index)   
    48.                throw new fileCacheException("Could't open index file:".$this->_file_index_name);  
    49.   
    50.           $this->_index_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php标记防止下载  
    51.           $this->_index_puts($this->_file_header_size, pack("V1", 0));  
    52.      }  
    53.   
    54.   
    55.      //创建存储文件  
    56.      private function _create_data(){  
    57.           $this->_file_data = fopen($this->_file_data_name, "wb+");//二进制存储需要使用b  
    58.           if(!$this->_file_index)   
    59.                throw new fileCacheException("Could't open index file:".$this->_file_data_name);  
    60.   
    61.           $this->_data_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php标记防止下载  
    62.      }  
    63.   
    64.      private function _index_puts($offset, $data, $length=false){  
    65.           fseek($this->_file_index, $offset);  
    66.   
    67.           if($length)  
    68.           fputs($this->_file_index, $data, $length);  
    69.           else  
    70.           fputs($this->_file_index, $data);  
    71.      }  
    72.   
    73.      private function _data_puts($offset, $data, $length=false){  
    74.           fseek($this->_file_data, $offset);  
    75.           if($length)  
    76.           fputs($this->_file_data, $data, $length);  
    77.           else  
    78.           fputs($this->_file_data, $data);  
    79.      }  
    80.   
    81.      /** 
    82.      * 文件锁 
    83.      * @param $is_block 是否独占、阻塞锁 
    84.      */  
    85.      private function _lock($file_res, $is_block=true){  
    86.           flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB);  
    87.      }  
    88.   
    89.      private function _unlock($file_res){  
    90.           flock($file_res, LOCK_UN);  
    91.      }  
    92.   
    93.      public function add($key, $value){  
    94.           $key = md5($key);  
    95.           $value = serialize($value);  
    96.           $this->_lock($this->_file_index, true);  
    97.           $this->_lock($this->_file_data, true);  
    98.   
    99.           fseek($this->_file_index, $this->_file_header_size);  
    100.   
    101.           list(, $index_count) = unpack('V1', fread($this->_file_index, 4));  
    102.   
    103.           $data_size = filesize($this->_file_data_name);  
    104.   
    105.           fseek($this->_file_data, $data_size);  
    106.   
    107.           $value_size = strlen($value);  
    108.   
    109.           $this->_data_puts(filesize($this->_file_data_name), $value);  
    110.   
    111.           $node_data =   
    112.           pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $key);  
    113.   
    114.           $index_count++;  
    115.   
    116.           $this->_index_puts($this->_file_header_size, $index_count, 4);  
    117.   
    118.           $this->_index_puts($this->get_new_node_pos($index_count), $node_data);  
    119.   
    120.           $this->_unlock($this->_file_data);  
    121.           $this->_unlock($this->_file_index);  
    122.      }  
    123.   
    124.      public function get_new_node_pos($index_count){  
    125.           return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1);  
    126.      }  
    127.   
    128.      public function get_node($key){  
    129.           $key = md5($key);  
    130.           fseek($this->_file_index, $this->_file_header_size);  
    131.           $index_count = fread($this->_file_index, 4);  
    132.   
    133.           if($index_count>0) {  
    134.                for ($i=0; $i < $index_count ; $i++) {   
    135.                     fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i);  
    136.                     $data = fread($this->_file_index, $this->_inx_node_size);  
    137.                     $node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data);  
    138.   
    139.                     if($key == $node['key']){  
    140.                          return $node;  
    141.                     }  
    142.                }  
    143.           }else{  
    144.                return null;  
    145.           }  
    146.      }  
    147.   
    148.      public function get_data($offset, $length){  
    149.           fseek($this->_file_data, $offset);  
    150.           return unserialize(fread($this->_file_data, $length));  
    151.      }  
    152. }  
    153.   
    154. //使用方法  
    155. $cache = new fileCache();  
    156. $cache->add('abcdefg' , 'testabc');  
    157. $data = $cache->get_node('abcdefg');  
    158. print_r($data);  
    159. echo $cache->get_data($data['data_offset'], $data['data_size']);  


     
    案例四、socket通信加密
    通信双方都定义好加密格式:
    例如:
    [php] view plaincopy
     
     
    1. $LOGIN = array(  
    2.      'COMMAND'=>array('a30', 'LOGIN'),  
    3.      'DATA'=>array('a30', 'HELLO')  
    4. );  
    5.   
    6. $LOGOUT = array(  
    7.      'COMMAND'=>array('a30', 'LOGOUT'),  
    8.      'DATA'=>array('a30', 'GOOD BYE')  
    9. );  
    10.   
    11. $LOGIN_SUCCESS = array(  
    12.      'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),  
    13.      'DATA'=>array('V1', 1)  
    14. );  
    15.   
    16. $LOGOUT_SUCCESS = array(  
    17.      'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),  
    18.      'DATA'=>array('V1', time())  
    19. );  
    服务器端与客户端根据解析COMMAND格式,找到对应的DATA解码方式,得到正确的数据.
     
     
    http://blog.csdn.net/yongcto/article/details/17366605
  • 相关阅读:
    Linux内核RPC请求过程
    二分图
    Java实现 蓝桥杯 算法提高 合并石子
    Java实现 蓝桥杯 算法提高 合并石子
    Java实现 蓝桥杯 算法提高 摩尔斯电码
    Java实现 蓝桥杯 算法提高 摩尔斯电码
    Java实现 蓝桥杯 算法提高 文本加密
    Java实现 蓝桥杯 算法提高 文本加密
    Java蓝桥杯 算法提高 九宫格
    Java蓝桥杯 算法提高 九宫格
  • 原文地址:https://www.cnblogs.com/lemon66/p/4047975.html
Copyright © 2011-2022 走看看